aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-08-19 11:48:22 +0100
committerGitHub <noreply@github.com>2025-08-19 11:48:22 +0100
commitc072342cf2e865e8de2336d1fa0ab3f7f06e24d4 (patch)
tree58457bf23b7e2441c063719686f098e2dbe7c4cf
parent2c9ff2db5d7fd9d5a6be3bac4f99093a941b2aff (diff)
parentbd7154d608ac663d3c8dccaa3ecb629c2ea9b407 (diff)
downloadperlweeklychallenge-club-c072342cf2e865e8de2336d1fa0ab3f7f06e24d4.tar.gz
perlweeklychallenge-club-c072342cf2e865e8de2336d1fa0ab3f7f06e24d4.tar.bz2
perlweeklychallenge-club-c072342cf2e865e8de2336d1fa0ab3f7f06e24d4.zip
Merge pull request #12536 from mahnkong/challenge-335
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..31ee2ef606
--- /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 if scalar(@words) < 2;
+
+ my %occurences;
+ my @result;
+
+ foreach my $c (split //, $words[0]) {
+ if (! exists($occurences{$c})) {
+ my $max_occurences_all;
+ foreach my $word (@words[1 .. $#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");