aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-025/paulo-custodio/README1
-rw-r--r--challenge-025/paulo-custodio/perl/ch-1.pl53
-rw-r--r--challenge-025/paulo-custodio/perl/ch-2.pl77
-rw-r--r--challenge-025/paulo-custodio/test.pl24
4 files changed, 155 insertions, 0 deletions
diff --git a/challenge-025/paulo-custodio/README b/challenge-025/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-025/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-025/paulo-custodio/perl/ch-1.pl b/challenge-025/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..3a03067850
--- /dev/null
+++ b/challenge-025/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+
+# Challenge 025
+#
+# Generate a longest sequence of the following “English Pokemon” names where
+# each name starts with the last letter of previous name.
+
+use strict;
+use warnings;
+use 5.030;
+
+my @names = qw( audino bagon baltoy banette bidoof braviary bronzor carracosta
+ charmeleon cresselia croagunk darmanitan deino emboar emolga
+ exeggcute gabite girafarig gulpin haxorus heatmor heatran
+ ivysaur jellicent jumpluff kangaskhan kricketune landorus
+ ledyba loudred lumineon lunatone machamp magnezone mamoswine
+ nosepass petilil pidgeotto pikachu pinsir poliwrath poochyena
+ porygon2 porygonz registeel relicanth remoraid rufflet sableye
+ scolipede scrafty seaking sealeo silcoon simisear snivy snorlax
+ spoink starly tirtouga trapinch treecko tyrogue vigoroth
+ vulpix wailord wartortle whismur wingull yamask
+ );
+
+say join(" ", find_longest_sequence(@names));
+
+sub find_longest_sequence1 {
+ my($longest_path, $current_path, $seen) = @_;
+
+ # words that can still be used
+ my @pending = grep {!$seen->{$_} &&
+ (@$current_path == 0 ||
+ substr($current_path->[-1], -1, 1) eq substr($_, 0, 1))
+ } @names;
+ if (@pending == 0) { # end of search
+ if (@$current_path > @$longest_path) {
+ @$longest_path = @$current_path;
+ }
+ }
+ else { # find each possible path
+ for my $word (@pending) {
+ find_longest_sequence1($longest_path,
+ [ @$current_path, $word ],
+ { %$seen, $word => 1 } );
+ }
+ }
+}
+
+sub find_longest_sequence {
+ my(@words) = @_;
+ my @longest_path;
+ find_longest_sequence1(\@longest_path, [], {});
+ return @longest_path;
+}
diff --git a/challenge-025/paulo-custodio/perl/ch-2.pl b/challenge-025/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..8e5702b732
--- /dev/null
+++ b/challenge-025/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,77 @@
+#!/usr/bin/env perl
+
+# Challenge 025
+#
+# Task #2
+# Create script to implement Chaocipher. Please checkout wiki page for more
+# information.
+
+use strict;
+use warnings;
+use 5.030;
+
+my $left = "HXUCZVAMDSLKPEFJRIGTWOBNYQ";
+my $right = "PTLNBQDEOYSFAVZKGJRIHWXUMC";
+
+sub permute_alphabets {
+ my($pos) = @_;
+
+ # permute left alphabet
+ $left = substr($left, $pos).substr($left, 0, $pos);
+ $left = substr($left, 0, 1).substr($left, 2, 12)
+ .substr($left, 1, 1).substr($left, 14);
+
+ # permute right alphabet
+ $right = substr($right, $pos).substr($right, 0, $pos);
+ $right = substr($right, 1, 25).substr($right, 0, 1);
+ $right = substr($right, 0, 2).substr($right, 3, 11)
+ .substr($right, 2, 1).substr($right, 14);
+}
+
+sub find_pos {
+ my($letter, $alphabet) = @_;
+ $alphabet =~ /$letter/g;
+ my $pos = pos($alphabet)-1;
+ return $pos;
+}
+
+sub encode {
+ my($text) = @_;
+ my $encoded = "";
+ while ($text ne '') {
+ (my $letter, $text) = $text =~ /(.)(.*)/;
+ my $pos = find_pos($letter, $right);
+ my $code = substr($left, $pos, 1);
+ $encoded .= $code;
+
+ permute_alphabets($pos);
+ }
+ return $encoded;
+}
+
+sub decode {
+ my($encoded) = @_;
+ my $text = "";
+ while ($encoded ne '') {
+ (my $code, $encoded) = $encoded =~ /(.)(.*)/;
+ my $pos = find_pos($code, $left);
+ my $letter = substr($right, $pos, 1);
+ $text .= $letter;
+
+ permute_alphabets($pos);
+ }
+ return $text;
+}
+
+
+# main
+my($op, $arg) = @ARGV;
+if (@ARGV==2 && $op =~ /^enc/i) {
+ say encode($arg);
+}
+elsif (@ARGV==2 && $op =~ /^dec/i) {
+ say decode($arg);
+}
+else {
+ say "Usage: ch-2.pl encode|decode text\n";
+}
diff --git a/challenge-025/paulo-custodio/test.pl b/challenge-025/paulo-custodio/test.pl
new file mode 100644
index 0000000000..771a7059b4
--- /dev/null
+++ b/challenge-025/paulo-custodio/test.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+use 5.030;
+
+is capture("perl perl/ch-1.pl"), <<END;
+machamp petilil landorus scrafty yamask kricketune emboar registeel loudred darmanitan nosepass simisear relicanth heatmor rufflet trapinch haxorus seaking girafarig gabite exeggcute emolga audino
+END
+
+my($text, $encoded) = qw( WELLDONEISBETTERTHANWELLSAID
+ OAHQHCNYNXTSZJRRHJBYHQKSOUJY );
+is capture("perl perl/ch-2.pl encode $text"), "$encoded\n";
+is capture("perl perl/ch-2.pl decode $encoded"), "$text\n";
+
+done_testing;
+
+sub capture {
+ my($cmd) = @_;
+ my $out = `$cmd`;
+ $out =~ s/[ \t\v\f\r]*\n/\n/g;
+ return $out;
+}