diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-08-25 02:25:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-25 02:25:51 +0100 |
| commit | f552fab4d3bdb6640b42b756a12efbac1ca8cb33 (patch) | |
| tree | a83e525e74065aa12d45f5c94f3aa85ff04de999 /challenge-022 | |
| parent | 476fa9523316121a46a3da5211402b473eeb4924 (diff) | |
| parent | 7551356237bd76b784b7224f2845afb4ccb9cec7 (diff) | |
| download | perlweeklychallenge-club-f552fab4d3bdb6640b42b756a12efbac1ca8cb33.tar.gz perlweeklychallenge-club-f552fab4d3bdb6640b42b756a12efbac1ca8cb33.tar.bz2 perlweeklychallenge-club-f552fab4d3bdb6640b42b756a12efbac1ca8cb33.zip | |
Merge pull request #547 from drclaw1394/master
ruben/drclaw solutions for W22 ch1 and ch2. p5 and p6
Diffstat (limited to 'challenge-022')
| -rw-r--r-- | challenge-022/ruben-westerberg/README | 8 | ||||
| -rwxr-xr-x | challenge-022/ruben-westerberg/perl5/ch-1.pl | 32 | ||||
| -rwxr-xr-x | challenge-022/ruben-westerberg/perl5/ch-2.pl | 50 | ||||
| -rwxr-xr-x | challenge-022/ruben-westerberg/perl6/ch-1.p6 | 14 | ||||
| -rwxr-xr-x | challenge-022/ruben-westerberg/perl6/ch-2.p6 | 43 |
5 files changed, 141 insertions, 6 deletions
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-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/perl5/ch-2.pl b/challenge-022/ruben-westerberg/perl5/ch-2.pl new file mode 100755 index 0000000000..98895fd8e7 --- /dev/null +++ b/challenge-022/ruben-westerberg/perl5/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw<uniq first>; +use v5.26; + +# implement LZW +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"; + + + +sub encode { + my ($dict,$in, $out)=@_; + @$dict= uniq @$in;# ((0..9),('A'..'Z'),('a'..'z')); + push @$in, undef; + for (@$in) { + state $symbol=""; + my $next=$_;#shift; + $symbol.=$next if defined $next; + state $prev=""; + my $s=first {$symbol eq $_} @$dict; + unless ( $s and $next) { + push @$dict, $symbol; + push @$out, first {$prev eq $$dict[$_]} 0..@$dict-1; + $symbol=substr $symbol, -1; + } + $prev=$symbol; + } +} + + +sub decode { + my ($dict,$in,$out)=@_; + for (@$in) { + push @$out, @$dict[$_]; + } +} 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; +}); + 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[$_]; + } +} |
