aboutsummaryrefslogtreecommitdiff
path: root/challenge-151/andinus/README
blob: bd1dd6e3ce857e624f624a3473a1d5970c2514d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
                            ━━━━━━━━━━━━━━━
                             CHALLENGE 135

                                Andinus
                            ━━━━━━━━━━━━━━━


                               2021-10-22





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.

  ┌────
  │ Input: $n = 1234567
  │ Output: 345
  │
  │ Input: $n = -123
  │ Output: 123
  │
  │ Input: $n = 1
  │ Output: too short
  │
  │ Input: $n = 10
  │ Output: even number of digits
  └────


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.

  ┌────
  │ $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);
  └────