aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Westerberg <drclaw@mac.com>2019-08-25 08:46:12 +1000
committerRuben Westerberg <drclaw@mac.com>2019-08-25 08:46:12 +1000
commit7551356237bd76b784b7224f2845afb4ccb9cec7 (patch)
treed4001a1a7daf908a1ce320a48b91d737ac4dfbb8
parent6f969d71a5a6c9df64046eb0a3ef659346b51776 (diff)
downloadperlweeklychallenge-club-7551356237bd76b784b7224f2845afb4ccb9cec7.tar.gz
perlweeklychallenge-club-7551356237bd76b784b7224f2845afb4ccb9cec7.tar.bz2
perlweeklychallenge-club-7551356237bd76b784b7224f2845afb4ccb9cec7.zip
Added ch-2.p6
Refactored ch-2.pl to use List::Util Removed superfluous conditional blocks
-rw-r--r--challenge-022/ruben-westerberg/README8
-rwxr-xr-xchallenge-022/ruben-westerberg/perl5/ch-2.pl18
-rwxr-xr-xchallenge-022/ruben-westerberg/perl6/ch-2.p643
3 files changed, 50 insertions, 19 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-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<uniq first>;
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[$_];
+ }
+}