aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-10-12 12:45:58 +0100
committerGitHub <noreply@github.com>2021-10-12 12:45:58 +0100
commitc103a81faaadd3e1608e8d690c756a52827ea4bc (patch)
tree5d80e7901d0290fcd04561a6680cae931ef01efb
parentf4ede652d93c176ff60d0ff1759f6505042c3c59 (diff)
parent8434299312e341eb515f1bf9507a6282f17488db (diff)
downloadperlweeklychallenge-club-c103a81faaadd3e1608e8d690c756a52827ea4bc.tar.gz
perlweeklychallenge-club-c103a81faaadd3e1608e8d690c756a52827ea4bc.tar.bz2
perlweeklychallenge-club-c103a81faaadd3e1608e8d690c756a52827ea4bc.zip
Merge pull request #5014 from andemark/branch-for-challenge-134
ch-1.raku do-over
-rw-r--r--challenge-134/mark-anderson/raku/ch-1.raku50
1 files changed, 44 insertions, 6 deletions
diff --git a/challenge-134/mark-anderson/raku/ch-1.raku b/challenge-134/mark-anderson/raku/ch-1.raku
index 82bea0e855..b60563f66c 100644
--- a/challenge-134/mark-anderson/raku/ch-1.raku
+++ b/challenge-134/mark-anderson/raku/ch-1.raku
@@ -1,11 +1,49 @@
#!/usr/bin/env raku
-say pandigital(100);
-say pandigital(999);
-say pandigital(^5);
-say pandigital(400, 300, 200, 100, 0);
+# This program only deals with pandigital numbers up to 9,876,543,210
+# (or up to the 3,265,920th)
-sub pandigital(+$arr where .all ~~ UInt)
+use Test;
+plan 2;
+
+subset pd-index of UInt where * < 3_265_920;
+
+my $seq := 0, 362879, 362880, { |($^a + 362880, $^b + 362880) } ... *;
+
+is-deeply pandigital(^5), (1023456789,
+ 1023456798,
+ 1023456879,
+ 1023456897,
+ 1023456978), 'Example 1';
+
+is-deeply pandigital($seq[^18]), (1023456789, 1987654320,
+ 2013456789, 2987654310,
+ 3012456789, 3987654210,
+ 4012356789, 4987653210,
+ 5012346789, 5987643210,
+ 6012345789, 6987543210,
+ 7012345689, 7986543210,
+ 8012345679, 8976543210,
+ 9012345678, 9876543210), 'Edge Cases';
+
+sub pandigital(+$arr where .all ~~ pd-index)
{
- (1023456789.comb.permutations)[|$arr]>>.join.join(', ');
+ gather
+ {
+ for 1..9 -> $n
+ {
+ for create-list($n).permutations -> $list
+ {
+ last if $list[0] == 0;
+ take $list;
+ }
+ }
+ }[|$arr]>>.join>>.Int
}
+
+sub create-list($n)
+{
+ my $head = ($n, 0);
+ my $tail = ^10 (-) $head;
+ |$head, |$tail.keys.sort;
+}