aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCY Fung <fungcheokyin@gmail.com>2023-09-17 15:31:52 +0800
committerCY Fung <fungcheokyin@gmail.com>2023-09-17 15:31:52 +0800
commitb206ebfcbb7870b0247bd33f8b3e20f25e4e921e (patch)
tree16e8ade444a6e18c30575ef052c2b3f80f42003f
parent12ee0b590fc517529422b2f9403cdd318cc3e5f2 (diff)
downloadperlweeklychallenge-club-b206ebfcbb7870b0247bd33f8b3e20f25e4e921e.tar.gz
perlweeklychallenge-club-b206ebfcbb7870b0247bd33f8b3e20f25e4e921e.tar.bz2
perlweeklychallenge-club-b206ebfcbb7870b0247bd33f8b3e20f25e4e921e.zip
Week 234
-rw-r--r--challenge-234/cheok-yin-fung/perl/ch-1.pl33
-rw-r--r--challenge-234/cheok-yin-fung/perl/ch-2.pl28
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-234/cheok-yin-fung/perl/ch-1.pl b/challenge-234/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..e28b8047d1
--- /dev/null
+++ b/challenge-234/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,33 @@
+# The Weekly Challenge 234
+# Task 1 Common Characters
+
+use v5.30.0;
+use warnings;
+
+sub cc {
+ my @words = @_;
+
+# my @s_words = map {join "", sort {$a cmp $b} split "", $_} @words;
+# #Then may use longest common subsequence algorithm.
+
+ my %occurence;
+ for my $alphabet ('a'..'z', 'A'..'Z' ) {
+ $occurence{$alphabet} = grep {$alphabet eq $_} split "", $words[0];
+ for my $word (@words[1..$#words]) {
+ my $temp = grep {$alphabet eq $_} split "", $word;
+ if ($occurence{$alphabet} > $temp) {
+ $occurence{$alphabet} = $temp;
+ }
+ }
+ }
+ my $final_str = "";
+ for my $alphabet ('a'..'z', 'A'..'Z' ) {
+ $final_str .= $alphabet x $occurence{$alphabet};
+ }
+ return [split "", $final_str];
+}
+
+say join " ", cc("java", "javascript", "julia")->@*; # a, j
+say join " ", cc("bella", "label", "roller")->@*; # e, l, l
+say join " ", cc("cool", "lock", "cook")->@*; # c, o
+
diff --git a/challenge-234/cheok-yin-fung/perl/ch-2.pl b/challenge-234/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..557905515f
--- /dev/null
+++ b/challenge-234/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,28 @@
+# The Weekly Challenge 234
+# Task 2 Unequal Triplets
+
+use v5.30.0;
+use warnings;
+use List::MoreUtils qw/frequency/;
+
+sub ut {
+ my @ints = sort {$a <=> $b} @_;
+ my $ans = 0;
+ my %freq = frequency @ints;
+ my @f = values %freq;
+ return 0 if scalar @f <= 2;
+ for my $i (0..$#f) {
+ for my $j ($i+1..$#f) {
+ for my $k ($j+1..$#f) {
+ $ans += $f[$i]*$f[$j]*$f[$k];
+ }
+ }
+ }
+ return $ans;
+}
+
+use Test::More tests => 4;
+ok ut(4, 4, 2, 4, 3) == 3;
+ok ut(1, 1, 1, 1, 1) == 0;
+ok ut(4, 7, 1, 10, 7, 4, 1, 1) == 28;
+ok ut(1, 1, 1, 1, 2) == 0;