From 67d2e3a9f14c7e969a3952bed7e01ecbb852dd12 Mon Sep 17 00:00:00 2001 From: rir Date: Thu, 30 Jan 2025 22:51:27 -0500 Subject: 306 --- challenge-306/0rir/raku/ch-1.raku | 56 +++++++++++++++++++++++++ challenge-306/0rir/raku/ch-2.raku | 86 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 challenge-306/0rir/raku/ch-1.raku create mode 100644 challenge-306/0rir/raku/ch-2.raku diff --git a/challenge-306/0rir/raku/ch-1.raku b/challenge-306/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..f6d1937fa5 --- /dev/null +++ b/challenge-306/0rir/raku/ch-1.raku @@ -0,0 +1,56 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴ +use v6.d; +use Test; + +=begin comment +306-1: Odd Sum Submitted by: Mohammad Sajid Anwar +You are given an array of positive integers, @ints. +Write a script to return the sum of all possible odd-length subarrays of the given array. A subarray is a contiguous subsequence of the array. + +Example 1 +Input: @ints = (2, 5, 3, 6, 4) +Output: 77 + +Odd length sub-arrays: +(2) => 2 +(5) => 5 +(3) => 3 +(6) => 6 +(4) => 4 +(2, 5, 3) => 10 +(5, 3, 6) => 14 +(3, 6, 4) => 13 +(2, 5, 3, 6, 4) => 20 + +Sum => 2 + 5 + 3 + 6 + 4 + 10 + 14 + 13 + 20 => 77 +Example 2 +Input: @ints = (1, 3) +Output: 4 +=end comment + +my @Test = + 77, (2, 5, 3, 6, 4), + 4, (1, 3), + 4, (4,), + 0, (), +; + +plan @Test ÷ 2; + +multi task( [] ) { 0 } +multi task( @a --> Int) { + sum do for 1, *+2 … @a % 2 ?? +@a !! @a -1 { + @a.rotor($_ => -$_ +1 ).flat.sum; + } +} + +for @Test -> $exp, @in { + is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()" +} +done-testing; + +my @int = (2, 5, 3, 6, 4, 10); +say "\nInput: @int = @int[]\nOutput: ", task( @int); + + diff --git a/challenge-306/0rir/raku/ch-2.raku b/challenge-306/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..ade054fab6 --- /dev/null +++ b/challenge-306/0rir/raku/ch-2.raku @@ -0,0 +1,86 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴ +use v6.d; +use Test; + +=begin comment +Task 2: Last Element +Submitted by: Mohammad Sajid Anwar +You are given a array of integers, @ints. + +Write a script to play a game where you pick two biggest integers in the given array, say x and y. Then do the following: + +a) if x == y then remove both from the given array +b) if x != y then remove x and replace y with (y - x) +At the end of the game, there is at most one element left. + +Return the last element if found otherwise return 0. + +Example 1 +Input: @ints = (3, 8, 5, 2, 9, 2) +Output: 1 + +Step 1: pick 8 and 9 => (3, 5, 2, 1, 2) +Step 2: pick 3 and 5 => (2, 2, 1, 2) +Step 3: pick 2 and 1 => (1, 2, 2) +Step 4: pick 2 and 1 => (1, 2) +Step 5: pick 1 and 2 => (1) +Example 2 +Input: @ints = (3, 2, 5) +Output: 0 + +Step 1: pick 3 and 5 => (2, 2) +Step 2: pick 2 and 2 => () +=end comment + +=begin comment + Although the game cannot generate a zed, it is a possible ending + value. So I am maintaining the difference between empty and 0. +=end comment + + +my @Test = + Int, ()».Int, + 0, (0,)».Int, + 3, (3,)».Int, + Int, (0,0)».Int, + Int, ( -1, -1)».Int, + 0, (0,0,0)».Int, + Int, (3, 2, 5)».Int, + Int, (3,3,3,3)».Int, + 1, (1,1, 39 ,40)».Int, + 3, (3,3,3,3,3)».Int, + 1, (3, 8, 5, 2, 9, 2)».Int, + 7, (0, 0, 0, 0, 0, 7)».Int, +; +plan @Test ÷ 2; + + +only task( @a --> Int) { + my $h = @a.BagHash; + my Int $max; + my Int $max-jr; + + loop { + return Int if not $h; # ().BagHash + + # Handle the max level. + $max = $h.keys.max; + next if $h{$max} :delete %% 2; # Just delete it. + + return $max if not $h; # No next level. + + $max-jr = $h.keys.max; # Calc & store difference. + -- $h{ $max-jr}; + $h.add( $max - $max-jr ); + } +} + +for @Test -> $exp, @in { + is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()" +} +done-testing; + +my Int @int = 3, 8, 5, 2, 9, 1; +say "\nInput: @int = @int[]\nOutput: ", task(@int) // 0; + -- cgit From 8e2d10b3db8aa23569e02582c79be2e28fc1ae7f Mon Sep 17 00:00:00 2001 From: rir Date: Thu, 30 Jan 2025 23:03:06 -0500 Subject: 306 --- challenge-306/0rir/raku/ch-1.raku | 4 ++-- challenge-306/0rir/raku/ch-2.raku | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/challenge-306/0rir/raku/ch-1.raku b/challenge-306/0rir/raku/ch-1.raku index f6d1937fa5..e40bf11f01 100644 --- a/challenge-306/0rir/raku/ch-1.raku +++ b/challenge-306/0rir/raku/ch-1.raku @@ -40,8 +40,8 @@ plan @Test ÷ 2; multi task( [] ) { 0 } multi task( @a --> Int) { - sum do for 1, *+2 … @a % 2 ?? +@a !! @a -1 { - @a.rotor($_ => -$_ +1 ).flat.sum; + sum do for 1, *+2 … $ = @a % 2 ?? +@a !! @a -1 { + sum @a.rotor($_ => -$_ +1 ).flat; } } diff --git a/challenge-306/0rir/raku/ch-2.raku b/challenge-306/0rir/raku/ch-2.raku index ade054fab6..360a462676 100644 --- a/challenge-306/0rir/raku/ch-2.raku +++ b/challenge-306/0rir/raku/ch-2.raku @@ -62,22 +62,23 @@ only task( @a --> Int) { my Int $max-jr; loop { - return Int if not $h; # ().BagHash + return Int if not $h; # Done if empty. # Handle the max level. $max = $h.keys.max; - next if $h{$max} :delete %% 2; # Just delete it. + next if $h{$max} :delete %% 2; return $max if not $h; # No next level. - $max-jr = $h.keys.max; # Calc & store difference. + # Grab next max, delete it, and store the differ𝑒nce. + $max-jr = $h.keys.max; -- $h{ $max-jr}; - $h.add( $max - $max-jr ); + $h.add( $max - $max-jr); } } for @Test -> $exp, @in { - is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()" + is task( @in), $exp, "{$exp // $exp.^name()}\t<- @in.raku()" } done-testing; -- cgit