diff options
| author | PJ Durai <pj.durai@sap.com> | 2020-11-23 14:28:32 -0800 |
|---|---|---|
| committer | PJ Durai <pj.durai@sap.com> | 2020-11-23 14:28:32 -0800 |
| commit | b11763c9f666936c3e678be2b58a505f06203fef (patch) | |
| tree | 5ed3a3f276174d5a2fc33f7af2ffbbc0201b6255 | |
| parent | cea83925f87e4bbe52ab8c1ce9a2e1b0f2d86611 (diff) | |
| download | perlweeklychallenge-club-b11763c9f666936c3e678be2b58a505f06203fef.tar.gz perlweeklychallenge-club-b11763c9f666936c3e678be2b58a505f06203fef.tar.bz2 perlweeklychallenge-club-b11763c9f666936c3e678be2b58a505f06203fef.zip | |
Raku solutions for Week 88
| -rw-r--r-- | challenge-088/pjdurai/raku/ch-1.pl6 | 36 | ||||
| -rw-r--r-- | challenge-088/pjdurai/raku/ch-2.pl6 | 82 |
2 files changed, 118 insertions, 0 deletions
diff --git a/challenge-088/pjdurai/raku/ch-1.pl6 b/challenge-088/pjdurai/raku/ch-1.pl6 new file mode 100644 index 0000000000..8f3caba5bc --- /dev/null +++ b/challenge-088/pjdurai/raku/ch-1.pl6 @@ -0,0 +1,36 @@ +use Test; + + +# Problem +# Write a script to return an array @M where $M[i] is the product of all elements of @N except the index $N[i] + +# Solution Algorithm. + +# Find the product of ALL elements first. +# Loop through the list and re-divide the overall product by that element to get "product of all but one" effect. + +sub solve(@N){ + my $all-product = [*] @N; + @N.map(-> $n { + $all-product / $n; + }); +} + +multi sub MAIN() { + my @N = (5, 2, 1, 4, 3); + say "Problem : ", @N; + say "Solution : ", solve(@N); + + say ""; + + my @N1 = (2, 1, 4, 3); + say "Problem : ", @N1; + say "Solution : ", solve(@N1); +} + +# Get input from command line. +# Call like: raku ch-1.pl6 5 2 1 4 5 +multi sub MAIN(*@N){ + say "Problem : ", @N; + say "Solution : ", solve(@N); +} diff --git a/challenge-088/pjdurai/raku/ch-2.pl6 b/challenge-088/pjdurai/raku/ch-2.pl6 new file mode 100644 index 0000000000..a2fe17c22e --- /dev/null +++ b/challenge-088/pjdurai/raku/ch-2.pl6 @@ -0,0 +1,82 @@ +use Test; +# Solution algoritm. +# We peel layers (like a cake) from the top, then right, then bottom and left until nothing is left. +# String them together for solution. + +# Solution walk through +# Input: +# 1 2 3 +# 4 5 6 +# 7 8 9 +# +# After taking the top layer, it becomes +# 4 5 6 +# 7 8 9 +# +# Take the right layer off. +# 4 5 +# 7 8 +# +# Take the bootom layer off +# +# 4 5 +# +# Take left layer off. +# 5 +# +# Finally tope layer again +# +# [] +# +# Collect the removed items in a result list + +sub solve(@M) { + + my @directions = |<top right bottom left> xx *; + + my $num-rows = @M.elems; + my $num-cols = @M[0].elems; + + my @result = reduce sub (@acc, $direction){ + given $direction { + when 'top' { + @acc.append(@(@M.shift)); + } + when 'right' { + my @right-col = do for @M -> $r { + $r.pop; + } + @acc.append(@right-col); + } + when 'bottom' { + @acc.append(@M.pop.reverse); + } + when 'left' { + my @left-col = do for @M -> $c { + $c.shift; + } + @acc.append(@left-col.reverse); + } + } + @acc; + }, [], |@directions[0..^($num-rows + $num-cols -1)]; +} + +sub MAIN(){ + my @M = [[ 1, 2, 3, 4 ], + [ 5, 6, 7, 8 ], + [ 9, 10, 11, 12 ], + [ 13, 14, 15, 16 ]]; + say "Problem: ", @M; + say "Solution: ", solve (@M); + + say ""; + + my @M1 = [[ 1, 2, 3 ], + [ 4, 5, 6 ], + [ 7, 8, 9 ]]; + + say "Problem: ", @M1; + say "Solution: ", solve (@M1); +} + |
