diff options
Diffstat (limited to 'challenge-199')
115 files changed, 3462 insertions, 2 deletions
diff --git a/challenge-199/0rir/raku/ch-1.raku b/challenge-199/0rir/raku/ch-1.raku new file mode 100755 index 0000000000..867cc81695 --- /dev/null +++ b/challenge-199/0rir/raku/ch-1.raku @@ -0,0 +1,68 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use lib $?FILE.IO.parent(2).add("lib"); +use Test; + +=begin comment +199-1: Good Pairs Submitted by: Mohammad S Anwar +Given a list of integers, @list, find the total count of Good Pairs. +A pair (i, j) is called good if list[i] == list[j] and i < j. + +Example 1 +Input: @list = (1,2,3,1,1,3) +Output: 4 + +4 good pairs found: (0,3) (0,4) (3,4) (2,5) + +Example 2 +Input: @list = (1,2,3) +Output: 0 + +Example 3 +Input: @list = (1,1,1,1) +Output: 6 + +Good pairs are: (0,1) (0,2) (0,3) (1,2) (1,3) (2,3) +=end comment + +multi sub ct-good-pairs( Empty --> 0 ) {} +multi sub ct-good-pairs( @list --> Int) { + #A pair (i, j) is called good if list[i] == list[j] and i < j. + my $return = 0; + for 0..@list.end -> $i { + for $i^..@list.end -> $j { + ++ $return if @list[$i] == @list[$j]; + } + } + $return; +} + +multi MAIN ( ) { + my @Test = + [6,6,6,6] => 6, + [3,3,3] => 3, + [1,1] => 1, + [0,] => 0, + [0,2,3] => 0, + [1,2,1,2] => 2, + [1,2,3,1,2,3] => 3, + [1,2,9,1,2,9,1,2,9] => 9, + [3,4,5,3,4,5,1,2,12,1,2,12,1,2,12] => 12, + [12,11,10,3,4,5,3,4,5,1,2,9,1,2,9,1,2,9] => 12, + [12,11,10,6,7,8,3,4,5,3,4,5,1,2,9,1,2,9,1,2,9] => 12, + ; + plan 2 * @Test +1; + + is ct-good-pairs(Empty), 0, '0 <- ' ~ (Empty).raku; + for @Test -> $p { + is ct-good-pairs( $p.key), $p.value, "$p.value() <- $p.key().raku()"; + is ct-good-pairs( $p.key.pick(*)), $p.value, + "$p.value() <- $p.key().pick(*).Array.raku() shuffle"; + } + done-testing; + + my @list = [12,11,10,3,4,5,3,4,5,1,2,9,1,2,9,1,2,9]; + say "\nInput: \@list = @list.raku()\nOutput: &ct-good-pairs(@list)"; + exit; +} diff --git a/challenge-199/0rir/raku/ch-2.raku b/challenge-199/0rir/raku/ch-2.raku new file mode 100755 index 0000000000..562400e230 --- /dev/null +++ b/challenge-199/0rir/raku/ch-2.raku @@ -0,0 +1,94 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ «» +use v6.d; +use Test; + +=begin comment +199-2: Good Triplets Submitted by: Mohammad S Anwar +Given an array of integers, @array and three integers $x,$y,$z, +find the total of Good Triplets in the given array. + +Array[i], array[j], and array[k] are a Good Triplet if they satisfies +the following conditions: + +a) 0 <= i < j < k <= n (size of given array) +b) abs(array[i] - array[j]) <= x +c) abs(array[j] - array[k]) <= y +d) abs(array[i] - array[k]) <= z + +Example 1 +Input: @array = (3,0,1,1,9,7) and $x = 7, $y = 2, $z = 3 +Output: 4 + +Good Triplets are as below: +(3,0,1) where (i=0, j=1, k=2) +(3,0,1) where (i=0, j=1, k=3) +(3,1,1) where (i=0, j=2, k=3) +(0,1,1) where (i=1, j=2, k=3) +Example 2 +Input: @array = (1,1,2,2,3) and $x = 0, $y = 0, $z = 1 +Output: 0 +=end comment + +sub ct-good-triplets( @array, :$x, :$y, :$z --> Int ) { + my $return = 0; + for 0..@array.end -> $i { + for $i^..@array.end -> $j { + for $j^..@array.end -> $k { + if abs(@array[$i] - @array[$j]) <= $x + and abs(@array[$j] - @array[$k]) <= $y + and abs(@array[$i] - @array[$k]) <= $z { + ++$return; + } } } } + $return; +} + +multi MAIN() { + my @Test = + { array => Empty, x => 7, y => 2, z => 3, exp => 0 }, + { array => [3,0,1,1,9,7], x => 7 |
