aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-318/mahnkong/perl/ch-1.pl24
-rw-r--r--challenge-318/mahnkong/perl/ch-2.pl17
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-318/mahnkong/perl/ch-1.pl b/challenge-318/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..5b717851ad
--- /dev/null
+++ b/challenge-318/mahnkong/perl/ch-1.pl
@@ -0,0 +1,24 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run($string) {
+ my $result = [];
+ my $last = undef;
+ my @string = split //, $string;
+ my @parts;
+
+ for (my $i = 0; $i < scalar(@string); $i++) {
+ if (! defined $last || $last ne $string[$i]) {
+ push @parts, '';
+ }
+ $last = $string[$i];
+ $parts[-1] .= $last;
+ }
+ return [ grep { length($_) > 2} @parts ];
+}
+
+is_deeply(run("abccccd"), ["cccc"], "Example 1");
+is_deeply(run("aaabcddddeefff"), ["aaa", "dddd", "fff"], "Example 2");
+is_deeply(run("abcdd"), [], "Example 3");
diff --git a/challenge-318/mahnkong/perl/ch-2.pl b/challenge-318/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..a6f56ab803
--- /dev/null
+++ b/challenge-318/mahnkong/perl/ch-2.pl
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run($source, $target) {
+ return 0 if $#$source != $#$target;
+ my $diff = 0;
+ for (my $i = 0; $i < scalar(@$source); $i++) {
+ $diff += 1 if $source->[$i] != $target->[$i];
+ }
+ return $diff <= 2 ? 1 : 0;
+}
+
+is(run([3, 2, 1, 4], [1, 2, 3, 4]), 1, "Example 1");
+is(run([1, 3, 4], [4, 1, 3]), 0, "Example 2");
+is(run([2], [2]), 1, "Example 3");