Skip to content

Commit b166b82

Browse files
JakubDotPygithub-actions[bot]
authored andcommitted
Automated update
1 parent 418f0e2 commit b166b82

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

output/message_validator.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Kata - Message Validator
2+
3+
completed at: 2024-09-06 18:51:26
4+
by: Jakub Červinka
5+
6+
In this kata, you have an input string and you should check whether it is a valid message. To decide that, you need to split the string by the numbers, and then compare the numbers with the number of characters in the following substring.
7+
8+
For example `"3hey5hello2hi"` should be split into `3, hey, 5, hello, 2, hi` and the function should return `true`, because `"hey"` is 3 characters, `"hello"` is 5, and `"hi"` is 2; as the numbers and the character counts match, the result is `true`.
9+
10+
11+
**Notes:**
12+
* Messages are composed of only letters and digits
13+
* Numbers may have multiple digits: e.g. `"4code13hellocodewars"` is a valid message
14+
* Every number must match the number of character in the following substring, otherwise the message is invalid: e.g. `"hello5"` and `"2hi2"` are invalid
15+
* If the message is an empty string, you should return `true`
16+
17+
"""
18+
19+
import re
20+
from itertools import tee
21+
from itertools import islice
22+
from itertools import zip_longest
23+
24+
def is_a_valid_message(message):
25+
parts = re.split(r'(\d+)', message)
26+
parts = filter(None, parts) # remove empty strings
27+
28+
# prepare iterators
29+
first_it, second_it = tee(parts, 2)
30+
31+
nums = islice(first_it, 0, None, 2)
32+
nums = map(int, nums)
33+
texts = islice(second_it, 1, None, 2)
34+
35+
try:
36+
for num, text in zip_longest(nums, texts):
37+
assert len(text) == num
38+
except (AssertionError, ValueError, TypeError):
39+
return False
40+
41+
# all passed
42+
return True

output/so_many_permutations.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Kata - So Many Permutations!
2+
3+
completed at: 2024-09-06 18:55:10
4+
by: Jakub Červinka
5+
6+
In this kata, your task is to create all permutations of a non-empty input string and remove duplicates, if present.
7+
8+
Create as many "shufflings" as you can!
9+
10+
Examples:
11+
```
12+
With input 'a':
13+
Your function should return: ['a']
14+
15+
With input 'ab':
16+
Your function should return ['ab', 'ba']
17+
18+
With input 'abc':
19+
Your function should return ['abc','acb','bac','bca','cab','cba']
20+
21+
With input 'aabb':
22+
Your function should return ['aabb', 'abab', 'abba', 'baab', 'baba', 'bbaa']
23+
```
24+
25+
Note: The order of the permutations doesn't matter.
26+
27+
Good luck!
28+
29+
30+
31+
"""
32+
33+
from itertools import permutations as perm
34+
35+
def permutations(s):
36+
return set(''.join(p) for p in perm(s))

output/the_observed_pin.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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

Comments
 (0)