aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-11-10 19:49:36 +0000
committerGitHub <noreply@github.com>2024-11-10 19:49:36 +0000
commit1fb3df137c74ff372e886c33b11977c2c03391cb (patch)
tree901cf537812c0abd0ce077b4b15ef740ce336e04
parente4208a3bb3974effe49d5453908659ebda5870b2 (diff)
parent9a607dd163cd247de874943769520c38302fc1ed (diff)
downloadperlweeklychallenge-club-1fb3df137c74ff372e886c33b11977c2c03391cb.tar.gz
perlweeklychallenge-club-1fb3df137c74ff372e886c33b11977c2c03391cb.tar.bz2
perlweeklychallenge-club-1fb3df137c74ff372e886c33b11977c2c03391cb.zip
Merge pull request #11146 from wambash/challenge-week-294
solutions week 294
-rw-r--r--challenge-294/wambash/raku/ch-1.raku31
-rw-r--r--challenge-294/wambash/raku/ch-2.raku81
2 files changed, 112 insertions, 0 deletions
diff --git a/challenge-294/wambash/raku/ch-1.raku b/challenge-294/wambash/raku/ch-1.raku
new file mode 100644
index 0000000000..1de16f4bbf
--- /dev/null
+++ b/challenge-294/wambash/raku/ch-1.raku
@@ -0,0 +1,31 @@
+#!/usr/bin/env raku
+use v6.e.*;
+
+sub consecutive-sequence-iter ( (Set() $ints, :$max =0) ) {
+ $ints.keys.min, { $ints{$_+1}:k } ... {!$ints{$_}}\
+ andthen .Set
+ andthen \( $ints (-) $_, max => $max max $_.elems )
+}
+
+multi consecutive-sequence (+ints, :$max=0) {
+ \(ints.Set, :$max), &consecutive-sequence-iter ... !*.head.elems
+ andthen .tail.<max>
+}
+
+
+multi MAIN (Bool :test($)!) {
+ use Test;
+ is-deeply consecutive-sequence-iter( \(set(10, 4, 20, 1, 3, 2))), \(set(10,20), :4max);
+ is-deeply consecutive-sequence-iter( \(set(10, 20), :4max)), \(set(20), :4max);
+ is-deeply consecutive-sequence-iter( \(set(20), :4max)), \(set(), :4max);
+ is-deeply consecutive-sequence(10, 4, 20, 1, 3, 2), 4;
+ is-deeply consecutive-sequence(0, 6, 1, 8, 5, 2, 4, 3, 0, 7), 9;
+ is-deeply consecutive-sequence(10,20), 1;
+ is-deeply consecutive-sequence(), 0;
+ is-deeply consecutive-sequence(0, 6, 1, 8, 5,-10, 2,-20, 4, 3, 0, 7), 9;
+ done-testing;
+}
+
+multi MAIN (+ints) {
+ say consecutive-sequence intsĀ».Int
+}
diff --git a/challenge-294/wambash/raku/ch-2.raku b/challenge-294/wambash/raku/ch-2.raku
new file mode 100644
index 0000000000..5a3fca3fb3
--- /dev/null
+++ b/challenge-294/wambash/raku/ch-2.raku
@@ -0,0 +1,81 @@
+#!/usr/bin/env raku
+use v6.e.PREVIEW;
+
+sub perm2seq-it (($hd,+tail)) {
+ $hd -1 , tail.map: { $_ > $hd ?? $_ - 1 !! $_ }
+}
+
+sub perm2seq(+@int) {
+ (-1,@int), -> ($, @perm) { perm2seq-it @perm } ... !*[1].elems
+ andthen .skip
+ andthen .map: *[0]
+}
+
+sub seq2int (+seq) {
+ [+] seq.reverse.skip Z* [\*] 1..*
+}
+
+my &perm2int = &perm2seq Ro &seq2int;
+
+sub int2seq ($n, $k) {
+ $n
+ andthen .polymod(1..*)
+ andthen .reverse
+ andthen |(0 xx $k - .elems), |$_
+}
+
+sub seq2perm-it ($ind, @adept) {
+ @adept[ $ind ], @adept.skip($ind,1,*)
+}
+
+sub seq2perm (@seq,@adept) {
+ ((-1,@adept), |@seq).produce: -> ($, @ad), $ind { seq2perm-it $ind, @ad }\
+ andthen .skip
+ andthen .map: *[0]
+}
+
+sub int2perm($n, @adept) {
+ int2seq $n, @adept.elems
+ andthen seq2perm $_, @adept;
+}
+
+sub next-permutation (+perm) {
+ perm2int(perm) + 1
+ andthen int2perm $_, 1.. perm.elems
+}
+
+multi MAIN (Bool :test($)!) {
+ use Test;
+ is perm2seq-it((2,1,3)), (1,(1,2));
+ is perm2seq-it((1,2)), (0,(1));
+ is-deeply perm2seq(2,1,3), (1,0,0);
+ is perm2seq(5,2,4,1,3), (4,1,2,0,0);
+ is perm2seq(4,2,5,3,1), (3,1,2,1,0);
+ is seq2int(3,1,2,1,0), 83;
+ is perm2int(4,2,5,3,1), 83;
+ subtest {
+ is perm2int(.value),.key, "perm {.value} is {.key}" for permutations(1..3).pairs;
+ }, 'perm2int 3';
+ subtest {
+ is perm2int(.value),.key, "perm {.value} is {.key}" for permutations(1..4).pairs;
+ }, 'perm2int 4';
+ is int2seq(83,5),(3,1,2,1,0);
+ is int2seq(83,7),(0,0,3,1,2,1,0);
+ is seq2perm-it(3,(1..5)), (4,(1,2,3,5));
+ is-deeply seq2perm((3,1,2,1,0),1..5), (4,2,5,3,1);
+ is int2perm(83,1..5), (4,2,5,3,1);
+ subtest {
+ is int2perm(.key, 1..3), .value, "perm {.value} is {.key}" for permutations(1..3).pairs;
+ }, 'int2perm 3';
+ subtest {
+ is int2perm(.key, 1..4), .value, "perm {.value} is {.key}" for permutations(1..4).pairs;
+ }, 'int2perm 4';
+ is next-permutation(1,2,3),(1,3,2);
+ is next-permutation(2,1,3),(2,3,1);
+ is next-permutation(3,1,2),(3,2,1);
+ done-testing;
+}
+
+multi MAIN (+int) {
+ put next-permutation int
+}