diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-27 23:19:57 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-27 23:19:57 +0000 |
| commit | c1a6044be308718b86b145082167c5265cc8eb93 (patch) | |
| tree | 0c637da49636087a2841e3ed2094e0f26462a79f | |
| parent | a0399e9a066cf505228bb9fb3c0408b3da0712ec (diff) | |
| parent | 88f043a11896e9eb2f6fa999e7253de0a1d6ca3b (diff) | |
| download | perlweeklychallenge-club-c1a6044be308718b86b145082167c5265cc8eb93.tar.gz perlweeklychallenge-club-c1a6044be308718b86b145082167c5265cc8eb93.tar.bz2 perlweeklychallenge-club-c1a6044be308718b86b145082167c5265cc8eb93.zip | |
Merge pull request #12926 from ash/ash-345
Week 345 in Raku by @ash
| -rw-r--r-- | challenge-345/ash/raku/ch-1.raku | 13 | ||||
| -rw-r--r-- | challenge-345/ash/raku/ch-2.raku | 34 |
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-345/ash/raku/ch-1.raku b/challenge-345/ash/raku/ch-1.raku new file mode 100644 index 0000000000..bc53c65653 --- /dev/null +++ b/challenge-345/ash/raku/ch-1.raku @@ -0,0 +1,13 @@ +# Task 1 of the Weekly Challenge 345 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-345/#TASK1 + +say peaks 1, 3, 2; # 1 +say peaks 2, 4, 6, 5, 3; # 2 +say peaks 1, 2, 3, 2, 4, 1; # 2 4 +say peaks 5, 3, 1; # 0 +say peaks 1, 5, 1, 5, 1, 5, 1; # 1 3 5 + +sub peaks(*@data) { + my @indices = 1 .. (@data.elems - 2); + return (@indices.grep: {@data[$_ - 1] < @data[$_] > @data[$_ + 1]}) || (0,); +} diff --git a/challenge-345/ash/raku/ch-2.raku b/challenge-345/ash/raku/ch-2.raku new file mode 100644 index 0000000000..86db3a5ded --- /dev/null +++ b/challenge-345/ash/raku/ch-2.raku @@ -0,0 +1,34 @@ +# Task 2 of the Weekly Challenge 345 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-345/#TASK2 + +say process 5, -1, -1; # 5 -1 +say process 3, 7, -1, -1, -1; # 7 3 -1 +say process 2, -1, 4, -1, -1; # 2 4 2 +say process 10, 20, -1, 30, -1, -1; # 20 30 20 +say process -1, -1, 5, -1; # -1 -1 5 + +sub process(*@data) { + my @seen; + my @ans; + + my $x = 0; + + for @data -> $val { + if $val > 0 { + @seen.unshift($val); + $x = 0; # this was not clear from the task + } + elsif $val == -1 { + if $x < @seen.elems { + @ans.push(@seen[$x]); + } + else { + @ans.push(-1); + } + + $x++; + } + } + + return @ans; +} |
