aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-24 08:39:59 +0000
committerGitHub <noreply@github.com>2022-12-24 08:39:59 +0000
commit03a5b2e285225643226df71df1370033e195f9e0 (patch)
tree4487d9b8a9eed24af470339b78f0b5d108568e4c
parent7e75032e92257085e42a6d8e1d8c88c9772d244c (diff)
parent69a63fc06125eda422dcde1266bed555adc8eaf7 (diff)
downloadperlweeklychallenge-club-03a5b2e285225643226df71df1370033e195f9e0.tar.gz
perlweeklychallenge-club-03a5b2e285225643226df71df1370033e195f9e0.tar.bz2
perlweeklychallenge-club-03a5b2e285225643226df71df1370033e195f9e0.zip
Merge pull request #7298 from 0rir/196
196
-rw-r--r--challenge-196/0rir/raku/ch-1.raku143
-rw-r--r--challenge-196/0rir/raku/ch-2.raku71
2 files changed, 214 insertions, 0 deletions
diff --git a/challenge-196/0rir/raku/ch-1.raku b/challenge-196/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..152149ae92
--- /dev/null
+++ b/challenge-196/0rir/raku/ch-1.raku
@@ -0,0 +1,143 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «␤»
+use v6.e.PREVIEW;
+use Test;
+
+=begin comment
+196-1: Pattern 132 Submitted by: Mohammad S Anwar
+Given a list of integers, @list, find all subsequence that respect Pattern 132. Return empty array if none found.
+
+Pattern 132 is a sequence (a[i], a[j], a[k]) such that i < j < k and a[i] < a[k] < a[j].
+
+Example 1
+Input: @list = (3, 1, 4, 2)
+Output: (1, 4, 2) respect the Pattern 132.
+Example 2
+Input: @list = (1, 2, 3, 4)
+Output: () since no susbsequence can be found.
+Example 3
+Input: @list = (1, 3, 2, 4, 6, 5)
+Output: (1, 3, 2) if more than one subsequence found then return the first.
+Example 4
+Input: @list = (1, 3, 4, 2)
+Output: (1, 3, 2)
+=end comment
+
+=begin comment
+The spec says to find all triads and to return only the first. Doing both.
+"First" commonly means the sequence starting with the left-most head and, as
+a tie breaker, ending with left-most tail, and, as a second tie breaker,
+having the left-most body. That is the definition used here.
+=end comment
+
+multi MAIN ( 'test' ) {
+ # input, expect-single, expect
+ my @Test =
+ ( [], [], [],),
+ ( [1,], [], [],),
+ ( [1,2], [], [],),
+ ( [1,2,3], [], [],),
+ ( [1,2,3,4], [], [],),
+ ( [1,2,3,4,5], [], [],),
+ ( [0,1,2,3,4,5], [], [],),
+ ( [1,1,1], [], [],),
+ ( [1,1,2], [], [],),
+ ( [1,2,1], [], [],),
+ ( [2,1,1], [], [],),
+ ( [2,2,1], [], [],),
+ ( [2,2,2], [], [],),
+
+ ( [ -1,2,1], [-1,2,1], [[ -1,2,1],]),
+
+ ( [1,4,2], [1,4,2], [[1,4,2],],),
+ ( [3,1,4,2], [1,4,2], [[1,4,2],],),
+ ( [3,1,4,2,5], [1,4,2], [[1,4,2],],),
+
+ ( [1,4,6,4,3,5,10],
+ [1,6,4],
+ [[1,4,3],[1,4,3],[1,6,3],[1,6,4],[1,6,5],[4,6,5]],), #dupes
+ ( [10,1,4,6,4,3,5],
+ [1,6,4],
+ [[1,4,3],[1,4,3],[1,6,3],[1,6,4],[1,6,5],[4,6,5]],), #dupes
+ ( [10,1,4,6,4,3,5,10],
+ [1,6,4],
+ [[1,4,3],[1,4,3],[1,6,3],[1,6,4],[1,6,5],[4,6,5]],), #dupes
+ ( [1,2,3,4], [], [],),
+ ( [1,3,2,4,6,5],
+ [1,3,2],
+ [[1,3,2],[1,6,5],[3,6,5],[2,6,5],[4,6,5]],),
+ ( [1,1,3,4,2,1,1,1],
+ [1,3,2],
+ [[1,3,2],[1,4,2],[1,3,2],[1,4,2] ] ), #dupes
+
+ ( [42,95,17,4,25,51,25,36,38,85,82,13,63,8],
+ [42,95,51],
+ [
+ [ 4,13, 8],
+ [ 4,25, 8], [ 4,25, 8], [ 4,25,13], [ 4,25,13], # dupes
+ [ 4,36, 8], [ 4,36,13],
+ [ 4,38, 8], [ 4,38,13], [ 4,51, 8], [ 4,51,13], [ 4,51,25],
+ [ 4,51,36], [ 4,51,38], [ 4,63, 8], [ 4,82, 8], [ 4,82,13],
+ [ 4,82,63], [ 4,85, 8], [ 4,85,13], [ 4,85,63], [ 4,85,82],
+ [17,51,25], [17,51,36], [17,51,38], [17,82,63], [17,85,63],
+ [17,85,82],
+ [25,51,36], [25,51,38],
+ [25,82,63], [25,82,63], # dupes
+ [25,85,63], [25,85,63], # dupes
+ [25,85,82], [25,85,82], # dupes
+ [36,82,63], [36,85,63], [36,85,82],
+ [38,82,63], [38,85,63], [38,85,82],
+ [42,82,63], [42,85,63], [42,85,82], [42,95,63],
+ [42,95,51], [42,95,82], [42,95,85],
+ [51,82,63], [51,85,63], [51,85,82],
+ ],
+ ),
+ ;
+
+ plan 2 × @Test;
+ for @Test -> @a {
+ my @in = @a[0];
+ my @single-exp = @a[1];
+ my @exp = @a[2];
+ my @ans = get132triads(@in );
+ is-deeply @ans.sort, @exp.sort, "Test: " ~ @in.raku;
+ is-deeply get132triad( @in), @single-exp, 'Test single: ' ~ @in.raku;
+ }
+ done-testing;
+}
+
+sub get132triad( @in --> Array ) {
+ return [] if @in ~~ Empty;
+ return [] if @in.elems < 3;
+ get132triads( @in, :single);
+}
+
+sub get132triads( @in, Bool :$single --> Array ) {
+ return [] if @in ~~ Empty;
+ return [] if @in.elems < 3;
+
+ my ($head, $tail, @caught, @ret) = 0, 2;
+ while $head < @in.end -1 {
+ while $tail < @in.elems {
+ if @in[$tail] ≤ @in[$head] {
+ ++ $tail;
+ next;
+ }
+ @caught = (@in[$head^..^$tail].grep: * > @in[$tail]); #.unique;
+ for @caught -> $m {
+ @ret.push: [ @in[$head], $m, @in[$tail]];
+ once if $single { return @ret[0]<>; }
+ }
+ ++ $tail;
+ }
+ ++ $head;
+ $tail = $head + 2;
+ }
+ return @ret;
+}
+
+multi MAIN( @list = [42,95,17,4,25,51,25,36,38,85,82,13,63,8]) {
+ say "Input: \@list = @list.raku()
+Output: ", my @res = get132triad( @list);
+ die 'wrong' unless @res eqv [42,95,51];
+}
diff --git a/challenge-196/0rir/raku/ch-2.raku b/challenge-196/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..821977fc64
--- /dev/null
+++ b/challenge-196/0rir/raku/ch-2.raku
@@ -0,0 +1,71 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «␤»
+use v6.e.PREVIEW;
+use Test;
+
+=begin comment
+196-2: Range List Submitted by: Mohammad S Anwar
+Given a sorted unique integer array, @array, find all possible Number
+Range i.e [x, y] represent range all integers from x and y (both inclusive).
+
+Example 1
+Input: @array = (1,3,4,5,7)
+Output: [3,5]
+Example 2
+Input: @array = (1,2,3,6,7,9)
+Output: [1,3], [6,7]
+Example 3
+Input: @array = (0,1,2,4,5,6,8,9)
+Output: [0,2], [4,6], [8,9]
+=end comment
+
+multi MAIN (@array = (9...90, 1_001,1_002,1_003,1_004) ) {
+ say "Input: @array = @array.raku()\n
+Output: ", my @r = get-ranges( @array);;
+ die 'wrong' unless @r eqv [9,90],[1_001,1_004];
+}
+
+multi MAIN ( 'test' ) {
+ my @Test =
+ { in => Empty, exp => [], },
+ { in => 99999, exp => [], },
+ { in => [1,2], exp => [1,2], },
+ { in => [1,2,3], exp => [1,3], },
+ { in => [5,6,9], exp => [5,6], },
+ { in => [1...7,9], exp => [1,7], },
+ { in => [1,3,5,7,9], exp => [] },
+ { in => [1,2,3,6,7,9], exp => [ 1,3, 6,7], },
+ { in => [2,3,6,7,9], exp => [ 2,3, 6,7], },
+ { in => [2,3,100_006,100_007,100_008],
+ exp => [ 2,3, 100_006,100_008], },
+ { in => [5,6,7, 1_000_001,1_000_002,1_000_003,1_000_004],
+ exp => [ [5,7],[1_000_001,1_000_004]] },
+ { in => [ 9...99_000, 1_000_001,1_000_002,1_000_003,1_000_004],
+ exp => [ [9,99_000],[1_000_001,1_000_004]] },
+ ;
+ plan +@Test;
+ for @Test -> %t {
+ my @in = |%t<in>;
+ my $exp = %t<exp>;
+ quietly is get-ranges( @in), $exp , @in.gist ~" --> "~ $exp.raku;
+ }
+ done-testing;
+}
+
+sub get-ranges( @a --> Array ) {
+ return [] if @a ~~ Empty or 1 == @a.elems;
+ my @return = Empty;
+ my @w = @a.pairs;
+ my $hd-idx = 0;
+ while $hd-idx < @w.end {
+ my $offset = @w[$hd-idx].value - @w[$hd-idx].key;
+ my $tail = @w[1+$hd-idx..@w.end].first(
+ { .value - .key == $offset}, :end );
+ with $tail {
+ @return.push: [ @w[$hd-idx].value, $tail.value];
+ $hd-idx = $tail.key;
+ }
+ ++$hd-idx;
+ }
+ return @return;
+}