diff options
| author | CY Fung <fungcheokyin@gmail.com> | 2023-09-11 00:06:02 +0800 |
|---|---|---|
| committer | CY Fung <fungcheokyin@gmail.com> | 2023-09-11 00:06:02 +0800 |
| commit | 12ee0b590fc517529422b2f9403cdd318cc3e5f2 (patch) | |
| tree | 058063d6123d950e4814fba76d00ee5b8933c7fd | |
| parent | 10a90bc9e30c0a441304591425dfecdcb7802720 (diff) | |
| download | perlweeklychallenge-club-12ee0b590fc517529422b2f9403cdd318cc3e5f2.tar.gz perlweeklychallenge-club-12ee0b590fc517529422b2f9403cdd318cc3e5f2.tar.bz2 perlweeklychallenge-club-12ee0b590fc517529422b2f9403cdd318cc3e5f2.zip | |
Week 233
| -rw-r--r-- | challenge-233/cheok-yin-fung/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-233/cheok-yin-fung/perl/ch-2.pl | 22 |
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]; |
