diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-30 10:22:53 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-30 10:22:53 +0000 |
| commit | 79cb3c4dd66f650bf19d218ea0b9c47bb005fc4d (patch) | |
| tree | d3089edeb71c43978c535ce12cb8b2296c88d077 | |
| parent | 65c09e5b2b05b300c933f2ff8d5bc1e44981b930 (diff) | |
| parent | 2f8167b48bc84f6578f9113bbe1943bc93724b61 (diff) | |
| download | perlweeklychallenge-club-79cb3c4dd66f650bf19d218ea0b9c47bb005fc4d.tar.gz perlweeklychallenge-club-79cb3c4dd66f650bf19d218ea0b9c47bb005fc4d.tar.bz2 perlweeklychallenge-club-79cb3c4dd66f650bf19d218ea0b9c47bb005fc4d.zip | |
Merge pull request #12944 from 0rir/work
345
| -rw-r--r-- | challenge-345/0rir/raku/ch-1.raku | 74 | ||||
| -rw-r--r-- | challenge-345/0rir/raku/ch-2.raku | 73 |
2 files changed, 147 insertions, 0 deletions
diff --git a/challenge-345/0rir/raku/ch-1.raku b/challenge-345/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..97376c4e51 --- /dev/null +++ b/challenge-345/0rir/raku/ch-1.raku @@ -0,0 +1,74 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ 🐧 +use v6.d; +use Test; + +=begin comment +Edited for space. +345-Task 1: Peak Positions +Submitted by: Mohammad Sajid Anwar +You are given an array of integers, @ints. + +Find all the peaks in the array, a peak is an element that is strictly greater than its left and right neighbours. Return the indices of all such peak positions. +=end comment + +my @Test = + + # in exp + (1, 3, 2), (1,), + (2, 4, 6, 5, 3), (2,), + (1, 2, 3, 2, 4, 1), (2, 4), + (5, 3, 1), (0,), + (1, 5, 1, 5, 1, 5, 1), (1, 3, 5), + + (5, 1, 5, 1, 5, 1, 5), (0, 2, 4, 6), + (1, 1), (), + (1, 0), (0,), + (0, 1), (1,), + (3, 1, 2), (0, 2), + (1, 1, 2), (2,), + (3, 1, 1), (0,), + (1, 1, 1), (), + (1, 1, 1, 1), (), + (1, 1, 1, 1, 1), (), + (1, 1, 1, 1, 1, 1), (), + (1, 1, 1, 1, 1, 1, 1), (), + ( 1 xx 500 ), (), + ( (1,0) xx 500).flat, ( 0,2,4 … 998), + ( (0,1) xx 50_000).flat, ( 1,3,5 … 99_999), +; +plan +@Test ÷ 2; + +multi task( @a -->Array) { + my @ret; + + @ret.unshift( 0) if @a[0] > @a[ 1]; + + my @trio = @a.rotor( 3 => -2); + my $idx = 0; + for @trio -> @t { + ++$idx; + if @t[0] < @t[1] > @t[2] { + @ret.push: $idx; + } + } + @ret.push( @a.end) if @a[*-1] > @a[*-2]; + return @ret; +} + +for @Test -> @in, @exp { + if @in < 20 { + is task( @in), @exp, "@exp.raku() <- @in.raku()"; + } else { + is task( @in), @exp, + "@exp.raku.substr(0,20) … " + ~ "<- @in.raku.substr(0,20) … @in.elems() elems"; + } +} +done-testing; + +my @int = (1, 5, 1, 5, 1, 5, 1); +say "\nInput: @int = (", @int.join( ', '), ")", + "\nOutput: ( ", task(@int).join(', '), ")"; + +=finish diff --git a/challenge-345/0rir/raku/ch-2.raku b/challenge-345/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..9fc6af800b --- /dev/null +++ b/challenge-345/0rir/raku/ch-2.raku @@ -0,0 +1,73 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ 🐧 +use v6.d; +use Test; + +=begin comment +Edited for space. +345-Task 2: Last Visitor Submitted by: Mohammad Sajid Anwar +You are given an integer array @ints where each element is either a positive integer or -1. + +We process the array from left to right while maintaining two lists: + +@seen: stores previously seen positive integers (newest at the front) +@ans: stores the answers for each -1 +Rules: + +If $ints[i] is a positive number -> insert it at the front of @seen +If $ints[i] is -1: +Let $x be how many -1s in a row we’ve seen before this one. + +If $x < len(@seen) -> append seen[x] to @ans + +Else -> append -1 to @ans + +At the end, return @ans. + +=end comment + +my @Test = + (5, -1, -1), (5, -1), + (3, 7, -1, -1, -1), (7, 3, -1), + (2, -1, 4, -1, -1), (2, 4, 2), + (10, 20, -1, 30, -1, -1), (20, 30, 20), + (-1, -1, 5, -1), (-1, -1, 5), + + (1,2,3), (), + (-1,), (-1,), + (-1,-1,-1), (-1,-1,-1), +; +plan +@Test ÷ 2; + +my subset PosInt of Int where { $_ > 0} + +sub task( @a) { + my (@seen, @ret); + my $x = -1; + + for @a -> $e { + given $e { + when PosInt { + @seen.unshift: $e; + $x = -1; + } + when -1 { + @ret.push: ++$x < @seen ?? @seen[$x] !! -1; + } + default { + die "Value '$e' is illegal"; + } + } + } + @ret; +} + +for @Test -> @in, @exp { + is task( @in), @exp, "{@exp.raku // @exp.^name()} <- @in.raku()"; +} +done-testing; + +my @int = (2, -1, 4, -1, -1); + +say "\nInput: @int = ({@int.join( ', ')})" + ~ "\nOutput: ({task(@int).join: ', '})"; |
