diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-04-26 06:45:27 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-04-26 06:45:27 +0100 |
| commit | a957e8a7197bf14c7a82f6bf66fd684a70ca2f4d (patch) | |
| tree | 8e9f461e698a11b9db09416fe1ed79365bef9977 | |
| parent | 06e23df22ddf273de0e4f801f903a81a509262d5 (diff) | |
| download | perlweeklychallenge-club-a957e8a7197bf14c7a82f6bf66fd684a70ca2f4d.tar.gz perlweeklychallenge-club-a957e8a7197bf14c7a82f6bf66fd684a70ca2f4d.tar.bz2 perlweeklychallenge-club-a957e8a7197bf14c7a82f6bf66fd684a70ca2f4d.zip | |
added some extra examples and stopping doing the x stuff when decrypting
| -rw-r--r-- | challenge-162/james-smith/perl/ch-2.pl | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/challenge-162/james-smith/perl/ch-2.pl b/challenge-162/james-smith/perl/ch-2.pl index d4e3edfb20..1cc3f6cb36 100644 --- a/challenge-162/james-smith/perl/ch-2.pl +++ b/challenge-162/james-smith/perl/ch-2.pl @@ -10,7 +10,8 @@ use Data::Dumper qw(Dumper); is( encrypt('playfair example', 'hide the gold in the tree stump'), 'bmodzbxdnabekudmuixmmouvif' ); is( decrypt('perl and raku', 'siderwrdulfipaarkcrw'), 'thewexeklychallengex' ); - +is( encrypt('abc','xxx'), 'yyyyyy' ); +is( decrypt('abc','yyyyyy'), 'xxxxxx' ); done_testing(); sub encrypt { return _crypt( 1,@_); } @@ -22,10 +23,11 @@ sub _crypt { for grep { /[a-z]/ } split(//,$key),'a'..'i','k'..'z'; $r[$l{$_}[0]][$l{$_}[1]]=$_ for keys %l; ## @r maps position to letter - my @seq = grep {/[a-z]/} split //, shift =~ s{j}{i}gr; ## Prep sequence + my @seq = grep {/[a-z]/} split //, lc shift =~ s{j}{i}gr; ## Prep sequence while(my($m,$n)=splice @seq,0,2) { ## Loop through letter pairs - unshift(@seq,$n), $n='x' if $n && $n eq $m && $n ne 'x'; ## Deal with case when both letters the same + unshift(@seq,$n), $n='x' if $n && $n eq $m && $o == 1; ## Deal with case when both letters the same + ## Only do when encrypting.. $n ||= 'x'; ## Pad if required... $out.= $l{$m}[0] eq $l{$n}[0] ? $r[ $l{$m}[0] ][($l{$m}[1]+$o)%5]. $r[ $l{$n}[0] ][($l{$n}[1]+$o)%5] |
