Skip to content

Commit af825ba

Browse files
authored
Merge pull request #428 from bracteatus/master
Solutions to Challenge 018, #1 and #2.
2 parents fb872e9 + 85119de commit af825ba

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed

challenge-018/jaime/README

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
1-
Solution by Jaime Corchado, (@tortsnare)[https://twitter.com/tortsnare].
2-
31
# Challenge #1
42

5-
Write a script to generate Van Eck’s sequence.
3+
Write a script that takes 2 or more strings as command line parameters
4+
and print the longest common substring. For example, the longest
5+
common substring of the strings “ABABC”, “BABCA” and “ABCBA” is string
6+
“ABC” of length 3. Other common substrings are “A”, “AB”, “B”, “BA”,
7+
“BC” and “C”.
8+
9+
## Solution
10+
11+
Substrings of the first argument are iterated by decreasing length.
12+
13+
The script finds the `$substring` that is common within the list of
14+
parameters `@ARGV == grep(/$substring/,@ARGV)`.
615

716
# Challenge #2
817

9-
Using only the official postal (2-letter) abbreviations for the 50 U.S. states,
10-
write a script to find the longest English word you can spell.
18+
Write a script to implement Priority Queue. It is like regular queue
19+
except each element has a priority associated with it. In a priority
20+
queue, an element with high priority is served before an element with
21+
low priority. Please check this wiki page for more informations. It
22+
should serve the following operations:
23+
24+
1. `is_empty`: check whether the queue has no elements.
25+
26+
2. `insert_with_priority`: add an element to the queue with an
27+
associated priority.
28+
29+
3. `pull_highest_priority_element`: remove the element from the queue
30+
that has the highest priority, and return it. If two elements have the
31+
same priority, then return element added first.
32+
33+
## Solution
1134

12-
# Challenge #3
35+
The solution is a hashmap of priority and anonymous arrays.
1336

14-
Find the given city current time using the Geo DB Cities API.
37+
The anonymous array `[]` is accessed with a `$priority`.
38+
`$queue` is the hashmap. `$queue->{$priority}` is an anonymous array.
39+
`@{$queue->{$priority}}` wraps access to the prioritized queues as arrays.
1540

41+
`pull_highest_priority_element()` parses the `$queue` to find the
42+
nonempty array with the largest priority, then returns the first entry.

challenge-018/jaime/perl5/ch-1.pl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# # Challenge #1
3+
#
4+
# Write a script that takes 2 or more strings as command line parameters
5+
# and print the longest common substring.
6+
7+
my $head = shift;
8+
for my $n (reverse 1..(length $head)) {
9+
SUBSTRING: for my $i (0..((length $head)-$n)) {
10+
my $s = substr($head,$i,$n);
11+
next SUBSTRING unless @ARGV == grep(/$s/,@ARGV);
12+
print "$s\n";
13+
exit;
14+
}
15+
}

challenge-018/jaime/perl5/ch-2.pl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# # Challenge #2
3+
#
4+
# Write a script to implement Priority Queue. It is like regular queue
5+
# except each element has a priority associated with it. In a priority
6+
# queue, an element with high priority is served before an element with
7+
# low priority. It should serve the following operations:
8+
#
9+
# 1. is_empty: check whether the queue has no elements.
10+
#
11+
# 2. insert_with_priority: add an element to the queue with an
12+
# associated priority.
13+
#
14+
# 3. pull_highest_priority_element: remove the element from the queue
15+
# that has the highest priority, and return it. If two elements have the
16+
# same priority, then return element added first.
17+
18+
my $queue = {};
19+
20+
sub is_empty {
21+
return not map { @{$_} } values $queue;
22+
}
23+
24+
sub insert_with_priority {
25+
my ($priority,$body) = @_;
26+
$queue->{$priority} = [] unless $queue->{$priority};
27+
push @{$queue->{$priority}}, $body;
28+
}
29+
30+
sub pull_highest_priority_element {
31+
for my $priority (reverse sort keys %$queue) {
32+
return shift @{$queue->{$priority}} if @{$queue->{$priority}};
33+
}
34+
}

0 commit comments

Comments
 (0)