aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Westerberg <drclaw@mac.com>2019-07-24 19:38:08 +1000
committerRuben Westerberg <drclaw@mac.com>2019-07-24 19:38:08 +1000
commit308d2c5a19d93acf77a9c65e0751044ed0230ae0 (patch)
treef962f866ee96a1dd785339c5720d989f5974bcfe
parentb2f6e57f6abddcee29c9354b390903cdd633e6fe (diff)
downloadperlweeklychallenge-club-308d2c5a19d93acf77a9c65e0751044ed0230ae0.tar.gz
perlweeklychallenge-club-308d2c5a19d93acf77a9c65e0751044ed0230ae0.tar.bz2
perlweeklychallenge-club-308d2c5a19d93acf77a9c65e0751044ed0230ae0.zip
Added ch-1 and ch-2 p5 and p6 solutions
-rw-r--r--challenge-018/ruben-westerberg/README7
-rwxr-xr-xchallenge-018/ruben-westerberg/perl5/ch-1.pl32
-rwxr-xr-xchallenge-018/ruben-westerberg/perl5/ch-2.pl79
-rwxr-xr-xchallenge-018/ruben-westerberg/perl6/ch-2.p671
4 files changed, 184 insertions, 5 deletions
diff --git a/challenge-018/ruben-westerberg/README b/challenge-018/ruben-westerberg/README
index 14ffe9e086..60f1fb8f9a 100644
--- a/challenge-018/ruben-westerberg/README
+++ b/challenge-018/ruben-westerberg/README
@@ -2,11 +2,8 @@ Solution by Ruben Westerberg
ch-1.pl and ch-1.p6
===
-Run the program to with two arguments to calculate the Ackerman value of them
-Displays the current recusrion count as a progress indicator and displays the
-calculated value and total recursion count as last output.
+Run the program with two arguments to find the longest sub string common to both. alpha numeric characters only.
ch-2.pl and ch-2.p6
===
-Run the program with a command line argument (in single quotes). The argument is a URL to validate.
-With no command line argument, uses builtin test URI
+Run the program to demonstrate a priority queue inserting, listing and pulling data until empty
diff --git a/challenge-018/ruben-westerberg/perl5/ch-1.pl b/challenge-018/ruben-westerberg/perl5/ch-1.pl
new file mode 100755
index 0000000000..2a86d437fb
--- /dev/null
+++ b/challenge-018/ruben-westerberg/perl5/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use List::Util qw<sum>;
+use v5.26;
+
+my $s1=$ARGV[0]//"dfasabcdef";
+my $s2=$ARGV[1]//"dfabcd";
+my @a2=split "",$s2;
+my $len2=@a2;
+my @a1=split "",("_" x scalar @a2-1).$s1.("_"x scalar @a2-1);
+push @a2, split "", "_" x (scalar @a1 -scalar @a2);
+my $longest="";
+my $maxCount=0;
+my @rot=@a2;
+
+for (0..(scalar @a1 - $len2)) {
+ my @tmp=map { ($a1[$_] eq $rot[$_] && $a1[$_] ne "_")?1:0} 0..$#rot;
+ my $sum=sum @tmp;
+ if ($sum > $maxCount) {
+ $maxCount=$sum;
+ $_=(join "", @tmp);
+ while (/(1+)/g) {
+ if (length $& > length $longest) {
+ $longest=substr join("",@a1), $-[0],$+[0]-$-[0];
+ }
+ }
+ }
+ unshift @rot, pop @rot ;
+}
+
+print "Longest common substring: $longest\n";
diff --git a/challenge-018/ruben-westerberg/perl5/ch-2.pl b/challenge-018/ruben-westerberg/perl5/ch-2.pl
new file mode 100755
index 0000000000..154e1d2039
--- /dev/null
+++ b/challenge-018/ruben-westerberg/perl5/ch-2.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use List::Util qw<max min>;
+use v5.26;
+
+#######
+#Priority Queue IMPLEMENTATION
+#######
+
+#Returns true if no elements present in queue
+#Requires a hash reference.
+sub is_empty {
+ my $sum=0;
+ my $h=shift;
+ $sum+=$$h{$_} for (keys %$h);
+ $sum == 0;
+}
+
+#Inserts new element, into queue with priorty
+#Requres hash reference, integer priority, list of elements to add to queue
+sub insert_with_priority {
+ my ($h,$p,@a)=@_;
+ print "Adding to priority: $p -> ", (join ",", @a),"\n";
+ my $sq=$$h{$p};
+ $sq=[] unless defined $sq;
+ push @$sq, $_ for @a;
+ $$h{$p}=$sq;
+}
+
+#Prints out queue contents in decreasing priority
+#Requires a hash reference
+sub list_priorities {
+ my $h=shift;
+ for (sort { $a<=>$b} keys %$h) {
+ print "Prioritory $_:\n";
+ print " ", join(", ", @{$$h{$_}});
+ print "\n";
+ }
+}
+
+#Returns the oldest, highest prioritory element
+#Requres a hash reference
+sub pull_highest_priority_element {
+ my $h=shift; #Queue reference
+ my $p=min keys %$h; #Get highest priority
+ my $sq=$$h{$p}; #Get sub queue
+ my $res=shift @$sq; #Dequeue first (oldest)
+ delete $$h{$p} if (@$sq == 0); #Remove priorit level
+ #when empty
+ $res; #return Result
+}
+
+
+#######
+#Priority Queue DEMONSTRATION
+#######
+#Define our Queue
+my %Q;
+
+#Insert elements with prioritys
+print "Inserting Items \n";
+print "=========================\n";
+insert_with_priority(\%Q,10,"E1", "E2", "E3");
+insert_with_priority(\%Q,1,"E4");
+insert_with_priority(\%Q,10,"E5");
+insert_with_priority(\%Q,2,"E6");
+
+#list contents of queue
+print "\nListing contents of Queue\n";
+print "=========================\n";
+list_priorities(\%Q);
+
+#Drain the queue
+print "\nPulling queue until empty\n";
+print "==========================\n";
+while ( ! is_empty(\%Q) ) {
+ print "Pulling: ",pull_highest_priority_element(\%Q),"\n";
+}
diff --git a/challenge-018/ruben-westerberg/perl6/ch-2.p6 b/challenge-018/ruben-westerberg/perl6/ch-2.p6
new file mode 100755
index 0000000000..026afe3a46
--- /dev/null
+++ b/challenge-018/ruben-westerberg/perl6/ch-2.p6
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl6
+
+#######
+#Priority Queue IMPLEMENTATION
+#######
+
+#Returns true if no elements present in queue
+#Requires a hash.
+sub is_empty(%h) {
+ my $sum=0;
+ $sum+=%h{$_} for (keys %h);
+ $sum == 0;
+}
+
+#Inserts new element, into queue with priorty
+#Requres hash, integer priority, list of elements to add to queue
+sub insert_with_priority(%h,$p,*@a) {
+ put "Adding to priority: $p -> ", @a.join: ",";
+ my $sq=%h{$p};
+ $sq=[] unless defined $sq;
+ push @$sq, $_ for @a;
+ %h{$p}=$sq;
+}
+
+#Prints out queue contents in decreasing priority
+#Requires a hash
+sub list_priorities(%h) {
+ for (sort { $^a <=> $^b}, keys %h) {
+ print "Prioritory $_:\n";
+ print " ", %h{$_}.join(", ");
+ print "\n";
+ }
+}
+
+#Returns the oldest, highest prioritory element
+#Requres a hash
+sub pull_highest_priority_element(%h) {
+ my $p=min (keys %h)>>.Int; #Get highest priority
+ my $sq=%h{$p}; #Get sub queue
+ my $res=shift $sq; #Dequeue first (oldest)
+ %h{$p}:delete if ($sq == 0); #Remove priorit level
+ #when empty
+ $res; #return Result
+}
+
+
+#######
+#Priority Queue DEMONSTRATION
+#######
+#Define our Queue
+my %Q;
+
+#Insert elements with prioritys
+print "Inserting Items \n";
+print "=========================\n";
+insert_with_priority(%Q,10,"E1", "E2", "E3");
+insert_with_priority(%Q,1,"E4");
+insert_with_priority(%Q,10,"E5");
+insert_with_priority(%Q,2,"E6");
+
+#list contents of queue
+print "\nListing contents of Queue\n";
+print "=========================\n";
+list_priorities(%Q);
+
+#Drain the queue
+print "\nPulling queue until empty\n";
+print "==========================\n";
+while ( ! is_empty(%Q) ) {
+ print "Pulling: ",pull_highest_priority_element(%Q),"\n";
+}