aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-07-28 03:44:16 +0100
committerGitHub <noreply@github.com>2019-07-28 03:44:16 +0100
commitaf825ba1d040029c1583e2f3293eb77af44320ef (patch)
treef28825db12ffeb682328537e670e4934cfd812b0
parentfb872e922475cb55c557b5b1a9c56674418264c1 (diff)
parent85119de4e314c3ab930d7369e20222a6e2f61144 (diff)
downloadperlweeklychallenge-club-af825ba1d040029c1583e2f3293eb77af44320ef.tar.gz
perlweeklychallenge-club-af825ba1d040029c1583e2f3293eb77af44320ef.tar.bz2
perlweeklychallenge-club-af825ba1d040029c1583e2f3293eb77af44320ef.zip
Merge pull request #428 from bracteatus/master
Solutions to Challenge 018, #1 and #2.
-rw-r--r--challenge-018/jaime/README41
-rw-r--r--challenge-018/jaime/perl5/ch-1.pl15
-rw-r--r--challenge-018/jaime/perl5/ch-2.pl34
3 files changed, 83 insertions, 7 deletions
diff --git a/challenge-018/jaime/README b/challenge-018/jaime/README
index 9acbfee5ea..c1fbac90da 100644
--- a/challenge-018/jaime/README
+++ b/challenge-018/jaime/README
@@ -1,15 +1,42 @@
-Solution by Jaime Corchado, (@tortsnare)[https://twitter.com/tortsnare].
-
# Challenge #1
-Write a script to generate Van Eck’s sequence.
+Write a script that takes 2 or more strings as command line parameters
+and print the longest common substring. For example, the longest
+common substring of the strings “ABABC”, “BABCA” and “ABCBA” is string
+“ABC” of length 3. Other common substrings are “A”, “AB”, “B”, “BA”,
+“BC” and “C”.
+
+## Solution
+
+Substrings of the first argument are iterated by decreasing length.
+
+The script finds the `$substring` that is common within the list of
+parameters `@ARGV == grep(/$substring/,@ARGV)`.
# Challenge #2
-Using only the official postal (2-letter) abbreviations for the 50 U.S. states,
-write a script to find the longest English word you can spell.
+Write a script to implement Priority Queue. It is like regular queue
+except each element has a priority associated with it. In a priority
+queue, an element with high priority is served before an element with
+low priority. Please check this wiki page for more informations. It
+should serve the following operations:
+
+1. `is_empty`: check whether the queue has no elements.
+
+2. `insert_with_priority`: add an element to the queue with an
+associated priority.
+
+3. `pull_highest_priority_element`: remove the element from the queue
+that has the highest priority, and return it. If two elements have the
+same priority, then return element added first.
+
+## Solution
-# Challenge #3
+The solution is a hashmap of priority and anonymous arrays.
-Find the given city current time using the Geo DB Cities API.
+The anonymous array `[]` is accessed with a `$priority`.
+`$queue` is the hashmap. `$queue->{$priority}` is an anonymous array.
+`@{$queue->{$priority}}` wraps access to the prioritized queues as arrays.
+`pull_highest_priority_element()` parses the `$queue` to find the
+nonempty array with the largest priority, then returns the first entry.
diff --git a/challenge-018/jaime/perl5/ch-1.pl b/challenge-018/jaime/perl5/ch-1.pl
new file mode 100644
index 0000000000..afc6d29993
--- /dev/null
+++ b/challenge-018/jaime/perl5/ch-1.pl
@@ -0,0 +1,15 @@
+#
+# # Challenge #1
+#
+# Write a script that takes 2 or more strings as command line parameters
+# and print the longest common substring.
+
+my $head = shift;
+for my $n (reverse 1..(length $head)) {
+ SUBSTRING: for my $i (0..((length $head)-$n)) {
+ my $s = substr($head,$i,$n);
+ next SUBSTRING unless @ARGV == grep(/$s/,@ARGV);
+ print "$s\n";
+ exit;
+ }
+}
diff --git a/challenge-018/jaime/perl5/ch-2.pl b/challenge-018/jaime/perl5/ch-2.pl
new file mode 100644
index 0000000000..d4ddde212e
--- /dev/null
+++ b/challenge-018/jaime/perl5/ch-2.pl
@@ -0,0 +1,34 @@
+#
+# # Challenge #2
+#
+# Write a script to implement Priority Queue. It is like regular queue
+# except each element has a priority associated with it. In a priority
+# queue, an element with high priority is served before an element with
+# low priority. It should serve the following operations:
+#
+# 1. is_empty: check whether the queue has no elements.
+#
+# 2. insert_with_priority: add an element to the queue with an
+# associated priority.
+#
+# 3. pull_highest_priority_element: remove the element from the queue
+# that has the highest priority, and return it. If two elements have the
+# same priority, then return element added first.
+
+my $queue = {};
+
+sub is_empty {
+ return not map { @{$_} } values $queue;
+}
+
+sub insert_with_priority {
+ my ($priority,$body) = @_;
+ $queue->{$priority} = [] unless $queue->{$priority};
+ push @{$queue->{$priority}}, $body;
+}
+
+sub pull_highest_priority_element {
+ for my $priority (reverse sort keys %$queue) {
+ return shift @{$queue->{$priority}} if @{$queue->{$priority}};
+ }
+}