aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-244/cheok-yin-fung/perl/ch-1.pl22
-rw-r--r--challenge-244/cheok-yin-fung/perl/ch-2.pl20
2 files changed, 42 insertions, 0 deletions
diff --git a/challenge-244/cheok-yin-fung/perl/ch-1.pl b/challenge-244/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..c364fc44a1
--- /dev/null
+++ b/challenge-244/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,22 @@
+# The Weekly Challenge 244
+# Task 1 Count Smaller
+use v5.30.0;
+use warnings;
+
+sub cs {
+ my @int = @_;
+ my @ans;
+ for my $k (0..$#int) {
+ push @ans, 0;
+ for my $i (0..$#int) {
+ $ans[-1]++ if $int[$i]<$int[$k];
+ }
+ }
+ return [@ans];
+}
+
+use Test::More tests=>3;
+use Test::Deep;
+cmp_deeply cs(8,1,2,2,3),[4,0,1,1,3];
+cmp_deeply cs(6,5,4,8),[2,1,0,3];
+cmp_deeply cs(2,2,2),[0,0,0];
diff --git a/challenge-244/cheok-yin-fung/perl/ch-2.pl b/challenge-244/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..315046e0c0
--- /dev/null
+++ b/challenge-244/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,20 @@
+# The Weekly Challenge 244
+# Task 2 Group Hero
+use v5.30.0;
+use warnings;
+use List::Util qw/max min/;
+sub gh {
+ my $ans = 0;
+ my @nums = @_;
+ my $num = $#nums+1;
+ for my $grp (2 .. 2 << $#nums) {
+ my $str = unpack("b$num", pack("s", $grp-1));
+ my @arr = split "", $str;
+ my @ints = map {$nums[$_]} grep {$arr[$_]} 0..$#nums;
+ $ans += max(@ints)**2 * min(@ints);
+ }
+ return $ans;
+}
+
+use Test::More tests=>1;
+ok gh(2,1,4)==141;