Skip to content

Commit af5d8a3

Browse files
JakubDotPygithub-actions[bot]
authored andcommitted
Automated update
1 parent c6d5c53 commit af5d8a3

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

output/find_the_integer_sequences.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Kata - Find the integer sequences
2+
3+
completed at: 2024-04-16 16:14:07
4+
by: Jakub Červinka
5+
6+
>When no more interesting kata can be resolved, I just choose to create the new kata, to solve their own, to enjoy the process --myjinxin2015 said
7+
8+
# Description:
9+
Complete function `findSequences`. It accepts a positive integer `n`. Your task is to find such integer sequences:
10+
11+
<span style="background:#000000"><font size=4>Continuous positive integer and their sum value equals to n </font></span>
12+
13+
```
14+
For example, n = 15
15+
valid integer sequences:
16+
[1,2,3,4,5] (1+2+3+4+5==15)
17+
[4,5,6] (4+5+6==15)
18+
[7,8] (7+8==15)
19+
20+
```
21+
The result should be an array `[sequence 1,sequence 2...sequence n]`, sorted by ascending order of the length of sequences; If no result found, return [].
22+
23+
24+
# Some examples:
25+
26+
```
27+
28+
findSequences(3) === [[1,2]]
29+
30+
findSequences(15) === [[7,8],[4,5,6],[1,2,3,4,5]]
31+
32+
findSequences(20) === [[2, 3, 4, 5, 6]]
33+
34+
findSequences(100) === [[18, 19, 20, 21, 22], [9, 10, 11, 12, 13, 14, 15, 16]]
35+
36+
findSequences(1) === []
37+
```
38+
39+
"""
40+
41+
def find_sequences(n):
42+
sequences = []
43+
start = 1
44+
end = 1
45+
current_sum = 0
46+
while start <= n // 2:
47+
if current_sum < n:
48+
current_sum += end
49+
end += 1
50+
elif current_sum > n:
51+
current_sum -= start
52+
start += 1
53+
else:
54+
sequences.append(list(range(start, end)))
55+
current_sum -= start
56+
start += 1
57+
return sorted(sequences, key=len)
58+

output/real_password_cracker.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Kata - Real Password Cracker
2+
3+
completed at: 2024-04-16 18:23:59
4+
by: Jakub Červinka
5+
6+
### Story
7+
8+
You are a h4ck3r n00b: you "acquired" a bunch of password hashes, and you want to decypher them. Based on the length, you already guessed that they must be SHA-1 hashes. You also know that these are weak passwords: maximum 5 characters long and use only lowercase letters (`a-z`), no other characters.
9+
10+
Happy hacking!
11+
12+
**Notes:**
13+
* pre-generating the full hash table is not advised, due to the time-limit on the CW platform
14+
* there will be only a few tests for 5-letter words *(hint: start from the beginning of the alphabet)*
15+
* if your solution times out, try running it again - the CW runner is sometimes a bit slow(er)
16+
17+
---
18+
19+
### My other katas
20+
21+
If you enjoyed this kata then please try [my other katas](https://www.codewars.com/users/anter69/authored)! :-)
22+
23+
#### *Translations are welcome!*
24+
"""
25+
26+
from hashlib import sha1
27+
from itertools import product
28+
from string import ascii_lowercase as al
29+
30+
def password_cracker(hash):
31+
for l in range(1, 6):
32+
for word in product(al, repeat=l):
33+
word = ''.join(word)
34+
if sha1(word.encode()).hexdigest() == hash:
35+
return word

output/summarize_ranges.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Kata - Summarize ranges
2+
3+
completed at: 2024-04-16 21:02:17
4+
by: Jakub Červinka
5+
6+
Given a sorted array of numbers, return the summary of its ranges.
7+
8+
9+
## Examples
10+
```javascript
11+
summaryRanges([1, 2, 3, 4]) === ["1->4"]
12+
summaryRanges([1, 1, 1, 1, 1]) === ["1"]
13+
summaryRanges([0, 1, 2, 5, 6, 9]) === ["0->2", "5->6", "9"]
14+
summaryRanges([0, 1, 2, 3, 3, 3, 4, 5, 6, 7]) === ["0->7"]
15+
summaryRanges([0, 1, 2, 3, 3, 3, 4, 4, 5, 6, 7, 7, 9, 9, 10]) === ["0->7", "9->10"]
16+
summaryRanges([-2,0, 1, 2, 3, 3, 3, 4, 4, 5, 6, 7, 7, 9, 9, 10, 12]) ===["-2", "0->7", "9->10", "12"]
17+
```
18+
```python
19+
summary_ranges([1, 2, 3, 4]) == ["1->4"]
20+
summary_ranges([1, 1, 1, 1, 1]) == ["1"]
21+
summary_ranges([0, 1, 2, 5, 6, 9]) == ["0->2", "5->6", "9"]
22+
summary_ranges([0, 1, 2, 3, 3, 3, 4, 5, 6, 7]) == ["0->7"]
23+
summary_ranges([0, 1, 2, 3, 3, 3, 4, 4, 5, 6, 7, 7, 9, 9, 10]) == ["0->7", "9->10"]
24+
summary_ranges([-2, 0, 1, 2, 3, 3, 3, 4, 4, 5, 6, 7, 7, 9, 9, 10, 12]) == ["-2", "0->7", "9->10", "12"]
25+
```
26+
```ruby
27+
summary_ranges([1, 2, 3, 4]) == ["1->4"]
28+
summary_ranges([1, 1, 1, 1, 1]) == ["1"]
29+
summary_ranges([0, 1, 2, 5, 6, 9]) == ["0->2", "5->6", "9"]
30+
summary_ranges([0, 1, 2, 3, 3, 3, 4, 5, 6, 7]) == ["0->7"]
31+
summary_ranges([0, 1, 2, 3, 3, 3, 4, 4, 5, 6, 7, 7, 9, 9, 10]) == ["0->7", "9->10"]
32+
summary_ranges([-2, 0, 1, 2, 3, 3, 3, 4, 4, 5, 6, 7, 7, 9, 9, 10, 12]) == ["-2", "0->7", "9->10", "12"]
33+
```
34+
"""
35+
36+
from itertools import pairwise
37+
38+
39+
def find_consecutive_start(arr):
40+
for i, (a, b) in enumerate(pairwise(arr)):
41+
if b > a + 1:
42+
return arr[:i + 1]
43+
return arr
44+
45+
46+
def ranges_to_string(ranges):
47+
result = []
48+
for start, end in ranges:
49+
if start == end:
50+
result.append(str(start))
51+
else:
52+
result.append(f'{start}->{end}')
53+
return result
54+
55+
56+
def summary_ranges(arr):
57+
# setup
58+
start, end = 0, 1
59+
output = []
60+
61+
# edge cases
62+
if not arr: return output
63+
if len(arr) == 1: return [str(arr[0])]
64+
65+
while arr:
66+
sub_arr = find_consecutive_start(arr)
67+
output.append([sub_arr[0], sub_arr[-1]])
68+
arr = arr[len(sub_arr):]
69+
70+
output = ranges_to_string(output)
71+
return output
72+
73+

0 commit comments

Comments
 (0)