diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-15 13:30:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-15 13:30:21 +0100 |
| commit | f2eaec99d481334cf589b49e3df4014ce7f72387 (patch) | |
| tree | 1d97fcab629baccd279a30093a76992bc87a65e5 /challenge-073 | |
| parent | ab9d9549997cc8b650785a9b3e6e560e793544ae (diff) | |
| parent | 4bd33369d130e4272b1fea628c89dcca0223e471 (diff) | |
| download | perlweeklychallenge-club-f2eaec99d481334cf589b49e3df4014ce7f72387.tar.gz perlweeklychallenge-club-f2eaec99d481334cf589b49e3df4014ce7f72387.tar.bz2 perlweeklychallenge-club-f2eaec99d481334cf589b49e3df4014ce7f72387.zip | |
Merge pull request #2076 from noudald/challenge-073-noud
Solution for challenge 073 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-073')
| -rw-r--r-- | challenge-073/noud/raku/ch-1.p6 | 23 | ||||
| -rw-r--r-- | challenge-073/noud/raku/ch-2.p6 | 36 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-073/noud/raku/ch-1.p6 b/challenge-073/noud/raku/ch-1.p6 new file mode 100644 index 0000000000..c2a9cdf67e --- /dev/null +++ b/challenge-073/noud/raku/ch-1.p6 @@ -0,0 +1,23 @@ +# You are given an array of integers @A and sliding window size $S. +# +# Write a script to create an array of min from each sliding window. +# Example +# Input: @A = (1, 5, 0, 2, 9, 3, 7, 6, 4, 8) and $S = 3 +# Output: (0, 0, 0, 2, 3, 3, 4, 4) +# +# [(1 5 0) 2 9 3 7 6 4 8] = Min (0) +# [1 (5 0 2) 9 3 7 6 4 8] = Min (0) +# [1 5 (0 2 9) 3 7 6 4 8] = Min (0) +# [1 5 0 (2 9 3) 7 6 4 8] = Min (2) +# [1 5 0 2 (9 3 7) 6 4 8] = Min (3) +# [1 5 0 2 9 (3 7 6) 4 8] = Min (3) +# [1 5 0 2 9 3 (7 6 4) 8] = Min (4) +# [1 5 0 2 9 3 7 (6 4 8)] = Min (4) + +sub min-sliding-window(@A, $S) { + gather for 0..@A.elems-$S -> $i { + take min(@A[$i..$i+$S-1]); + } +} + +min-sliding-window((1, 5, 0, 2, 9, 3, 7, 6, 4, 8), 3).say; diff --git a/challenge-073/noud/raku/ch-2.p6 b/challenge-073/noud/raku/ch-2.p6 new file mode 100644 index 0000000000..df045ca322 --- /dev/null +++ b/challenge-073/noud/raku/ch-2.p6 @@ -0,0 +1,36 @@ +# You are given an array of integers @A. +# +# Write a script to create an array that represents the smallest element to the +# left of each corresponding index. If none found then use 0. +# Example 1 +# Input: @A = (7, 8, 3, 12, 10) +# Output: (0, 7, 0, 3, 3) +# +# For index 0, the smallest number to the left of $A[0] i.e. 7 is none, so we put 0. +# For index 1, the smallest number to the left of $A[1] as compare to 8, in (7) is 7 so we put 7. +# For index 2, the smallest number to the left of $A[2] as compare to 3, in (7, 8) is none, so we put 0. +# For index 3, the smallest number to the left of $A[3] as compare to 12, in (7, 8, 3) is 3, so we put 3. +# For index 4, the smallest number to the left of $A[4] as compare to 10, in (7, 8, 3, 12) is 3, so we put 3 again. +# +# Example 2 +# Input: @A = (4, 6, 5) +# Output: (0, 4, 4) +# +# For index 0, the smallest number to the left of $A[0] is none, so we put 0. +# For index 1, the smallest number to the left of $A[1] as compare to 6, in (4) is 4, so we put 4. +# For index 2, the smallest number to the left of $A[2] as compare to 5, in (4, 6) is 4, so we put 4 again. + +sub smallest-neighbour(@A) { + my $smallest = @A[0] + 1; + gather for @A -> $a { + if ($smallest <= $a) { + take $smallest; + } else { + $smallest = $a; + take 0; + } + } +} + +smallest-neighbour((7, 8, 3, 12, 10)).say; +smallest-neighbour((4, 6, 5)).say; |
