From f9e7ebe3ce76adf06c0e468954e72c2b29154803 Mon Sep 17 00:00:00 2001 From: Andinus Date: Fri, 22 Oct 2021 20:14:53 +0530 Subject: Add challenge-135 --- challenge-135/andinus/README | 47 ++++++++++++++++++++++-------------- challenge-135/andinus/README.org | 42 ++++++++++++++++++++++++++++++++ challenge-135/andinus/raku/ch-1.raku | 5 ++++ 3 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 challenge-135/andinus/README.org create mode 100644 challenge-135/andinus/raku/ch-1.raku diff --git a/challenge-135/andinus/README b/challenge-135/andinus/README index 1d5171cfa8..bd1dd6e3ce 100644 --- a/challenge-135/andinus/README +++ b/challenge-135/andinus/README @@ -1,40 +1,51 @@ ━━━━━━━━━━━━━━━ - CHALLENGE 134 + CHALLENGE 135 Andinus ━━━━━━━━━━━━━━━ - 2021-10-11 + 2021-10-22 -Task 1 - Pandigital Numbers -═══════════════════════════ +Task 1 - Middle 3-digits +════════════════════════ - Write a script to generate first 5 Pandigital Numbers in base 10. + You are given an integer. - As per the [wikipedia], it says: - - A pandigital number is an integer that in a given base has - among its significant digits each digit used in the base - at least once. + Write a script find out the middle 3-digits of the given integer, if + possible otherwise throw sensible error. + ┌──── + │ Input: $n = 1234567 + │ Output: 345 + │ + │ Input: $n = -123 + │ Output: 123 + │ + │ Input: $n = 1 + │ Output: too short + │ + │ Input: $n = 10 + │ Output: even number of digits + └──── -[wikipedia] Raku ──── - • Program: - - Loop from 1023456789 (first Pandigital Number) and take if it contains - every digit in base 10. + 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. ┌──── - │ put gather for 1023456789 .. ∞ { - │ .take if .comb>>.Int.Set ≡ (0 .. 9).Set; - │ }[^5] + │ $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); └──── diff --git a/challenge-135/andinus/README.org b/challenge-135/andinus/README.org new file mode 100644 index 0000000000..caf8135a12 --- /dev/null +++ b/challenge-135/andinus/README.org @@ -0,0 +1,42 @@ +#+title: Challenge 135 +#+date: 2021-10-22 +#+html_link_up: ../ +#+export_file_name: index +#+options: toc:nil +#+setupfile: ~/.emacs.d/org-templates/level-2.org + +* Task 1 - Middle 3-digits + +You are given an integer. + +Write a script find out the middle 3-digits of the given integer, if +possible otherwise throw sensible error. + +#+begin_src +Input: $n = 1234567 +Output: 345 + +Input: $n = -123 +Output: 123 + +Input: $n = 1 +Output: too short + +Input: $n = 10 +Output: even number of digits +#+end_src + +** 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. + +#+begin_src raku +$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); +#+end_src diff --git a/challenge-135/andinus/raku/ch-1.raku b/challenge-135/andinus/raku/ch-1.raku new file mode 100644 index 0000000000..b96911d691 --- /dev/null +++ b/challenge-135/andinus/raku/ch-1.raku @@ -0,0 +1,5 @@ +unit sub MAIN(Int $n is copy); +$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); -- cgit