aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCY Fung <fungcheokyin@gmail.com>2024-02-12 05:21:35 +0800
committerCY Fung <fungcheokyin@gmail.com>2024-02-12 05:21:35 +0800
commit0c0f465a0a069024148709388c24b173a6cb26e8 (patch)
treeb474ba50deca4191b78c32632820ee389dc69e70
parent33dcd8be189e911df40a39fa97f481f02c06bd84 (diff)
downloadperlweeklychallenge-club-0c0f465a0a069024148709388c24b173a6cb26e8.tar.gz
perlweeklychallenge-club-0c0f465a0a069024148709388c24b173a6cb26e8.tar.bz2
perlweeklychallenge-club-0c0f465a0a069024148709388c24b173a6cb26e8.zip
Week 255, and add Task 2 of the missed Week 254
-rw-r--r--challenge-254/cheok-yin-fung/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/ch-2.pl b/challenge-254/cheok-yin-fung/ch-2.pl
new file mode 100644
index 0000000000..307101ff7b
--- /dev/null
+++ b/challenge-254/cheok-yin-fung/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;