aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Mahnke <andreas.mahnke@leuphana.de>2025-08-18 14:13:26 +0200
committerAndreas Mahnke <andreas.mahnke@leuphana.de>2025-08-18 14:13:26 +0200
commit6cb5d85d959b95b0073b3c08c82fa0aa6c1b8f81 (patch)
tree60e954ce93b4e96fe6f6c59f40ca67e0da6ee917
parent4f766edf1327ad3628c824c3c00f1c1f10c50b38 (diff)
downloadperlweeklychallenge-club-6cb5d85d959b95b0073b3c08c82fa0aa6c1b8f81.tar.gz
perlweeklychallenge-club-6cb5d85d959b95b0073b3c08c82fa0aa6c1b8f81.tar.bz2
perlweeklychallenge-club-6cb5d85d959b95b0073b3c08c82fa0aa6c1b8f81.zip
Challenge 335
-rw-r--r--challenge-335/mahnkong/perl/ch-1.pl34
-rw-r--r--challenge-335/mahnkong/perl/ch-2.pl36
2 files changed, 70 insertions, 0 deletions
diff --git a/challenge-335/mahnkong/perl/ch-1.pl b/challenge-335/mahnkong/perl/ch-1.pl
new file mode 100644
index 0000000000..4a7e42509a
--- /dev/null
+++ b/challenge-335/mahnkong/perl/ch-1.pl
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub run(@words) {
+ return undef unless scalar(@words);
+
+ my %occurences;
+ my @result;
+
+ foreach my $c (split //, $words[0]) {
+ if (! exists($occurences{$c})) {
+ my $max_occurences_all;
+ foreach my $word (@words) {
+ my $occurences = () = $word =~ /$c/gi;
+ $max_occurences_all = $occurences if (!defined $max_occurences_all || $max_occurences_all > $occurences);
+ }
+ $occurences{$c} = $max_occurences_all;
+ } else {
+ $occurences{$c} -= 1 if $occurences{$c} > 0;
+ }
+ push @result, $c if $occurences{$c};
+ }
+
+ return [ @result ];
+}
+
+is_deeply(run("bella", "label", "roller"), ['e', 'l', 'l'], "Example 1");
+is_deeply(run("cool", "lock", "cook"), ['c', 'o'], "Example 2");
+is_deeply(run("hello", "world", "pole"), ['l', 'o'], "Example 3");
+is_deeply(run("abc", "def", "ghi"), [], "Example 4");
+is_deeply(run("aab", "aac", "aaa"), ['a', 'a'], "Example 5");
+is_deeply(run("belal", "label", "aroller"), ['e', 'l', 'a', 'l'], "Example 6");
diff --git a/challenge-335/mahnkong/perl/ch-2.pl b/challenge-335/mahnkong/perl/ch-2.pl
new file mode 100644
index 0000000000..632c7894ae
--- /dev/null
+++ b/challenge-335/mahnkong/perl/ch-2.pl
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+use feature 'signatures';
+use Test::More 'no_plan';
+
+sub check_winning(%moves) {
+ foreach my $win (["0:0", "0:1", "0:2"], ["1:0", "1:1", "1:2"], ["2:0", "2:1", "2:2"], ["0:0", "1:0", "2:0"], ["0:1", "1:1", "2:1"] ,["0:2", "1:2", "2:2"], ["0:0", "1:1", "2:2"], ["2:0", "1:1", "0:2"]) {
+ my $count = 0;
+ foreach my $point (@$win) {
+ if (exists($moves{$point})) {
+ $count += 1;
+ }
+ }
+ return 1 if $count == 3;
+ }
+ return 0;
+}
+
+sub run(@moves) {
+ my %a;
+ my %b;
+ for (my $i = 0; $i < scalar(@moves); $i++) {
+ my $c = $i % 2 == 0 ? \%a : \%b;
+ $c->{"$moves[$i]->[0]:$moves[$i]->[1]"} = 1;
+ }
+ return 'A' if check_winning(%a);
+ return 'B' if check_winning(%b);
+ return 'Draw' if scalar(@moves) == 9;
+ return 'Pending';
+}
+
+is(run([0,0],[2,0],[1,1],[2,1],[2,2]), 'A', "Example 1");
+is(run([0,0],[1,1],[0,1],[0,2],[1,0],[2,0]), 'B', "Example 2");
+is(run([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]), 'Draw', "Example 3");
+is(run([0,0],[1,1]), 'Pending', "Example 4");
+is(run([1,1],[0,0],[2,2],[0,1],[1,0],[0,2]), 'B', "Example 5");