diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-30 15:56:26 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-30 15:56:26 +0000 |
| commit | 976fac490edef69d2e929b748879f6f96f29beef (patch) | |
| tree | 103bba087c8e068c1d077be1890c4d5f8f0abb52 | |
| parent | f901bacf3a0c04c061216a60f50ec1a332039af1 (diff) | |
| parent | 28af4e6c7bc7a561f74a6aea4ebefb89612b04ef (diff) | |
| download | perlweeklychallenge-club-976fac490edef69d2e929b748879f6f96f29beef.tar.gz perlweeklychallenge-club-976fac490edef69d2e929b748879f6f96f29beef.tar.bz2 perlweeklychallenge-club-976fac490edef69d2e929b748879f6f96f29beef.zip | |
Merge pull request #9834 from 0rir/work
262
| -rw-r--r-- | challenge-260/0rir/raku/ch-1.raku | 1 | ||||
| -rw-r--r-- | challenge-262/0rir/raku/ch-1.raku | 65 | ||||
| -rw-r--r-- | challenge-262/0rir/raku/ch-2.raku | 61 |
3 files changed, 127 insertions, 0 deletions
diff --git a/challenge-260/0rir/raku/ch-1.raku b/challenge-260/0rir/raku/ch-1.raku index a1aeab2376..f44608bdee 100644 --- a/challenge-260/0rir/raku/ch-1.raku +++ b/challenge-260/0rir/raku/ch-1.raku @@ -52,5 +52,6 @@ my @int = (1 xx 70, 2 xx 71, 3 xx 72, 4 xx 75, 5 xx 76).flat; say "\nInput: @int = @int[]\nOutput: ", func(@int).Int; + exit; diff --git a/challenge-262/0rir/raku/ch-1.raku b/challenge-262/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..463831657a --- /dev/null +++ b/challenge-262/0rir/raku/ch-1.raku @@ -0,0 +1,65 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6d; +use Test; + +=begin comment +262-1: Max Positive Negative Submitted by: Mohammad Sajid Anwar +You are given an array of integers, @ints. + +Write a script to return the maximum number of either positive or negative integers in the given array. + +Example 1 +Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +Output: 4 + +Count of positive integers: 4 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 4 +Example 2 +Input: @ints = (-1, -2, -3, 1) +Output: 3 + +Count of positive integers: 1 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 3 +Example 3 + +Count of positive integers: 2 +Count of negative integers: 0 +Maximum of count of positive and negative integers: 2 +=end comment + +my @Test = + (-3, 1, 2, -1, 3, -2, 4), 4, + (-1, -2, -3, 1), 3, + (1,2), 2, + (-2,2), 1, + (-2,), 1, + (2,), 1, + (1,0,1), 2, + (-1,0,0,0,1), 1, + (1,0,0,0,1), 2, + (0,0,0,0,0), 0, + (), 0, + +; +plan @Test ÷ 2; + +sub func( @a -->Int) { + my %h = @a.classify: { when $_ < 0 { Less } + when $_ > 0 { More } } + + for Less, More { %h{$_} = %h{$_} :exists ?? %h{$_}.elems !! 0 } + + %h{Less} max %h{More} +} + +for @Test -> @in, $exp { + is func(@in), $exp, "$exp <- @in.sort()"; +} +done-testing; + +my @in = -3, 1, 2, -1, 3, -2, 4; +say "\nInput: @in = @in.raku()\nOutput: &func(@in)" + diff --git a/challenge-262/0rir/raku/ch-2.raku b/challenge-262/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..515a9a3ca0 --- /dev/null +++ b/challenge-262/0rir/raku/ch-2.raku @@ -0,0 +1,61 @@ +#!/usr/bin/env rak; +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ; +use v6; +use Test; + +=begin comment + +262-2: Count Equal Divisible +Submitted by: Mohammad Sajid Anwar +You are given an array of integers, @ints and an integer $k. + +Write a script to return the number of pairs (i, j) where + +a) 0 <= i < j < size of @int +b) ints[i] == ints[j +c) i x j is divisible by k +Example 1 +Input: @ints = (3,1,2,2,2,1,3) and $k = 2 +Output: 4 + +(0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2 +(2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2 +(2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2 +(3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2 +Example 2 +Input: @ints = (1,2,3) and $k = 2 +Output: 0 +=end comment + +my @Test = + # $exp @in $k + 4, (3,1,2,2,2,1,3), 2, + 0, (1,2,3), 1, + 0, (), 1, + 0, (1,), 0, + 3, (0,0,0,), 1, +; +plan @Test ÷ 3; + +multi func( Int $k, @in where *.end == -1 ) { 0 } +multi func( Int:D $k, @in) { + # a) 0 <= i < j < size of @ints + #b) ints[i] == ints[j] + #c) i x j is divisible by k + (^@in).combinations(2).grep( { + ( .[0] < .[1] ) + and ( .[0] × .[1] %% $k ) + and ( @in[ $_[0]] == @in[ $_[1]] ) + }).Int; +} + +for @Test -> $exp, @in, $k { + is func($k, @in), $exp, "$exp <- $k «- @in.raku()"; +} + +done-testing; +my @in = ( 1,1,1,1,1,3,3,3); +my $j = 3; +say "Input: @ints = @in[] and \$k = $j\n Output: &func($j, @in)" + + |
