From 2ee0e7a9016e2d68e0791a6b20870ea6d02931e1 Mon Sep 17 00:00:00 2001 From: bracteatus <42359730+bracteatus@users.noreply.github.com> Date: Fri, 26 Jul 2019 18:56:54 -0600 Subject: Update README --- challenge-018/jaime/README | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/challenge-018/jaime/README b/challenge-018/jaime/README index 9acbfee5ea..6dd7a66fdb 100644 --- a/challenge-018/jaime/README +++ b/challenge-018/jaime/README @@ -1,15 +1,15 @@ -Solution by Jaime Corchado, (@tortsnare)[https://twitter.com/tortsnare]. +(@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”. # 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: -# Challenge #3 +1. is_empty: check whether the queue has no elements. -Find the given city current time using the Geo DB Cities API. +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. -- cgit From 9c24f79c6aac8a2d5e906a8aefbfb70fe59bc40e Mon Sep 17 00:00:00 2001 From: Jaime <42359730+bracteatus@users.noreply.github.com> Date: Fri, 26 Jul 2019 19:03:16 -0600 Subject: Update README --- challenge-018/jaime/README | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/challenge-018/jaime/README b/challenge-018/jaime/README index 6dd7a66fdb..62a35b5a51 100644 --- a/challenge-018/jaime/README +++ b/challenge-018/jaime/README @@ -1,15 +1,24 @@ -(@tortsnare)[https://twitter.com/tortsnare] - # Challenge #1 -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”. +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”. # 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. Please check this wiki page for more informations. It should serve the following operations: +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. +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. +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. -- cgit From 8730a72d03faaed9e6c4f419e9b06805fb3171f2 Mon Sep 17 00:00:00 2001 From: Jaime <42359730+bracteatus@users.noreply.github.com> Date: Fri, 26 Jul 2019 19:04:16 -0600 Subject: Create ch-1.pl --- challenge-018/jaime/perl5/ch-1.pl | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 challenge-018/jaime/perl5/ch-1.pl diff --git a/challenge-018/jaime/perl5/ch-1.pl b/challenge-018/jaime/perl5/ch-1.pl new file mode 100644 index 0000000000..b5cd68a3c6 --- /dev/null +++ b/challenge-018/jaime/perl5/ch-1.pl @@ -0,0 +1,8 @@ +# +# # Challenge #1 +# +# 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”. -- cgit From 7ec4eedfe14d441bdf41dce04f271ec9264314b7 Mon Sep 17 00:00:00 2001 From: Jaime <42359730+bracteatus@users.noreply.github.com> Date: Fri, 26 Jul 2019 19:04:21 -0600 Subject: Create ch-2.pl --- challenge-018/jaime/perl5/ch-2.pl | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 challenge-018/jaime/perl5/ch-2.pl diff --git a/challenge-018/jaime/perl5/ch-2.pl b/challenge-018/jaime/perl5/ch-2.pl new file mode 100644 index 0000000000..6689a671fb --- /dev/null +++ b/challenge-018/jaime/perl5/ch-2.pl @@ -0,0 +1,17 @@ +# +# # 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. 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. -- cgit From 81caa1238297c1e7aba934168200c314265bd735 Mon Sep 17 00:00:00 2001 From: Jaime <42359730+bracteatus@users.noreply.github.com> Date: Fri, 26 Jul 2019 23:51:27 -0600 Subject: Update ch-1.pl Iterate ordered substrings to find largest common substring. --- challenge-018/jaime/perl5/ch-1.pl | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/challenge-018/jaime/perl5/ch-1.pl b/challenge-018/jaime/perl5/ch-1.pl index b5cd68a3c6..ce9415e8c2 100644 --- a/challenge-018/jaime/perl5/ch-1.pl +++ b/challenge-018/jaime/perl5/ch-1.pl @@ -2,7 +2,23 @@ # # Challenge #1 # # 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”. +# and print the longest common substring. + +sub of_size { + my ($n,$s) = @_; + my @subs = (); + for (my $i = 0; ($i+$n) <= length $s; $i++) { + push @subs, substr($s,$i,$n); + } + return @subs; #substrings of $s of length $n. +} + +my $head = shift; +for my $n (reverse 1..(length $head)) { + for my $s (of_size($n,$head)) { + if (@ARGV == grep(/$s/,@ARGV)) { + print "$s\n"; + exit; + } + } +} -- cgit From 035b63413959f8a7218f0595751b14dd7d26a3b4 Mon Sep 17 00:00:00 2001 From: Jaime <42359730+bracteatus@users.noreply.github.com> Date: Sat, 27 Jul 2019 00:07:11 -0600 Subject: Update ch-1.pl Iterate indices instead of substrings. --- challenge-018/jaime/perl5/ch-1.pl | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/challenge-018/jaime/perl5/ch-1.pl b/challenge-018/jaime/perl5/ch-1.pl index ce9415e8c2..eeb61845eb 100644 --- a/challenge-018/jaime/perl5/ch-1.pl +++ b/challenge-018/jaime/perl5/ch-1.pl @@ -4,18 +4,10 @@ # Write a script that takes 2 or more strings as command line parameters # and print the longest common substring. -sub of_size { - my ($n,$s) = @_; - my @subs = (); - for (my $i = 0; ($i+$n) <= length $s; $i++) { - push @subs, substr($s,$i,$n); - } - return @subs; #substrings of $s of length $n. -} - my $head = shift; for my $n (reverse 1..(length $head)) { - for my $s (of_size($n,$head)) { + for my $i (0..((length $head)-$n)) { + my $s = substr($head,$i,$n); if (@ARGV == grep(/$s/,@ARGV)) { print "$s\n"; exit; -- cgit From b71b18eb68337ed77faf2a5c6ffa9e569a39b91d Mon Sep 17 00:00:00 2001 From: Jaime <42359730+bracteatus@users.noreply.github.com> Date: Sat, 27 Jul 2019 00:21:57 -0600 Subject: Update ch-1.pl Use loop labels. --- challenge-018/jaime/perl5/ch-1.pl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/challenge-018/jaime/perl5/ch-1.pl b/challenge-018/jaime/perl5/ch-1.pl index eeb61845eb..afc6d29993 100644 --- a/challenge-018/jaime/perl5/ch-1.pl +++ b/challenge-018/jaime/perl5/ch-1.pl @@ -6,11 +6,10 @@ my $head = shift; for my $n (reverse 1..(length $head)) { - for my $i (0..((length $head)-$n)) { + SUBSTRING: for my $i (0..((length $head)-$n)) { my $s = substr($head,$i,$n); - if (@ARGV == grep(/$s/,@ARGV)) { - print "$s\n"; - exit; - } + next SUBSTRING unless @ARGV == grep(/$s/,@ARGV); + print "$s\n"; + exit; } } -- cgit From 9fd8af41093e0041df6f69debc6fe7b66ffcc6fb Mon Sep 17 00:00:00 2001 From: Jaime <42359730+bracteatus@users.noreply.github.com> Date: Sat, 27 Jul 2019 00:24:46 -0600 Subject: Update ch-2.pl --- challenge-018/jaime/perl5/ch-2.pl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/challenge-018/jaime/perl5/ch-2.pl b/challenge-018/jaime/perl5/ch-2.pl index 6689a671fb..10dce92536 100644 --- a/challenge-018/jaime/perl5/ch-2.pl +++ b/challenge-018/jaime/perl5/ch-2.pl @@ -4,8 +4,7 @@ # 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: +# low priority. It should serve the following operations: # # 1. is_empty: check whether the queue has no elements. # -- cgit From 6e475c2d984d2396ed72b73919d0e03e8aca8e09 Mon Sep 17 00:00:00 2001 From: Jaime <42359730+bracteatus@users.noreply.github.com> Date: Sat, 27 Jul 2019 17:16:50 -0600 Subject: Update ch-2.pl Snappy implementation of priority queue. --- challenge-018/jaime/perl5/ch-2.pl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/challenge-018/jaime/perl5/ch-2.pl b/challenge-018/jaime/perl5/ch-2.pl index 10dce92536..d4ddde212e 100644 --- a/challenge-018/jaime/perl5/ch-2.pl +++ b/challenge-018/jaime/perl5/ch-2.pl @@ -14,3 +14,21 @@ # 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}}; + } +} -- cgit From 85119de4e314c3ab930d7369e20222a6e2f61144 Mon Sep 17 00:00:00 2001 From: Jaime <42359730+bracteatus@users.noreply.github.com> Date: Sat, 27 Jul 2019 19:22:19 -0600 Subject: Update README Solutions explained. --- challenge-018/jaime/README | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/challenge-018/jaime/README b/challenge-018/jaime/README index 62a35b5a51..c1fbac90da 100644 --- a/challenge-018/jaime/README +++ b/challenge-018/jaime/README @@ -6,6 +6,13 @@ common substring of the strings “ABABC”, “BABCA” and “ABCBA” is stri “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 Write a script to implement Priority Queue. It is like regular queue @@ -14,11 +21,22 @@ 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. +1. `is_empty`: check whether the queue has no elements. -2. insert_with_priority: add an element to the queue with an +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 +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 + +The solution is a hashmap of priority and anonymous arrays. + +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. -- cgit