From 88f043a11896e9eb2f6fa999e7253de0a1d6ca3b Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Mon, 27 Oct 2025 09:34:55 +0100 Subject: task 2 --- challenge-345/ash/raku/ch-2.raku | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 challenge-345/ash/raku/ch-2.raku 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; +} -- cgit