From 92c9dc707443f1d46ec3a84860c280cf190be249 Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Thu, 22 Aug 2019 20:40:53 +1000 Subject: ch-1 solutions p5 and p6 --- challenge-022/ruben-westerberg/perl5/ch-1.pl | 32 ++++++++++++++++++++++++++++ challenge-022/ruben-westerberg/perl6/ch-1.p6 | 14 ++++++++++++ 2 files changed, 46 insertions(+) create mode 100755 challenge-022/ruben-westerberg/perl5/ch-1.pl create mode 100755 challenge-022/ruben-westerberg/perl6/ch-1.p6 diff --git a/challenge-022/ruben-westerberg/perl5/ch-1.pl b/challenge-022/ruben-westerberg/perl5/ch-1.pl new file mode 100755 index 0000000000..8c54633ad7 --- /dev/null +++ b/challenge-022/ruben-westerberg/perl5/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use v5.26; + +#Find the first 10 sexy primes + +my $i=1; +my $count=10; +my @primes; +while ($count) { + if (isPrime($i)) { + @primes=grep { $i-$_ <=6} @primes; + my @match=grep {$i-6 == $_} @primes; + if (@match) { + printf "Sexy Pair: %d,%d\n", $match[0], $i; + $count--; + } + push @primes, $i; + } + $i++; +} + +sub isPrime { + my $t=shift; + my $sum=0; + for (1..(int $t/2)) { + $sum++ if $t % $_ == 0; + last if $sum >1; + } + $sum==1; +} diff --git a/challenge-022/ruben-westerberg/perl6/ch-1.p6 b/challenge-022/ruben-westerberg/perl6/ch-1.p6 new file mode 100755 index 0000000000..66552158d6 --- /dev/null +++ b/challenge-022/ruben-westerberg/perl6/ch-1.p6 @@ -0,0 +1,14 @@ +#!/usr/bin/env perl6 + +((1..*).grep: *.is-prime).map(->$p { + state @search; + state $count =10; + @search=@search.grep({$p-$_ <=6}); + my @match=@search.grep({$p-6 == $_}); + if @match { + put "Sexy Pair: @match[0],$p"; + last unless --$count; + } + @search.push: $p; +}); + -- cgit From 84014b58431f94d05607621f5be85454799a6f7d Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Sat, 24 Aug 2019 21:29:19 +1000 Subject: Basic working version for ch-2.p5 --- challenge-022/ruben-westerberg/perl5/ch-2.pl | 58 ++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100755 challenge-022/ruben-westerberg/perl5/ch-2.pl diff --git a/challenge-022/ruben-westerberg/perl5/ch-2.pl b/challenge-022/ruben-westerberg/perl5/ch-2.pl new file mode 100755 index 0000000000..aa504468d6 --- /dev/null +++ b/challenge-022/ruben-westerberg/perl5/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use v5.26; + +# implement LZW +my @symbols= 'A'..'Z'; +my @indexes; +for (split "","TOBEORNOTTOBEORTOBEORNOT") { + my $res=encode(\@symbols, $_); + if ($res) { + push @indexes, $res + } +} +my $res=encode(\@symbols, $_); +if ($res) { + push @indexes, $res +} +print join ",",@indexes; + +for (@indexes) { + print decode(\@symbols,$_),"\n"; +} +sub encode { + state $symbol=""; + my $dict=shift; + state $count= @$dict; + my $next=shift; + $symbol.=$next if defined $next; + print "Symbol: ",$symbol,"\n"; + state $prev=""; + print "Prev: ",$prev,"\n"; + my $s=(grep {$symbol eq $_} @$dict)[0]; + print "$s\n"; + print "$symbol\n"; + my $res; + if (defined $s and $next) { + #found existing keep adding + $res=undef; + } + else { + #add new symbol + print "New symbol $symbol\n"; + #$$dict{$symbol}=$count++; + push @$dict, $symbol; + $res=(grep {$prev eq $$dict[$_]} 0..@$dict-1)[0]; + $symbol=substr $symbol, -1; + } + $prev=$symbol; + $res; +} + + +sub decode { + my $dict=shift; + my $code=shift; + @$dict[$code]; +} -- cgit From 478d87cdf94e902f1fc01d4e42d0767a635940a6 Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Sat, 24 Aug 2019 22:10:04 +1000 Subject: A more tidy solution to ch-2.pl --- challenge-022/ruben-westerberg/perl5/ch-2.pl | 87 ++++++++++++++-------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/challenge-022/ruben-westerberg/perl5/ch-2.pl b/challenge-022/ruben-westerberg/perl5/ch-2.pl index aa504468d6..694883c4fe 100755 --- a/challenge-022/ruben-westerberg/perl5/ch-2.pl +++ b/challenge-022/ruben-westerberg/perl5/ch-2.pl @@ -4,55 +4,54 @@ use warnings; use v5.26; # implement LZW -my @symbols= 'A'..'Z'; -my @indexes; -for (split "","TOBEORNOTTOBEORTOBEORNOT") { - my $res=encode(\@symbols, $_); - if ($res) { - push @indexes, $res - } -} -my $res=encode(\@symbols, $_); -if ($res) { - push @indexes, $res -} -print join ",",@indexes; +my @input=split "", $ARGV[0]//"TOBEORNOTTOBEORTOBEORNOT"; +my @decoded; +my @encoded; +my @symbols; + +print "Input: ",join("",@input),"\n"; +encode(\@symbols, \@input ,\@encoded); + +print "Encoded: ",join(",",@encoded),"\n"; + +decode(\@symbols,\@encoded,\@decoded); + +print "Decoded: ",join("",@decoded),"\n"; + + -for (@indexes) { - print decode(\@symbols,$_),"\n"; -} sub encode { - state $symbol=""; - my $dict=shift; - state $count= @$dict; - my $next=shift; - $symbol.=$next if defined $next; - print "Symbol: ",$symbol,"\n"; - state $prev=""; - print "Prev: ",$prev,"\n"; - my $s=(grep {$symbol eq $_} @$dict)[0]; - print "$s\n"; - print "$symbol\n"; - my $res; - if (defined $s and $next) { - #found existing keep adding - $res=undef; - } - else { - #add new symbol - print "New symbol $symbol\n"; - #$$dict{$symbol}=$count++; - push @$dict, $symbol; - $res=(grep {$prev eq $$dict[$_]} 0..@$dict-1)[0]; - $symbol=substr $symbol, -1; + my ($dict,$in, $out)=@_; + @$dict= ((0..9),('A'..'Z'),('a'..'z')); + push @$in, undef; + for (@$in) { + state $symbol=""; + state $count= @$dict; + my $next=$_;#shift; + $symbol.=$next if defined $next; + state $prev=""; + my $s=(grep {$symbol eq $_} @$dict)[0]; + my $res; + if (defined $s and $next) { + #found existing keep adding + $res=undef; + } + else { + #add new symbol + #$$dict{$symbol}=$count++; + push @$dict, $symbol; + $res=(grep {$prev eq $$dict[$_]} 0..@$dict-1)[0]; + push @$out, $res; + $symbol=substr $symbol, -1; + } + $prev=$symbol; } - $prev=$symbol; - $res; } sub decode { - my $dict=shift; - my $code=shift; - @$dict[$code]; + my ($dict,$in,$out)=@_; + for (@$in) { + push @$out, @$dict[$_]; + } } -- cgit From 6f969d71a5a6c9df64046eb0a3ef659346b51776 Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Sun, 25 Aug 2019 07:47:20 +1000 Subject: ch-2.pl tidy up --- challenge-022/ruben-westerberg/perl5/ch-2.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/challenge-022/ruben-westerberg/perl5/ch-2.pl b/challenge-022/ruben-westerberg/perl5/ch-2.pl index 694883c4fe..f0f4051395 100755 --- a/challenge-022/ruben-westerberg/perl5/ch-2.pl +++ b/challenge-022/ruben-westerberg/perl5/ch-2.pl @@ -10,6 +10,7 @@ my @encoded; my @symbols; print "Input: ",join("",@input),"\n"; + encode(\@symbols, \@input ,\@encoded); print "Encoded: ",join(",",@encoded),"\n"; -- cgit From 7551356237bd76b784b7224f2845afb4ccb9cec7 Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Sun, 25 Aug 2019 08:46:12 +1000 Subject: Added ch-2.p6 Refactored ch-2.pl to use List::Util Removed superfluous conditional blocks --- challenge-022/ruben-westerberg/README | 8 ++---- challenge-022/ruben-westerberg/perl5/ch-2.pl | 18 ++++-------- challenge-022/ruben-westerberg/perl6/ch-2.p6 | 43 ++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 19 deletions(-) create mode 100755 challenge-022/ruben-westerberg/perl6/ch-2.p6 diff --git a/challenge-022/ruben-westerberg/README b/challenge-022/ruben-westerberg/README index 1f319161be..26a2690833 100644 --- a/challenge-022/ruben-westerberg/README +++ b/challenge-022/ruben-westerberg/README @@ -2,13 +2,9 @@ Solution by Ruben Westerberg ch-1.pl and ch-1.p6 === -Run with a single argument. Argument is the number of terms to add in calculating Eulers number. If no argument is given, a default of 10 terms are used. +Run the program to calculate the first ten sexy prime pairs ch-2.pl and ch-2.p6 === -Run with a single argument. Argument is a URI which is normalized (no semantic changes). If no argument is given a test URI is used. -- Normalizes scheme to lower case -- Normalize % codes to upper case -- decode unreserved % codes if present -- encode any characters outside of reserved or unreserved codes. +Run the program with a command line argument to demonstrate the LZW encoding. With no argument, a demonstration string is used diff --git a/challenge-022/ruben-westerberg/perl5/ch-2.pl b/challenge-022/ruben-westerberg/perl5/ch-2.pl index f0f4051395..98895fd8e7 100755 --- a/challenge-022/ruben-westerberg/perl5/ch-2.pl +++ b/challenge-022/ruben-westerberg/perl5/ch-2.pl @@ -1,6 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; +use List::Util qw; use v5.26; # implement LZW @@ -23,26 +24,17 @@ print "Decoded: ",join("",@decoded),"\n"; sub encode { my ($dict,$in, $out)=@_; - @$dict= ((0..9),('A'..'Z'),('a'..'z')); + @$dict= uniq @$in;# ((0..9),('A'..'Z'),('a'..'z')); push @$in, undef; for (@$in) { state $symbol=""; - state $count= @$dict; my $next=$_;#shift; $symbol.=$next if defined $next; state $prev=""; - my $s=(grep {$symbol eq $_} @$dict)[0]; - my $res; - if (defined $s and $next) { - #found existing keep adding - $res=undef; - } - else { - #add new symbol - #$$dict{$symbol}=$count++; + my $s=first {$symbol eq $_} @$dict; + unless ( $s and $next) { push @$dict, $symbol; - $res=(grep {$prev eq $$dict[$_]} 0..@$dict-1)[0]; - push @$out, $res; + push @$out, first {$prev eq $$dict[$_]} 0..@$dict-1; $symbol=substr $symbol, -1; } $prev=$symbol; diff --git a/challenge-022/ruben-westerberg/perl6/ch-2.p6 b/challenge-022/ruben-westerberg/perl6/ch-2.p6 new file mode 100755 index 0000000000..23c5e67daa --- /dev/null +++ b/challenge-022/ruben-westerberg/perl6/ch-2.p6 @@ -0,0 +1,43 @@ +#!/usr/bin/env perl6 + +# implement LZW +my @input=comb "", @*ARGS[0]//"TOBEORNOTTOBEORTOBEORNOT"; +my @decoded; +my @encoded; +my @symbols; + +print "Input: ",join("",@input),"\n"; + +encode(@symbols, @input ,@encoded); + +print "Encoded: ",join(",",@encoded),"\n"; + +decode(@symbols,@encoded,@decoded); + +print "Decoded: ",join("",@decoded),"\n"; + + +sub encode (@dict, @in, @out) { + @dict= @in.unique; #Create initial dictionary + push @in, Any; #Add end marker to input + for @in { + state $symbol=""; + my $next=$_; + $symbol~=$next if $next.defined; + state $prev=""; + my $s=first {$symbol eq $_}, @dict; #Search for existing + unless $s.defined and $next.defined { + push @dict, $symbol; + push @out, @dict.first: {$prev eq $_ },:k; + $symbol=substr $symbol, *-1; + } + $prev=$symbol; + } +} + + +sub decode(@dict, @in, @out) { + for @in { + push @out, @dict[$_]; + } +} -- cgit