From 73763579c2fe8b3882bcf72dc71053f57c3e7570 Mon Sep 17 00:00:00 2001 From: rir Date: Sat, 11 May 2024 13:31:05 -0400 Subject: 268 --- challenge-268/0rir/raku/ch-1.raku | 73 +++++++++++++++++++++++++++++++++++++++ challenge-268/0rir/raku/ch-2.raku | 57 ++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 challenge-268/0rir/raku/ch-1.raku create mode 100644 challenge-268/0rir/raku/ch-2.raku diff --git a/challenge-268/0rir/raku/ch-1.raku b/challenge-268/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..a3562c84f1 --- /dev/null +++ b/challenge-268/0rir/raku/ch-1.raku @@ -0,0 +1,73 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴ +use v6.d; +use Test; + +=begin comment +268-1 Task 1: Magic Number Submitted by: Mohammad Sajid Anwar +You are given two arrays of integers of same size, @x and @y. + +Write a script to find the magic number that when added to each +elements of one of the array gives the second array. Elements order +is not important. + +Example 1 +Input: @x = (3, 7, 5) + @y = (9, 5, 7) +Output: 2 + +The magic number is 2. +@x = (3, 7, 5) + + 2 2 2 +@y = (5, 9, 7) +Example 2 +Input: @x = (1, 2, 1) + @y = (5, 4, 4) +Output: 3 + +The magic number is 3. +@x = (1, 2, 1) + + 3 3 3 +@y = (5, 4, 4) +Example 3 +Input: @x = (2) + @y = (5) +Output: 3 + +=end comment + +my @Test = + # ary ary magic_num + (3, 7, 5), (9, 5, 7), 2, + (1, 2, 1), (5, 4, 4), 3, + (2,), (5,), 3, + (2,5), (2,5), 0, + (2,5), (1,9), Int, + (), (), Int, +; +my @Die = + (1,0), (2,), + (), (1,2), + (1,2), (), +; +plan @Test ÷ 3 + @Die ÷ 2; + +multi func( @a where * ~~ [], @b where * ~~ [] --> Int ) { Int } +multi func( @a, @b where +* == +@a --> Int) { + my @candi = (@a.sort [Z-] @b.sort); + return (@candi.all == @candi[0]) + ?? @candi[0].Int.abs + !! Int; +} + +for @Test -> @a, @b, $exp { + is func(@a, @b), $exp, ($exp // "(Int)") ~ " ~~\t @a[] <=- @b[]"; +} + +for @Die -> @a, @b { + dies-ok { func(@a, @b) }, "die ~~\t @a[] <=- @b[]"; +} +done-testing; + +exit; + diff --git a/challenge-268/0rir/raku/ch-2.raku b/challenge-268/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..31208a54f5 --- /dev/null +++ b/challenge-268/0rir/raku/ch-2.raku @@ -0,0 +1,57 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴ +use v6.d; +use Test; + +=begin comment +268-2: Number Game Submitted by: Mohammad Sajid Anwar +You are given an array of integers, @ints, with even number of elements. + +Write a script to create a new array made up of elements of the given array. +Pick the two smallest integers and add it to new array in decreasing order +i.e. high to low. Keep doing until the given array is empty. + +Example 1 +Input: @ints = (2, 5, 3, 4) +Output: (3, 2, 5, 4) + +Round 1: we picked (2, 3) and push it to the new array (3, 2) +Round 2: we picked the remaining (4, 5) and push it to the new array (5, 4) +Example 2 +Input: @ints = (9, 4, 1, 3, 6, 4, 6, 1) +Output: (1, 1, 4, 3, 6, 4, 9, 6) +Example 3 +Input: @ints = (1, 2, 2, 3) +Output: (2, 1, 3, 2) +=end comment + +my @Test = + # in out + (2, 5, 3, 4), (3,2, 5,4), + (9, 4, 1, 3, 6, 4, 6, 1), (1,1, 4,3, 6,4, 9,6), + (1, 2, 2, 3), (2,1, 3,2), + (), (), + (0,), (0,), + (0, 0), (0,0), + (0, 0, 0), (0,0, 0), + (9, 4, 1, 3, 6, 4, 6, 1, 0), (1,0, 3,1, 4,4, 6,6, 9), + (0,9, 4, 1, 3, 6, 4, 6, 1, 0), (0,0, 1,1, 4,3, 6,4, 9,6), +; + +plan @Test ÷ 2; + +sub func( @a is copy ) { + @a.=sort; + my @ret; # using two shift as Opt just two ptr moves but not tested + while @a > 1 { @ret.append: (@a.shift, @a.shift).sort.reverse } + if @a { @ret.append: @a.pop } + @ret; +} + +for @Test -> @in, @out { + func( @in); +} +for @Test -> @in, @exp { + is func(@in), @exp, "@exp[] <- @in.sort()"; +} +done-testing; -- cgit