aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-203/0rir/raku/ch-1.raku117
-rwxr-xr-xchallenge-203/0rir/raku/ch-2.raku149
2 files changed, 266 insertions, 0 deletions
diff --git a/challenge-203/0rir/raku/ch-1.raku b/challenge-203/0rir/raku/ch-1.raku
new file mode 100755
index 0000000000..451d575e14
--- /dev/null
+++ b/challenge-203/0rir/raku/ch-1.raku
@@ -0,0 +1,117 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # ๐Ÿฆ‹ โˆ… โ‰ก โˆฉ โ‰ข โˆˆ ยซ โค ยป โˆด
+use v6.e.PREVIEW;
+use lib $?FILE.IO.parent(2).add("lib");
+use Test;
+use Array::Sorted::Util;
+
+=begin comment
+203-1: Special Quadruplets Submitted by: Mohammad S Anwar
+Given an array of integers, find the total special quadruplets for the array.
+
+Special Quadruplets are defined by the following 2 rules.
+1) nums[a] + nums[b] + nums[c] == nums[d]
+2) a < b < c < d
+
+Example 1
+Input: @nums = (1,2,3,6)
+Output: 1
+
+Since the only special quadruplets found is $nums[0] + $nums[1] + $nums[2] == $nums[3].
+Example 2
+Input: @nums = (1,1,1,3,5)
+Output: 4
+
+$nums[0] + $nums[1] + $nums[2] == $nums[3]
+$nums[0] + $nums[1] + $nums[3] == $nums[4]
+$nums[0] + $nums[2] + $nums[3] == $nums[4]
+$nums[1] + $nums[2] + $nums[3] == $nums[4]
+Example 3
+Input: @nums = (3,3,6,4,5)
+Output: 0
+
+=end comment
+
+=begin comment
+
+=end comment
+
+my @Test =
+ [] => [],
+ [1,] => [],
+ [1,2] => [],
+ [1,2,3] => [],
+ [1,2,3,4] => [],
+
+ [1,2,3,6] => [[1,2,3,6],],
+ [1,2,1,2,3,6] => [[1,2,3,6],[1,2,3,6],[1,2,3,6],],
+ [3,3,6,4,5] => [],
+ [0,1,2,3] => [[0,1,2,3],],
+ [9,0,1,2,3] => [[0,1,2,3],],
+ [9,0,1,2,3,4] => [[0,1,2,3],[0,1,3,4]],
+ [9,0,1,2,3,4,5,6] => [
+ [0,1,2,3],[0,1,3,4],[0,1,4,5],[0,1,5,6],[0,2,3,5],[0,2,4,6],[1,2,3,6]],
+
+ [6,3,14,10,11,1,2,27,30,35] => [[6,10,11,27],[1,2,27,30]],
+
+ [ 6,3,14,10,11,1,2,27,30,35,30] => [[6,10,11,27],[1,2,27,30],[1,2,27,30]],
+;
+
+plan 2 ร— +@Test;
+
+# make an array of the same size with A o A[sum-of-2-num, index of 1st num]
+sub _first-sums( @a --> Array) {
+ my @presum = [] xx @a.elems;
+ enum Sum <SUM IDX>;
+
+ for 0..^@a-1 -> $i {
+ for 1+$i ..^@a-1 -> $j {
+ if @a[$i] < @a[$j] {
+ @presum[$j].push: [ @a[$i] + @a[$j], $i];
+ } } }
+ return @presum;
+}
+
+sub special-quad-count( @a --> UInt ) {
+ my @presum = _first-sums( @a);
+ my $return = 0;
+ for 0..^@presum -> $i {
+ next if @presum[$i] ~~ Empty;
+ for @presum[$i] -> @stack {
+ for @stack -> @sum {
+ for $i+1 ..^@a-1 -> $t {
+ next if @sum[SUM] - @a[@sum[IDX]] โ‰ฅ @a[$t];
+ $return += +@a[$t+1..^@a].grep( * ~~ @sum[SUM] + @a[$t]);
+ } } } }
+ $return;
+}
+
+sub make-special-quads( @a --> Array ) {
+ my @return;
+
+ my @presum = _first-sums( @a);
+
+ for 0..^@presum -> $i {
+ next if @presum[$i] ~~ Empty;
+ for @presum[$i] -> @stack {
+ for @stack -> @sum {
+ for $i+1 ..^@a-1 -> $t {
+ next if @sum[SUM] - @a[@sum[IDX]] โ‰ฅ @a[$t];
+ my @last = @a[$t+1..^@a].grep( * ~~ @sum[SUM] + @a[$t]);
+ for @last -> $fourth {
+ my $first = @a[@sum[IDX]];
+ my $second = @sum[SUM]-@a[@sum[IDX]];
+ my $third = @a[$t];
+ @return[+*] = [ $first, $second, $third, $fourth];
+ } } } } }
+ @return;
+}
+
+for @Test -> ( :key(@in), :value(@exp)) {
+ is special-quad-count(@in), @exp.elems, 'count';
+ is-deeply make-special-quads(@in), @exp, "@in.raku() => @exp.raku()";
+}
+
+my @num = [ 6,3,14,10,11,1,2,27,30,35,30];
+
+say "\nInput: \@nums = @num[]\nOutput: &special-quad-count(@num)";
diff --git a/challenge-203/0rir/raku/ch-2.raku b/challenge-203/0rir/raku/ch-2.raku
new file mode 100755
index 0000000000..e7b84c5ae5
--- /dev/null
+++ b/challenge-203/0rir/raku/ch-2.raku
@@ -0,0 +1,149 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # ๐Ÿฆ‹ โˆ… โ‰ก โˆฉ โ‰ข โˆˆ ยซ โค ยป โˆด
+use v6.d;
+use Test;
+use File::Directory::Tree;
+
+=begin comment
+203-2: Copy Directory Submitted by: Julien Fiegehenn
+
+Given path to two folders, $source and $target, recursively copy the
+directory from $source to $target except any files.
+
+Example
+Input: $source = '/a/b/c' and $target = '/x/y'
+
+Source directory structure:
+
+โ”œโ”€โ”€ a
+โ”‚ โ””โ”€โ”€ b
+โ”‚ โ””โ”€โ”€ c
+โ”‚ โ”œโ”€โ”€ 1
+โ”‚ โ”‚ โ””โ”€โ”€ 1.txt
+โ”‚ โ”œโ”€โ”€ 2
+โ”‚ โ”‚ โ””โ”€โ”€ 2.txt
+โ”‚ โ”œโ”€โ”€ 3
+โ”‚ โ”‚ โ””โ”€โ”€ 3.txt
+โ”‚ โ”œโ”€โ”€ 4
+โ”‚ โ””โ”€โ”€ 5
+โ”‚ โ””โ”€โ”€ 5.txt
+
+Target directory structure:
+
+โ”œโ”€โ”€ x
+โ”‚ โ””โ”€โ”€ y
+
+Expected Result:
+
+โ”œโ”€โ”€ x
+โ”‚ โ””โ”€โ”€ y
+โ”‚ โ”œโ”€โ”€ 1
+โ”‚ โ”œโ”€โ”€ 2
+โ”‚ โ”œโ”€โ”€ 3
+โ”‚ โ”œโ”€โ”€ 4
+โ”‚ โ””โ”€โ”€ 5
+=end comment
+
+my @Test =
+ { top => 'a/b/c/',
+ dest => 'x/y/',
+ src => [ '1/', '1/1.txt', '2/', '2/2.txt', '3/',
+ '3/3.txt', '4/', '5/', '5/5.txt' ],
+ exp => './sandbox/x:
+y
+
+./sandbox/x/y:
+1
+2
+3
+4
+5
+
+./sandbox/x/y/1:
+
+./sandbox/x/y/2:
+
+./sandbox/x/y/3:
+
+./sandbox/x/y/4:
+
+./sandbox/x/y/5:
+',
+ },
+ { top => 'a/b/',
+ dest => 'x/y/',
+ src =>
+ ['c/d/e/1/', 'c/d/f/1/', 'c/d/f/2/', 'c/d/e/4/', 'c/d/e/4/4.txt',],
+ exp => './sandbox/x:
+y
+
+./sandbox/x/y:
+c
+
+./sandbox/x/y/c:
+d
+
+./sandbox/x/y/c/d:
+e
+f
+
+./sandbox/x/y/c/d/e:
+1
+4
+
+./sandbox/x/y/c/d/e/1:
+
+./sandbox/x/y/c/d/e/4:
+
+./sandbox/x/y/c/d/f:
+1
+2
+
+./sandbox/x/y/c/d/f/1:
+
+./sandbox/x/y/c/d/f/2:
+'
+ },
+;
+
+my $box = < ./sandbox >;
+
+sub init-test( $test-num ) {
+ my %h = @Test[$test-num];
+ my $top = %h<top>;
+ for @(%h<src>) -> $p {
+ if $p ~~ / \w* '/' $ / {
+ mkdir "$box/$top/$p".IO();
+ } else {
+ spurt "$box/$top/$p", " This is $p";
+ } }
+ mkdir "$box/%h<dest>"
+}
+
+sub clean-test( --> Nil) { empty-directory $box; }
+
+# copy a directory tree's directories only
+sub copy-dir-tree-struct( Str $src, Str $dest ) {
+ my @from;
+ indir( $src, {
+ my @todo = '.'.IO;
+ @from = gather while @todo {
+ for @todo.pop.dir -> $path {
+ @todo.push: $path if $path.d;
+ take $path.Str if $path.d;
+ } } });
+ indir( $dest, {
+ for @from -> $dir-name { mkdir "$*CWD/$dir-name";}
+ });
+}
+
+plan +@Test;
+
+for 0..^@Test -> $n {
+ init-test( $n );
+ copy-dir-tree-struct( "$box/@Test[$n]<top>", "$box/@Test[$n]<dest>");
+ my $result = qqx{ls -R $box/x}; # XXX magic 'x'
+ is $result, @Test[$n]<exp>;
+ clean-test();
+}
+