|
| 1 | +"""Kata - The observed PIN |
| 2 | +
|
| 3 | +completed at: 2024-09-06 19:18:17 |
| 4 | +by: Jakub Červinka |
| 5 | +
|
| 6 | +Alright, detective, one of our colleagues successfully observed our target person, Robby the robber. We followed him to a secret warehouse, where we assume to find all the stolen stuff. The door to this warehouse is secured by an electronic combination lock. Unfortunately our spy isn't sure about the PIN he saw, when Robby entered it. |
| 7 | +
|
| 8 | +The keypad has the following layout: |
| 9 | +``` |
| 10 | +┌───┬───┬───┐ |
| 11 | +│ 1 │ 2 │ 3 │ |
| 12 | +├───┼───┼───┤ |
| 13 | +│ 4 │ 5 │ 6 │ |
| 14 | +├───┼───┼───┤ |
| 15 | +│ 7 │ 8 │ 9 │ |
| 16 | +└───┼───┼───┘ |
| 17 | + │ 0 │ |
| 18 | + └───┘ |
| 19 | +``` |
| 20 | +He noted the PIN `1357`, but he also said, it is possible that each of the digits he saw could actually be another adjacent digit (horizontally or vertically, but not diagonally). E.g. instead of the `1` it could also be the `2` or `4`. And instead of the `5` it could also be the `2`, `4`, `6` or `8`. |
| 21 | +
|
| 22 | +He also mentioned, he knows this kind of locks. You can enter an unlimited amount of wrong PINs, they never finally lock the system or sound the alarm. That's why we can try out all possible (*) variations. |
| 23 | +
|
| 24 | +\* possible in sense of: the observed PIN itself and all variations considering the adjacent digits |
| 25 | +
|
| 26 | +Can you help us to find all those variations? It would be nice to have a function, that returns an array (or a list in Java/Kotlin and C#) of all variations for an observed PIN with a length of 1 to 8 digits. We could name the function `getPINs` (`get_pins` in python, `GetPINs` in C#). But please note that all PINs, the observed one and also the results, must be strings, because of potentially leading '0's. We already prepared some test cases for you. |
| 27 | +
|
| 28 | +Detective, we are counting on you! |
| 29 | +
|
| 30 | +```if:csharp |
| 31 | +***For C# user:*** Do not use Mono. Mono is too slower when run your code. |
| 32 | +``` |
| 33 | +
|
| 34 | +""" |
| 35 | + |
| 36 | +from itertools import chain |
| 37 | +from itertools import product |
| 38 | + |
| 39 | +def get_pins(observed): |
| 40 | + adjacents = { |
| 41 | + '0': ('0', '8'), |
| 42 | + '1': ('1', '2', '4'), |
| 43 | + '2': ('2', '1', '3', '5'), |
| 44 | + '3': ('3', '2', '6'), |
| 45 | + '4': ('4', '1', '5', '7'), |
| 46 | + '5': ('5', '2', '4', '6', '8'), |
| 47 | + '6': ('6', '3', '5', '9'), |
| 48 | + '7': ('7', '4', '8'), |
| 49 | + '8': ('8', '5', '7', '9', '0'), |
| 50 | + '9': ('9', '6', '8') |
| 51 | + } |
| 52 | + options = product(*[adjacents[num] for num in observed]) |
| 53 | + return (''.join(opt) for opt in options) |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
0 commit comments