aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-191/cheok-yin-fung/perl/ch-1.pl23
-rw-r--r--challenge-191/cheok-yin-fung/perl/ch-2.pl45
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-191/cheok-yin-fung/perl/ch-1.pl b/challenge-191/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..ec9c61e4a8
--- /dev/null
+++ b/challenge-191/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,23 @@
+# The Weekly Challenge 191
+# Task 1 Twice Largest
+use v5.30.0;
+use List::Util qw/max all/;
+
+sub remove {
+ my @arr = $_[0]->@*;
+ return [grep {$_ != $_[1]} @arr];
+}
+
+sub twice_l {
+ my @list = @_;
+ my $max = max @list;
+ my @slist = remove([@list], $max)->@*;
+ return 1 if all {$_*2 <= $max} @slist;
+ return -1;
+}
+
+use Test::More tests=>4;
+ok twice_l(1,2,3,4) == -1;
+ok twice_l(1,2,0,5) == 1;
+ok twice_l(2,6,3,1) == 1;
+ok twice_l(4,5,2,3) == -1;
diff --git a/challenge-191/cheok-yin-fung/perl/ch-2.pl b/challenge-191/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..5431336c2f
--- /dev/null
+++ b/challenge-191/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,45 @@
+# The Weekly Challenge 191
+# Task 2 Cute List
+# only workable up to $n = 11
+use v5.30.0;
+use warnings;
+use Math::Permutation;
+
+my $n = $ARGV[0] || 1;
+
+sub fac {
+ my $ans = 1;
+ for (2..$_[0]) {$ans*=$_;}
+ return $ans;
+}
+
+sub check {
+ my $w = substr($_[0], 1, -1);
+ my @lst = split ",",$w;
+ my $count = 0;
+ for my $i (reverse 0..$n-1) {
+ last if !(($lst[$i] % ($i+1) == 0) || (($i+1) % $lst[$i] == 0));
+ $count++;
+ }
+ return ($count == $n) ? 1 : 0;
+}
+
+my $p = Math::Permutation->init($n);
+
+my $ans = 0;
+for my $i (1..fac($n)) {
+ $ans++ if check($p->sprint_wrepr);
+ $p->nxt;
+}
+
+say $ans;
+
+=pod
+
+1, 2, 3, 8, 10, 36, 41, 132, 250, 700, 750
+
+$ time perl ch-2.pl 11
+real 6m49.683s
+user 6m48.721s
+sys 0m0.192s
+