aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-097/james-smith/perl/ch-1.pl17
-rw-r--r--challenge-097/james-smith/perl/ch-2.pl29
2 files changed, 46 insertions, 0 deletions
diff --git a/challenge-097/james-smith/perl/ch-1.pl b/challenge-097/james-smith/perl/ch-1.pl
new file mode 100644
index 0000000000..d6dfd58a56
--- /dev/null
+++ b/challenge-097/james-smith/perl/ch-1.pl
@@ -0,0 +1,17 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+
+is( caesar('ABCDEFGHIJKLMNOPQRSTUVWXYZ',3), 'XYZABCDEFGHIJKLMNOPQRSTUVW' );
+is( caesar('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG',3), 'QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD' );
+
+done_testing();
+
+sub caesar {
+ return join q(), map { m{[A-Z]} ? chr 65+(ord($_)-65-$_[1])%26 : $_ } split m{}, $_[0];
+}
+
diff --git a/challenge-097/james-smith/perl/ch-2.pl b/challenge-097/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..26bdfe22a1
--- /dev/null
+++ b/challenge-097/james-smith/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/usr/local/bin/perl
+
+use strict;
+
+use warnings;
+use feature qw(say);
+use Test::More;
+
+is( min_flips('101100101',3), 1 );
+is( min_flips('10110111', 4), 2 );
+is( min_flips('101100100',4), 1 );
+is( min_flips('0000000100100011010001010110011110001001101010111100110111101111',4), 32 );
+
+done_testing();
+
+sub min_flips {
+ my( $str, $n, @parts ) = @_;;
+ my $min = length $str;
+ push @parts, substr $str,0,$n,'' while length $str;
+ foreach my $r (@parts) {
+ my $t = 0;
+ foreach my $s (@parts) {
+ $t += ($s^$r) =~ tr/\x01/\x01/;
+ }
+ $min = $t if $t < $min;
+ }
+ return $min;
+}
+