diff options
| author | rir <rirans@comcast.net> | 2024-01-17 09:44:51 -0500 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2024-01-17 09:44:51 -0500 |
| commit | 1bba104dd6bf486f8f10699361dceb39bd376744 (patch) | |
| tree | 5b84e89a3d383f030d6c76d5986217348945be12 | |
| parent | 857d573334deaa24866cb695f1e3758a85c0b9d9 (diff) | |
| download | perlweeklychallenge-club-1bba104dd6bf486f8f10699361dceb39bd376744.tar.gz perlweeklychallenge-club-1bba104dd6bf486f8f10699361dceb39bd376744.tar.bz2 perlweeklychallenge-club-1bba104dd6bf486f8f10699361dceb39bd376744.zip | |
252
| -rw-r--r-- | challenge-252/0rir/raku/ch-1.raku | 62 | ||||
| -rw-r--r-- | challenge-252/0rir/raku/ch-2.raku | 59 |
2 files changed, 121 insertions, 0 deletions
diff --git a/challenge-252/0rir/raku/ch-1.raku b/challenge-252/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..978a164dc3 --- /dev/null +++ b/challenge-252/0rir/raku/ch-1.raku @@ -0,0 +1,62 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use Test; + +=begin comment +252-1: Special Numbers Submitted by: Mohammad S Anwar +You are given an array of integers, @ints. Write a script to find the sum +of the squares of all special elements of the given array. + +An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0. +Where n is the length of the given array. Also the array is 1-indexed for the task. + +Example 1 +Input: @ints = (1, 2, 3, 4) +Output: 21 + +There are exactly 3 special elements in the given array: +$ints[1] since 1 divides 4, +$ints[2] since 2 divides 4, and +$ints[4] since 4 divides 4. + +Hence, the sum of the squares of all special elements of given array: +1 * 1 + 2 * 2 + 4 * 4 = 21. +Example 2 +Input: @ints = (2, 7, 1, 19, 18, 3) +Output: 63 + +There are exactly 4 special elements in the given array: +$ints[1] since 1 divides 6, +$ints[2] since 2 divides 6, +$ints[3] since 3 divides 6, and +$ints[6] since 6 divides 6. + +Hence, the sum of the squares of all special elements of given array: +2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63 +=end comment + +my @Test = + [,], Int, + [1, 2], 5, + [1, 2, 3, 4], 21, + [2, 7, 1, 19, 18, 3], 63, + [1, 2, 3, 4, 5, 6], 50, + [1, 2, 3, 4, 5, 6, 7], 50, + [1, 2, 3, 4, 5, 6, 7, 8], 85, + [100, 100], 20000, +; +plan @Test ÷ 2; + +multi func( @a where * ~~ Empty) { Int } +multi func( @a ) { + sum @a[ ( ^@a.elems +1).grep( @a.elems %% *).map( * -1)].map: *²; +} + +for @Test -> @in, $exp { + is func(@in), $exp, $exp//"Int" ~ " <- @in.raku()"; +} +done-testing; + +my @int = (2, 7, 1, 19, 18, 3); +say "\nInput: @int = @int[]\nOutput: ",func(@int); diff --git a/challenge-252/0rir/raku/ch-2.raku b/challenge-252/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..8e7cc2583f --- /dev/null +++ b/challenge-252/0rir/raku/ch-2.raku @@ -0,0 +1,59 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use Test; + +=begin comment +252-2: Unique Sum Zero Submitted by: Mohammad S Anwar + +You are given an integer, $n. Write a script to find an array containing +$n unique integers such that they add up to zero. + +Example 1 +Input: $n = 5 +Output: (-7, -1, 1, 3, 4) + +Two other possible solutions could be as below: +(-5, -1, 1, 2, 3) and (-3, -1, 2, -2, 4). +Example 2 +Input: $n = 3 +Output: (-1, 0, 1) +Example 3 +Input: $n = 1 +Output: (0) + +=end comment + +my @Test = 5, [-2..2], + 11, [-5..5], + 21, [-10..10], +; +my @Die = 500, -6, 0; + +plan +@Test + @Die; + +my $range = 100_000; + +sub func( $n, Int $abs-range = $range) { + die "Domain error" if $abs-range × 2 + 1 < $n; + die "Invalid index" if $n < 1; + my @a = ( -$abs-range..$abs-range).pick( $n-1).Array; ; + @a.push: 0 - @a.sum; + @a +} + +for @Test -> $n, @exp { + my @return = func( $n, ($n div 2)); + is +@return, $n, "Array size"; + is @return.sort.Array, @exp, "Array values"; +} +for @Die -> $n { + my $range = 10; + dies-ok { func $n, $range }, + "$n is invalid (\$range is -$range..$range)"; +} +done-testing; + +my $n = 99; +say "\nInput: \$n = $n\nOutput: ", func( $n ); + |
