diff options
| author | Andinus <andinus@nand.sh> | 2022-04-12 22:32:31 +0530 |
|---|---|---|
| committer | Andinus <andinus@nand.sh> | 2022-04-12 22:32:31 +0530 |
| commit | b8e0cde01c4aa44ce74ee74d81b6feb91534893b (patch) | |
| tree | aae2c2f43dbd00e8b61775b16f63975691096c8e /challenge-160 | |
| parent | 6e220136f3cd0b6189d3ea501127ae7b8531b0c6 (diff) | |
| download | perlweeklychallenge-club-b8e0cde01c4aa44ce74ee74d81b6feb91534893b.tar.gz perlweeklychallenge-club-b8e0cde01c4aa44ce74ee74d81b6feb91534893b.tar.bz2 perlweeklychallenge-club-b8e0cde01c4aa44ce74ee74d81b6feb91534893b.zip | |
Add challenge-160
Diffstat (limited to 'challenge-160')
| -rw-r--r-- | challenge-160/andinus/README | 110 | ||||
| -rw-r--r-- | challenge-160/andinus/README.org | 101 | ||||
| -rw-r--r-- | challenge-160/andinus/raku/ch-1.raku | 16 | ||||
| -rw-r--r-- | challenge-160/andinus/raku/ch-2.raku | 11 |
4 files changed, 214 insertions, 24 deletions
diff --git a/challenge-160/andinus/README b/challenge-160/andinus/README index bd1dd6e3ce..cb7306aa47 100644 --- a/challenge-160/andinus/README +++ b/challenge-160/andinus/README @@ -1,51 +1,113 @@ ━━━━━━━━━━━━━━━ - CHALLENGE 135 + CHALLENGE 160 Andinus ━━━━━━━━━━━━━━━ - 2021-10-22 + 2022-04-12 -Task 1 - Middle 3-digits -════════════════════════ +Task 1 - Four Is Magic +══════════════════════ - You are given an integer. + You are given a positive number, $n < 10. - Write a script find out the middle 3-digits of the given integer, if - possible otherwise throw sensible error. + Write a script to generate english text sequence starting with the + English cardinal representation of the given number, the word ‘is’ and + then the English cardinal representation of the count of characters + that made up the first word, followed by a comma. Continue until you + reach four. ┌──── - │ Input: $n = 1234567 - │ Output: 345 + │ Input: $n = 5 + │ Output: Five is four, four is magic. │ - │ Input: $n = -123 - │ Output: 123 + │ Input: $n = 7 + │ Output: Seven is five, five is four, four is magic. │ - │ Input: $n = 1 - │ Output: too short + │ Input: $n = 6 + │ Output: Six is three, three is five, five is four, four is magic. + └──── + + +Raku +──── + + Take a positive number, less than 10 as input from MAIN. Then we + define an array that holds the string representation of integers. The + `multi sub' `four-is-magic' is called on the input. It runs + recursively until `4' is called. + + `.tc' is called on the result to make the first character uppercase. + + ┌──── + │ unit sub MAIN( + │ UInt $n where * < 10, #= positive number, less than 10 + │ ); + │ + │ my @num-to-str = <zero one two three four five six seven eight nine>; + │ + │ multi sub four-is-magic(4 --> Str) { + │ return "four is magic."; + │ } + │ + │ multi sub four-is-magic(Int $n where * < 10 --> Str) { + │ my $n-str = @num-to-str[$n]; + │ return "$n-str is { @num-to-str[$n-str.chars] }, " ~ four-is-magic($n-str.chars); + │ } │ - │ Input: $n = 10 - │ Output: even number of digits + │ put four-is-magic($n).tc; + └──── + + +Task 2 - Equilibrium Index +══════════════════════════ + + You are give an array of integers, @n. + + Write a script to find out the Equilibrium Index of the given array, + if found. + + For an array A consisting n elements, index i is an + equilibrium index if the sum of elements of subarray + A[0…i-1] is equal to the sum of elements of subarray + A[i+1…n-1]. + + ┌──── + │ Input: @n = (1, 3, 5, 7, 9) + │ Output: 3 + │ + │ Input: @n = (1, 2, 3, 4, 5) + │ Output: -1 as no Equilibrium Index found. + │ + │ Input: @n = (2, 4, 2) + │ Output: 1 └──── Raku ──── - Input's absolute value is taken because the sign is meaningless here. - To get middle 3-digits we take 3 digits from `$n.chars div 2 - 1' - position, `-1' because Arrays are 0-indexed. It's guaranteed that we - have odd number of digits so `div 2' will land us on left of middle - digit, we just take 3 digits from there. + Takes an array of integers as input. Then it loops over the array by + index and does as the problem states, takes sum of all elements before + the index and compares it with the sum of all elements after the + index, if they're equal it prints the index and exits. If there is no + Equilibrium Index then it prints -1. ┌──── - │ $n = abs $n; - │ die "too short" if $n.chars < 3; - │ die "even number of digits" if $n.chars %% 2; - │ put $n.substr($n.chars div 2 - 1, 3); + │ unit sub MAIN( + │ *@n, #= array of integers + │ ); + │ + │ for 0 .. @n.end -> $i { + │ if @n[0 .. $i - 1].sum == @n[$i + 1 .. *].sum { + │ put $i; + │ exit; + │ } + │ } + │ put -1; └──── diff --git a/challenge-160/andinus/README.org b/challenge-160/andinus/README.org new file mode 100644 index 0000000000..d4ae1142fa --- /dev/null +++ b/challenge-160/andinus/README.org @@ -0,0 +1,101 @@ +#+title: Challenge 160 +#+date: 2022-04-12 +#+html_link_up: ../ +#+export_file_name: index +#+options: toc:nil +#+setupfile: ~/.emacs.d/org-templates/level-2.org + +* Task 1 - Four Is Magic + +You are given a positive number, $n < 10. + +Write a script to generate english text sequence starting with the +English cardinal representation of the given number, the word ‘is’ and +then the English cardinal representation of the count of characters that +made up the first word, followed by a comma. Continue until you reach +four. + +#+begin_src +Input: $n = 5 +Output: Five is four, four is magic. + +Input: $n = 7 +Output: Seven is five, five is four, four is magic. + +Input: $n = 6 +Output: Six is three, three is five, five is four, four is magic. +#+end_src + +** Raku + +Take a positive number, less than 10 as input from MAIN. Then we define +an array that holds the string representation of integers. The ~multi sub~ +~four-is-magic~ is called on the input. It runs recursively until ~4~ is +called. + +~.tc~ is called on the result to make the first character uppercase. + +#+begin_src raku +unit sub MAIN( + UInt $n where * < 10, #= positive number, less than 10 +); + +my @num-to-str = <zero one two three four five six seven eight nine>; + +multi sub four-is-magic(4 --> Str) { + return "four is magic."; +} + +multi sub four-is-magic(Int $n where * < 10 --> Str) { + my $n-str = @num-to-str[$n]; + return "$n-str is { @num-to-str[$n-str.chars] }, " ~ four-is-magic($n-str.chars); +} + +put four-is-magic($n).tc; +#+end_src + +* Task 2 - Equilibrium Index + +You are give an array of integers, @n. + +Write a script to find out the Equilibrium Index of the given array, if +found. + +#+begin_quote +For an array A consisting n elements, index i is an equilibrium index if +the sum of elements of subarray A[0…i-1] is equal to the sum of elements +of subarray A[i+1…n-1]. +#+end_quote + +#+begin_src +Input: @n = (1, 3, 5, 7, 9) +Output: 3 + +Input: @n = (1, 2, 3, 4, 5) +Output: -1 as no Equilibrium Index found. + +Input: @n = (2, 4, 2) +Output: 1 +#+end_src + +** Raku + +Takes an array of integers as input. Then it loops over the array by +index and does as the problem states, takes sum of all elements before +the index and compares it with the sum of all elements after the index, +if they're equal it prints the index and exits. If there is no +Equilibrium Index then it prints -1. + +#+begin_src raku +unit sub MAIN( + *@n, #= array of integers +); + +for 0 .. @n.end -> $i { + if @n[0 .. $i - 1].sum == @n[$i + 1 .. *].sum { + put $i; + exit; + } +} +put -1; +#+end_src diff --git a/challenge-160/andinus/raku/ch-1.raku b/challenge-160/andinus/raku/ch-1.raku new file mode 100644 index 0000000000..7ab3307848 --- /dev/null +++ b/challenge-160/andinus/raku/ch-1.raku @@ -0,0 +1,16 @@ +unit sub MAIN( + UInt $n where * < 10, #= positive number, less than 10 +); + +my @num-to-str = <zero one two three four five six seven eight nine>; + +multi sub four-is-magic(4 --> Str) { + return "four is magic."; +} + +multi sub four-is-magic(Int $n where * < 10 --> Str) { + my $n-str = @num-to-str[$n]; + return "$n-str is { @num-to-str[$n-str.chars] }, " ~ four-is-magic($n-str.chars); +} + +put four-is-magic($n).tc; diff --git a/challenge-160/andinus/raku/ch-2.raku b/challenge-160/andinus/raku/ch-2.raku new file mode 100644 index 0000000000..a9045a2712 --- /dev/null +++ b/challenge-160/andinus/raku/ch-2.raku @@ -0,0 +1,11 @@ +unit sub MAIN( + *@n, #= array of integers +); + +for 0 .. @n.end -> $i { + if @n[0 .. $i - 1].sum == @n[$i + 1 .. *].sum { + put $i; + exit; + } +} +put -1; |
