diff options
| -rw-r--r-- | challenge-162/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-162/paulo-custodio/perl/ch-1.pl | 18 | ||||
| -rw-r--r-- | challenge-162/paulo-custodio/perl/ch-2.pl | 138 | ||||
| -rw-r--r-- | challenge-162/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-162/paulo-custodio/t/test-2.yaml | 10 |
5 files changed, 178 insertions, 0 deletions
diff --git a/challenge-162/paulo-custodio/Makefile b/challenge-162/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-162/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-162/paulo-custodio/perl/ch-1.pl b/challenge-162/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..84207dc21b --- /dev/null +++ b/challenge-162/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl + +# Challenge 162 +# +# Task 1: ISBN-13 +# Submitted by: Mohammad S Anwar +# Write a script to generate the check digit of given ISBN-13 code. Please +# refer wikipedia for more information. +# +# Example +# ISBN-13 check digit for '978-0-306-40615-7' is 7. + +use Modern::Perl; +use Business::ISBN; + +my $isbn = Business::ISBN->new(shift||""); +$isbn->fix_checksum; +say substr($isbn->as_string, -1, 1); diff --git a/challenge-162/paulo-custodio/perl/ch-2.pl b/challenge-162/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..bb1da50147 --- /dev/null +++ b/challenge-162/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,138 @@ +#!/usr/bin/env perl + +# Challenge 162 +# +# Task 2: Wheatstone-Playfair +# Submitted by: Roger Bell_West +# Implement encryption and decryption using the Wheatstone-Playfair cipher. +# +# Examples: +# (These combine I and J, and use X as padding.) +# +# encrypt("playfair example", "hide the gold in the tree stump") +# = "bmodzbxdnabekudmuixmmouvif" +# +# decrypt("perl and raku", "siderwrdulfipaarkcrw") = "thewexeklychallengex" + +use Modern::Perl; + +{ + package WPcipher; + use Object::Tiny::RW qw( key letters ); + + sub new { + my($class, $key_phrase) = @_; + my $self = bless { key => [], letters => {} }, $class; + $self->make_key($key_phrase); + return $self; + } + + sub make_key { + my($self, $key_phrase) = @_; + + $self->key([[],[],[],[],[]]); + $self->letters({}); + + my $row = 0; + my $col = 0; + for my $c (split(//, lc($key_phrase)), 'a' .. 'z') { + next unless $c =~ /^[a-z]$/; + $c = 'i' if $c eq 'j'; + next if exists $self->letters->{$c}; + + $self->key->[$row][$col] = $c; + $self->letters->{$c} = [$row, $col]; + + $col++; + if ($col >= 5) { + $row++; + $col = 0; + if ($row >= 5) { + last; + } + } + } + } + + sub peek { + my($self, $row, $col) = @_; + return $self->key->[($row+5) % 5][($col+5) % 5]; + } + + sub encrypt_pair { + my($self, $a, $b, $dir) = @_; + + for ($a, $b) { + die $_ unless /^[a-ik-z]$/; + } + die if $a eq $b; + + my($row1, $col1) = @{$self->letters->{$a}}; + my($row2, $col2) = @{$self->letters->{$b}}; + + if ($row1 == $row2) { + return ($self->peek($row1, $col1+$dir), + $self->peek($row2, $col2+$dir)); + } + elsif ($col1 == $col2) { + return ($self->peek($row1+$dir, $col1), + $self->peek($row2+$dir, $col2)); + } + else { + return ($self->peek($row1, $col2), + $self->peek($row2, $col1)); + } + } + + sub encrypt_string { + my($self, $text, $dir) = @_; + $text = lc($text); + my $out = ""; + while ($text ne "") { + $text .= "x" if length($text)==1; + my($a, $b); + if ($text =~ /^(.)\1/) { # repeated first character + $a = substr($text, 0, 1); + $b = "x"; + $text = substr($text, 1); + } + else { + $a = substr($text, 0, 1); + $b = substr($text, 1, 1); + $text = substr($text, 2); + } + my($x, $y) = $self->encrypt_pair($a, $b, $dir); + $out .= $x.$y; + } + return $out; + } + + sub encrypt { + my($self, $text) = @_; + for ($text) { + s/[^a-z]//g; + tr/j/i/; + } + return $self->encrypt_string($text, 1); + } + + sub decrypt { + my($self, $code) = @_; + return $self->encrypt_string($code, -1); + } + +} + +@ARGV==3 or die "Usage: ch-2.pl -e|-d key text\n"; +my($op, $key_phrase, $text) = @ARGV; + +my $wp = WPcipher->new($key_phrase); +if ($op eq "-e") { + say $wp->encrypt($text); +} +elsif ($op eq "-d") { + say $wp->decrypt($text); +} +else { + die "Usage: ch-2.pl -e|-d key text\n"; +} diff --git a/challenge-162/paulo-custodio/t/test-1.yaml b/challenge-162/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..524940b706 --- /dev/null +++ b/challenge-162/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 978-0-306-40615-7 + input: + output: 7 +- setup: + cleanup: + args: 978-0-306-40615-0 + input: + output: 7 diff --git a/challenge-162/paulo-custodio/t/test-2.yaml b/challenge-162/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..6b490155df --- /dev/null +++ b/challenge-162/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: -e "playfair example" "hide the gold in the tree stump" + input: + output: bmodzbxdnabekudmuixmmouvif +- setup: + cleanup: + args: -d "perl and raku" "siderwrdulfipaarkcrw" + input: + output: thewexeklychallengex |
