diff options
| author | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-08-28 21:40:05 +0200 |
|---|---|---|
| committer | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-08-28 21:40:05 +0200 |
| commit | db0f17932b37ae40786ba07493e448dfa62790d4 (patch) | |
| tree | e85d1d1f80c162abbae8e6a8dcd885f6e83a1de4 /challenge-075 | |
| parent | 72133161c8d8f746d8fd8055de27649d454b81b8 (diff) | |
| download | perlweeklychallenge-club-db0f17932b37ae40786ba07493e448dfa62790d4.tar.gz perlweeklychallenge-club-db0f17932b37ae40786ba07493e448dfa62790d4.tar.bz2 perlweeklychallenge-club-db0f17932b37ae40786ba07493e448dfa62790d4.zip | |
Solution to challenge 075 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-075')
| -rw-r--r-- | challenge-075/noud/raku/ch-1.p6 | 39 | ||||
| -rw-r--r-- | challenge-075/noud/raku/ch-2.p6 | 56 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-075/noud/raku/ch-1.p6 b/challenge-075/noud/raku/ch-1.p6 new file mode 100644 index 0000000000..1d29b5dd71 --- /dev/null +++ b/challenge-075/noud/raku/ch-1.p6 @@ -0,0 +1,39 @@ +# You are given a set of coins @C, assuming you have infinite amount of each +# coin in the set. +# +# Write a script to find how many ways you make sum $S using the coins from the +# set @C. +# Example: +# +# Input: +# @C = (1, 2, 4) +# $S = 6 +# +# Output: 6 +# There are 6 possible ways to make sum 6. +# a) (1, 1, 1, 1, 1, 1) +# b) (1, 1, 1, 1, 2) +# c) (1, 1, 2, 2) +# d) (1, 1, 4) +# e) (2, 2, 2) +# f) (2, 4) + +sub coins-sum(@C, $S) { + if ($S == 0) { + return [(),]; + } + + my @ret = []; + for @C -> $c { + if ($c <= $S) { + for coins-sum(@C, $S - $c) -> @r { + if ($c <= @r.min) { + @ret.push([$c, |(@r)]); + } + } + } + } + return @ret.unique; +} + +coins-sum((1, 2, 4), 6).say; diff --git a/challenge-075/noud/raku/ch-2.p6 b/challenge-075/noud/raku/ch-2.p6 new file mode 100644 index 0000000000..89357985d3 --- /dev/null +++ b/challenge-075/noud/raku/ch-2.p6 @@ -0,0 +1,56 @@ +# You are given an array of positive numbers @A. +# +# Write a script to find the largest rectangle histogram created by the given +# array. BONUS: Try to print the histogram as shown in the example, if +# possible. +# +# Example 1: +# +# Input: @A = (2, 1, 4, 5, 3, 7) +# +# 7 # +# 6 # +# 5 # # +# 4 # # # +# 3 # # # # +# 2 # # # # # +# 1 # # # # # # +# _ _ _ _ _ _ _ +# 2 1 4 5 3 7 +# +# Looking at the above histogram, the largest rectangle (4 x 3) is formed by +# columns (4, 5, 3 and 7). +# Output: 12 +# +# Example 2: +# +# Input: @A = (3, 2, 3, 5, 7, 5) +# +# 7 # +# 6 # +# 5 # # # +# 4 # # # +# 3 # # # # # +# 2 # # # # # # +# 1 # # # # # # +# _ _ _ _ _ _ _ +# 3 2 3 5 7 5 +# +# Looking at the above histogram, the largest rectangle (3 x 5) is formed by +# columns (5, 7 and 5). +# Output: 15 + +# Without the BONUS. ;) + +sub largest-rec-hist(@A) { + my @largest-sub-rec = []; + for 0..(@A.elems - 1) -> $i { + for ($i + 1)..(@A.elems - 1) -> $j { + @largest-sub-rec.push(($j - $i + 1) * @A[$i..$j].min); + } + } + return @largest-sub-rec.max; +} + +largest-rec-hist((2, 1, 4, 5, 3, 7)).say; +largest-rec-hist((3, 2, 3, 5, 7, 5)).say; |
