aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-11 21:45:33 +0000
committerGitHub <noreply@github.com>2024-02-11 21:45:33 +0000
commitd3c4da7ff01935c750ecc2567051cc01e494626e (patch)
treed6eb9d6b3b3ec2b3f0f6c08eb88b288966c65299
parentc54cb0ecda94c0d2bee0207dda431392fb6cebe6 (diff)
parent8f2017a73332b0c2bc7cc9ca78d6287e9627ebe9 (diff)
downloadperlweeklychallenge-club-d3c4da7ff01935c750ecc2567051cc01e494626e.tar.gz
perlweeklychallenge-club-d3c4da7ff01935c750ecc2567051cc01e494626e.tar.bz2
perlweeklychallenge-club-d3c4da7ff01935c750ecc2567051cc01e494626e.zip
Merge pull request #9559 from E7-87-83/newt
mainly Week 255
-rw-r--r--challenge-254/cheok-yin-fung/perl/ch-2.pl35
-rw-r--r--challenge-255/cheok-yin-fung/perl/ch-1.pl23
-rw-r--r--challenge-255/cheok-yin-fung/perl/ch-2.pl39
3 files changed, 97 insertions, 0 deletions
diff --git a/challenge-254/cheok-yin-fung/perl/ch-2.pl b/challenge-254/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..307101ff7b
--- /dev/null
+++ b/challenge-254/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,35 @@
+# The Weekly Challenge 254
+# Task 2 Reverse Vowels
+use v5.30.0;
+use warnings;
+
+sub r_v {
+ my $s = $_[0];
+ my $c_s = $s;
+ $c_s =~ tr/aeiou/=/;
+ $c_s =~ tr/AEIOU/_/;
+ my @arr = split "", $s;
+ my @c_arr = split "", $c_s;
+ my @vowels = reverse map {lc} grep { /[aeiou]/i} @arr;
+ my $i = 0;
+ for my $j (0..$#c_arr) {
+ my $c = $c_arr[$j];
+ if ($c eq "=") {
+ $arr[$j] = $vowels[$i];
+ $i++;
+ }
+ if ($c eq "_") {
+ $arr[$j] = uc $vowels[$i];
+ $i++;
+ }
+ }
+ return join "", @arr;
+}
+
+use Test::More tests=>5;
+ok r_v("Raku") eq "Ruka";
+ok r_v("Perl") eq "Perl";
+ok r_v("Julia") eq "Jaliu";
+ok r_v("Uiua") eq "Auiu";
+ok r_v("Cpp") eq "Cpp";
+
diff --git a/challenge-255/cheok-yin-fung/perl/ch-1.pl b/challenge-255/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..84fc3b6da4
--- /dev/null
+++ b/challenge-255/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,23 @@
+# The Weekly Challenge 255
+# Task 1 Odd Character
+use v5.30.0;
+use warnings;
+use List::MoreUtils qw/frequency/;
+
+sub oc {
+ my $s = $_[0];
+ my $t = $_[1];
+ my %f_s = frequency split "", $s;
+ my %f_t = frequency split "", $t;
+ for my $c (keys %f_t) {
+ next if defined($f_s{$c}) && $f_t{$c} == $f_s{$c};
+ # if ($f_t{$c} == $f_s{$c} + 1 || !defined($f_s{$c})) {...}
+ # the above checker is not necessary
+ return $c;
+ }
+}
+
+use Test::More tests=>3;
+ok oc("Perl", "Preel") eq "e";
+ok oc("Weekly", "Weeakly") eq "a";
+ok oc("Box", "Boxy") eq "y";
diff --git a/challenge-255/cheok-yin-fung/perl/ch-2.pl b/challenge-255/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..24d764508f
--- /dev/null
+++ b/challenge-255/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,39 @@
+# The Weekly Challenge 255
+# Task 2 Most Frequent Word
+use v5.30.0;
+use warnings;
+use List::Util qw/max/;
+use List::MoreUtils qw/frequency/;
+
+sub mfw {
+ my $p = $_[0];
+ my $w = $_[1];
+ my @arr = split /\W+/, $p;
+ my %freq = frequency @arr;
+ my $c = 1;
+ my @max_freq_word;
+ while ($c) {
+ my $max_freq = max values %freq;
+ @max_freq_word = grep {$freq{$_} == $max_freq} keys %freq;
+ @max_freq_word = grep {$_ ne $w} @max_freq_word;
+ if (scalar @max_freq_word == 0) {
+ $freq{$w} = 0;
+ }
+ else {
+ $c = 0;
+ }
+ }
+ return $max_freq_word[0] if scalar @max_freq_word == 1;
+ return [@max_freq_word];
+}
+
+use Test2::V0;
+is mfw(
+ "Joe hit a ball, the hit ball flew far after it was hit.",
+ "hit"
+), "ball";
+is mfw(
+ "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.",
+ "the"
+), "Perl";
+done_testing;