aboutsummaryrefslogtreecommitdiff
path: root/challenge-233
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-09-11 02:21:43 +0100
committerGitHub <noreply@github.com>2023-09-11 02:21:43 +0100
commitbb631e0876d96ab8545c14ff2eb2980ec4b626fc (patch)
tree0481be848e13abb34110aba8b1a81e80017f9f6c /challenge-233
parent3eb38dc1e8cff1e13954ff6156c871f643ab91b9 (diff)
parent12ee0b590fc517529422b2f9403cdd318cc3e5f2 (diff)
downloadperlweeklychallenge-club-bb631e0876d96ab8545c14ff2eb2980ec4b626fc.tar.gz
perlweeklychallenge-club-bb631e0876d96ab8545c14ff2eb2980ec4b626fc.tar.bz2
perlweeklychallenge-club-bb631e0876d96ab8545c14ff2eb2980ec4b626fc.zip
Merge pull request #8679 from E7-87-83/newt
Week 233
Diffstat (limited to 'challenge-233')
-rw-r--r--challenge-233/cheok-yin-fung/perl/ch-1.pl24
-rw-r--r--challenge-233/cheok-yin-fung/perl/ch-2.pl22
2 files changed, 46 insertions, 0 deletions
diff --git a/challenge-233/cheok-yin-fung/perl/ch-1.pl b/challenge-233/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..2810b71457
--- /dev/null
+++ b/challenge-233/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,24 @@
+# The Weekly Challenge 223
+# Task 1 Similar Words
+use v5.30.0;
+use warnings;
+use List::Util qw/uniqstr/;
+
+sub sw {
+ my @words = @_;
+ my @cwords = map {join "", uniqstr sort {$a cmp $b} split "", $_} @words;
+ my $ans = 0;
+ for my $i (0..$#cwords) {
+ for my $j ($i+1..$#cwords) {
+ $ans++ if $cwords[$i] eq $cwords[$j];
+ }
+ }
+ return $ans;
+}
+
+
+use Test::More tests=>3;
+ok sw("aba", "aabb", "abcd", "bac", "aabc") == 2;
+ok sw("aabb", "ab", "ba") == 3;
+ok sw("nba", "cba", "dba") == 0;
+
diff --git a/challenge-233/cheok-yin-fung/perl/ch-2.pl b/challenge-233/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..40f863985d
--- /dev/null
+++ b/challenge-233/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,22 @@
+# The Weekly challenge 233
+# Task 2 Frequency Sort
+use v5.30.0;
+use warnings;
+use List::MoreUtils qw/frequency/;
+
+sub fs {
+ my @ints = @_;
+ my %freq = frequency @ints;
+ @ints = sort {
+ ($freq{$a} <=> $freq{$b})
+ ||
+ ($freq{$a} == $freq{$b} && $b<=>$a)} @ints;
+ return [@ints];
+}
+
+use Test::More tests=>4;
+use Test::Deep;
+cmp_deeply fs(1,1,2,2,2,3), [3,1,1,2,2,2];
+cmp_deeply fs(2,3,1,3,2), [1,3,3,2,2];
+cmp_deeply fs(-1,1,-6,4,5,-6,1,4,1), [5,-1,4,4,-6,-6,1,1,1];
+cmp_deeply fs(1,3,2), [3,2,1];