From 63811eb3b3df1ca743003bbeccef6e7deab78604 Mon Sep 17 00:00:00 2001 From: Ruben Westerberg Date: Wed, 3 Jul 2019 21:22:22 +1000 Subject: Tidy up --- challenge-015/ruben-westerberg/README | 16 ++++++++++++++-- challenge-015/ruben-westerberg/perl5/ch-2.pl | 11 +++++------ challenge-015/ruben-westerberg/perl6/ch-2.p6 | 14 ++++++++++++++ challenge-015/ruben-westerberg/perl6/ch-2.pl | 14 -------------- 4 files changed, 33 insertions(+), 22 deletions(-) create mode 100755 challenge-015/ruben-westerberg/perl6/ch-2.p6 delete mode 100755 challenge-015/ruben-westerberg/perl6/ch-2.pl diff --git a/challenge-015/ruben-westerberg/README b/challenge-015/ruben-westerberg/README index 7a30900455..e79fa3142b 100644 --- a/challenge-015/ruben-westerberg/README +++ b/challenge-015/ruben-westerberg/README @@ -2,8 +2,20 @@ Solution by Ruben Westerberg ch-1.pl and ch-1.p6 === -Simply run the program to generate the Van Eck’s sequence. If no argument is provided the first 10 items are printed. +Run the script to generate the first 10 strong and 10 weak prime numbers ch-2.pl and ch-2.p6 === -Run the program to find the longest English word possible from US state abbreviations. Uses words_alpha.txt as valid word list. An external word list can be used by providing its path as a single argument. +Encode and decode with Vigenère cipher. Ascii leters (upper and lower case), space, and sample pucctuation characters are supported. +Operation is performed on a line by line basis from standard input and printed to standard output + +To encode: + ch-2.pl KEY + where KEY is the key to encode with. Input data is read from stdin +To decode: + ch-2.pl -d KEY + where KEY is the key used for encoding. Input data is read from stdin + +Loopback test: + ./ch-2.pl "my key" | ./ch-2.pl -d "my key" + diff --git a/challenge-015/ruben-westerberg/perl5/ch-2.pl b/challenge-015/ruben-westerberg/perl5/ch-2.pl index dcb7487b35..9d55175053 100755 --- a/challenge-015/ruben-westerberg/perl5/ch-2.pl +++ b/challenge-015/ruben-westerberg/perl5/ch-2.pl @@ -5,7 +5,7 @@ use Getopt::Long; my $decode; GetOptions("decode"=>\$decode); -die "please specify a key\n" unless $ARGV[0]; +die "Please specify a key\n" unless $ARGV[0]; $|=1; my @alpha=("a".."z","A".."Z", " ", qw); @@ -18,12 +18,11 @@ for my $c (split "", $ARGV[0]) { my $f=$decode? 1 : -1; while () { my (@in, @out); - for my $c (split "") { - push @in, grep { $c eq $alpha[$_]} @a; - } my $i=0; - for my $c (@in) { - push @out, ($c+$f*$k[$i++])%@a; + chomp; + for my $c (split "") { + my $in =(grep { $c eq $alpha[$_]} @a)[0]; + push @out, ($in+$f*$k[$i++])%@a; $i%=@k; } print @alpha[@out],"\n"; diff --git a/challenge-015/ruben-westerberg/perl6/ch-2.p6 b/challenge-015/ruben-westerberg/perl6/ch-2.p6 new file mode 100755 index 0000000000..51a8eb082d --- /dev/null +++ b/challenge-015/ruben-westerberg/perl6/ch-2.p6 @@ -0,0 +1,14 @@ +#!/usr/bin/env perl6 + +sub MAIN ( Str $key, Bool :$decode, Str :$file ) { + my $f=$decode??1!!-1; + $*OUT.out-buffer=0; + my @alpha=("a".."z","A".."Z"," ", )[*;*]; + my @a=@alpha.keys; + my @k=$key.comb.map(-> $c {|@alpha.grep($c,:k)}); + for $*IN.lines -> $line { + my @in= $line.comb.map(->$c {|@alpha.grep($c,:k)}); + my @t= (@in >>+>> (@k >>*>> $f)) >>%>> @a.elems; + put join "", @alpha[@t]; + } +} diff --git a/challenge-015/ruben-westerberg/perl6/ch-2.pl b/challenge-015/ruben-westerberg/perl6/ch-2.pl deleted file mode 100755 index 51a8eb082d..0000000000 --- a/challenge-015/ruben-westerberg/perl6/ch-2.pl +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env perl6 - -sub MAIN ( Str $key, Bool :$decode, Str :$file ) { - my $f=$decode??1!!-1; - $*OUT.out-buffer=0; - my @alpha=("a".."z","A".."Z"," ", )[*;*]; - my @a=@alpha.keys; - my @k=$key.comb.map(-> $c {|@alpha.grep($c,:k)}); - for $*IN.lines -> $line { - my @in= $line.comb.map(->$c {|@alpha.grep($c,:k)}); - my @t= (@in >>+>> (@k >>*>> $f)) >>%>> @a.elems; - put join "", @alpha[@t]; - } -} -- cgit