diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-04-25 23:46:30 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-25 23:46:30 +0100 |
| commit | 180ce5fe7244ec11b602dee4a3d761c23f899591 (patch) | |
| tree | b140ed7e8566ca0b662579962480e8a5fa05d64b | |
| parent | b40a4f1caea5aeb999d0a30de0db28a7163c9e2f (diff) | |
| parent | 523d1bf351f326f623708368a0658d682c2f963e (diff) | |
| download | perlweeklychallenge-club-180ce5fe7244ec11b602dee4a3d761c23f899591.tar.gz perlweeklychallenge-club-180ce5fe7244ec11b602dee4a3d761c23f899591.tar.bz2 perlweeklychallenge-club-180ce5fe7244ec11b602dee4a3d761c23f899591.zip | |
Merge pull request #6006 from PerlBoy1967/branch-for-challenge-162
Wk 162 - Task 1 & 2
| -rwxr-xr-x | challenge-162/perlboy1967/perl/ch-1.pl | 25 | ||||
| -rwxr-xr-x | challenge-162/perlboy1967/perl/ch-2.pl | 101 |
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-162/perlboy1967/perl/ch-1.pl b/challenge-162/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..6d55acdfc3 --- /dev/null +++ b/challenge-162/perlboy1967/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 162 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-162/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: ISBN-13 +Submitted by: Mohammad S Anwar + +Write a script to generate the check digit of given ISBN-13 code. Please refer to +wikipedia for more information. + +=cut + +use v5.16; + +use List::Util qw(sum0); +use List::MoreUtils qw(pairwise); + +my @isbnDigits = (grep/\d/,split//,shift // '978-0-306-40615-7')[0..11]; +my @factors = split//,1313131313131; +printf "%d\n", (10 - (sum0 pairwise {$a*$b} @isbnDigits,@factors) % 10) % 10; diff --git a/challenge-162/perlboy1967/perl/ch-2.pl b/challenge-162/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..9f96f473d8 --- /dev/null +++ b/challenge-162/perlboy1967/perl/ch-2.pl @@ -0,0 +1,101 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 162 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-162/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Wheatstone-Playfair +Submitted by: Roger Bell_West + +Implement encryption and decryption using the Wheatstone-Playfair cipher. + +=cut + +use v5.16; + +use constant WP_ENCRYPT => 1; +use constant WP_DECRYPT => -1; + +use Test::More; + +is(encrypt('playfair example', 'hide the gold in the tree stump'), 'bmodzbxdnabekudmuixmmouvif'); +is(decrypt('perl and raku', 'siderwrdulfipaarkcrw'), 'thewexeklychallengex'); + +done_testing(); + + +sub encrypt ($$) { + _wpCrypt($_[0],$_[1],WP_ENCRYPT); +} + + +sub decrypt ($$) { + _wpCrypt($_[0],$_[1],WP_DECRYPT); +} + + +sub _wpCrypt ($$$) { + my ($key,$text,$mode) = (lc $_[0], lc $_[1], $_[2]); + + my ($hrC2L,$arL2C,@return); + + # Remove spaces from key + $key =~ s/\s//g; + + my @keyChars = split//,$key; + my %chars = map { $_ => 1 } ('a' .. 'z'); + my %usedChars; + + # Build encryption matrix + for my $row (0..4) { + for my $col (0..4) { + my $char; + do { + $char = (scalar @keyChars ? shift @keyChars : (sort keys %chars)[0]); + delete $chars{$char}; + delete $chars{j} if $char eq 'i'; + delete $chars{i} if $char eq 'j'; + } while (exists $usedChars{$char}); + $hrC2L->{$char} = [$row,$col]; + $arL2C->[$row][$col] = $char; + $usedChars{$char} = 1; + $usedChars{j} = 1 if $char eq 'i'; + $usedChars{i} = 1 if $char eq 'j'; + } + } + + # Remove spaces from text + $text =~ s/\s//g; + + # On ecryption, insert 'x' before repeating characters + $text =~ s/(.)(?=\1)/$1x/g + if $mode == WP_ENCRYPT; + + # Process per 2 chars of plaintext + while ($text =~ s#^(.)(.)?##c) { + my ($char1,$char2) = ($1, $2 // 'x'); + + my ($c1,$c2) = ($hrC2L->{$char1},$hrC2L->{$char2}); + + # Coords form rectangle? + if ($c1->[0] != $c2->[0] and $c1->[1] != $c2->[1]) { + push(@return,$arL2C->[$c1->[0]][$c2->[1]]); + push(@return,$arL2C->[$c2->[0]][$c1->[1]]); + } elsif ($c1->[0] == $c2->[0]) { + # row + push(@return,$arL2C->[$c1->[0]][($c1->[1]+$mode)%5]); + push(@return,$arL2C->[$c2->[0]][($c2->[1]+$mode)%5]); + } else { + # column + push(@return,$arL2C->[($c1->[0]+$mode)%5][$c1->[1]]); + push(@return,$arL2C->[($c2->[0]+$mode)%5][$c2->[1]]); + } + } + + return join('',@return); +} + + |
