diff options
| author | Andreas Mahnke <andreas.mahnke@leuphana.de> | 2025-04-24 13:34:49 +0200 |
|---|---|---|
| committer | Andreas Mahnke <andreas.mahnke@leuphana.de> | 2025-04-24 13:34:49 +0200 |
| commit | 47db27f81cefd84e8df74acaa4805665e75b9659 (patch) | |
| tree | 66b8b79344365baa145f1b50a424f72890b00598 | |
| parent | 1a3965d948a3400778249c2c33f063b06473fcd3 (diff) | |
| download | perlweeklychallenge-club-47db27f81cefd84e8df74acaa4805665e75b9659.tar.gz perlweeklychallenge-club-47db27f81cefd84e8df74acaa4805665e75b9659.tar.bz2 perlweeklychallenge-club-47db27f81cefd84e8df74acaa4805665e75b9659.zip | |
Challenge 318
| -rw-r--r-- | challenge-318/mahnkong/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-318/mahnkong/perl/ch-2.pl | 17 |
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"); |
