diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-24 13:22:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-24 13:22:06 +0100 |
| commit | d11e92eddcc8e699d4f1634d7ff9ebfe569b1395 (patch) | |
| tree | 3c4825584645d9928635866a8cbaae3b9df27a89 | |
| parent | 33190a3cba859dbe3634751588002f1b4008850e (diff) | |
| parent | 922786e8a930793ab4f6fb63c616c6eb15ad1933 (diff) | |
| download | perlweeklychallenge-club-d11e92eddcc8e699d4f1634d7ff9ebfe569b1395.tar.gz perlweeklychallenge-club-d11e92eddcc8e699d4f1634d7ff9ebfe569b1395.tar.bz2 perlweeklychallenge-club-d11e92eddcc8e699d4f1634d7ff9ebfe569b1395.zip | |
Merge pull request #10685 from 0rir/work
283
| -rw-r--r-- | challenge-283/0rir/raku/ch-1.raku | 62 | ||||
| -rw-r--r-- | challenge-283/0rir/raku/ch-2.raku | 65 |
2 files changed, 127 insertions, 0 deletions
diff --git a/challenge-283/0rir/raku/ch-1.raku b/challenge-283/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..741fdf5a76 --- /dev/null +++ b/challenge-283/0rir/raku/ch-1.raku @@ -0,0 +1,62 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment + +283-1: Unique Number Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints, where every element appears +more than once except one element. + +Write a script to find the one element that appears exactly one time. + +Example 1 +Input: @ints = (3, 3, 1) +Output: 1 +Example 2 +Input: @ints = (3, 2, 4, 2, 4) +Output: 3 +Example 3 +Input: @ints = (1) +Output: 1 +Example 4 +Input: @ints = (4, 3, 1, 1, 1, 4) +Output: 3 +=end comment + +my @Test = + # in exp + (3, 3, 1), 1, + (3, 2, 4, 2, 4), 3, + (1,), 1, + [4, 3, 1, 1, 1, 4], 3, +; + +my @Dead = (), [,], [2,2], ; + +plan @Test + @Dead; + +sub task( List $a) { $a.Bag.first( { .value == 1 }).key; } + +sub task-w-validate( List $a) { + my $l = $a.Bag.List; # stabilize sequencing + my @k = $l.grep( *.value == 1, :k ); + die 'Invalid input.' if @k ~~ Empty or @k > 1; + $l[@k[0]].key; +} + +for @Test -> $in, $exp { + is task($in), $exp, "$exp <- $in.raku()"; + is task-w-validate($in), $exp, "$exp <- $in.raku() valid"; +} +for @Dead -> $dead { + dies-ok { task-w-validate($dead)}, " dead <- $dead.raku()"; +} + +done-testing; + +my @int = (1,2,3,4,5,6,7,8,9,10,11,12,12,11,10,9,8,7,6,5,4,3,2); +say "\nInput: \@ints = @int[]\nOutput: ", task( @int); + diff --git a/challenge-283/0rir/raku/ch-2.raku b/challenge-283/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..a1fc3e0926 --- /dev/null +++ b/challenge-283/0rir/raku/ch-2.raku @@ -0,0 +1,65 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +283-2: Digit Count Value Submitted by: Mohammad Sajid Anwar +You are given an array of positive integers, @ints. + +Write a script to return true if for every index i in the range +0 <= i < size of array, the digit i occurs exactly the $ints[$i] times +in the given array otherwise return false. + +Example 1 +Input: @ints = (1, 2, 1, 0) +Ouput: true + +$ints[0] = 1, the digit 0 occurs exactly 1 time. +$ints[1] = 2, the digit 1 occurs exactly 2 times. +$ints[2] = 1, the digit 2 occurs exactly 1 time. +$ints[3] = 0, the digit 3 occurs 0 time. +Example 2 +Input: @ints = (0, 3, 0) +Ouput: false + +=end comment + +my @Test = + (1).comb.List, False, + (0).comb.List, False, + (10).comb.List, False, + (212).comb.List, False, + (030).comb.List, False, + (3000).comb.List, False, + (40000).comb.List, False, + (31000).comb.List, False, + (3312000).comb.List, False, + (4301000).comb.List, False, + (9122000001).comb.List, False, + (1210.comb.List), True, + (2020.comb.List), True, + (21200.comb.List), True, + (3211000.comb.List), True, + (521001000.comb.List), True, + (6210001000.comb.List), True, + (72100001000.comb.List), True, + (821000001000.comb.List), True, + (9210000001000.comb.List), True, +; +plan @Test ÷ 2; + +sub task( $a) { + for ^$a -> \i { + return False unless $a.grep( i ) == $a[ i]; + } + True; +} + +for @Test -> $in, $exp { + is task($in), $exp, "$exp <- $in"; +} +done-testing; + +my @int = (4, 1, 0, 0, 0, 0); +say "\nInput: @ints = @int.raku()\nOuput: { task @int }"; |
