Skip to content

Commit 9fbfe92

Browse files
JakubDotPygithub-actions[bot]
authored andcommitted
Automated update
1 parent 3904bf8 commit 9fbfe92

File tree

4 files changed

+199
-0
lines changed

4 files changed

+199
-0
lines changed

output/area_of_a_shape.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Kata - Area of a Shape
2+
3+
completed at: 2024-05-14 16:44:00
4+
by: Jakub Červinka
5+
6+
We'd like to know the area of an arbitrary shape. The only information of the shape is that it is bounded within the square area of four points: (0, 0), (1, 0), (1, 1) and (0, 1).
7+
8+
Given a function `f(x, y)` which returns a boolean representing whether the point `(x, y)` is inside the shape, determine the area of the shape. Your answer is allowed to differ from the true answer by at most 0.01.
9+
10+
Hint: http://bit.ly/1vJJt61
11+
"""
12+
13+
from random import random as rand
14+
15+
def area_of_the_shape(f):
16+
n = 10_000
17+
inside = sum(f(rand(), rand()) for _ in range(n))
18+
return inside / n

output/crack_the_pin.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Kata - Crack the PIN
2+
3+
completed at: 2024-05-14 19:09:24
4+
by: Jakub Červinka
5+
6+
Given is a md5 hash of a five digits long PIN. It is given as string.
7+
Md5 is a function to hash your password:
8+
"password123" ===> "482c811da5d5b4bc6d497ffa98491e38"
9+
10+
Why is this useful?
11+
Hash functions like md5 can create a hash from string in a short time and it is impossible to find out the password, if you only got the hash. The only way is cracking it, means try every combination, hash it and compare it with the hash you want to crack. (There are also other ways of attacking md5 but that's another story)
12+
Every Website and OS is storing their passwords as hashes, so if a hacker gets access to the database, he can do nothing, as long the password is safe enough.
13+
14+
<a href="https://en.wikipedia.org/wiki/Hash_function#:~:text=A%20hash%20function%20is%20any,table%20called%20a%20hash%20table." target="_blank">What is a hash?</a>
15+
16+
<a href="https://en.wikipedia.org/wiki/MD5" target="_blank">What is md5?</a>
17+
18+
Note: Many languages have build in tools to hash md5. If not, you can write your own md5 function or google for an example.
19+
20+
<a href="https://www.codewars.com/kata/password-hashes" target="_blank">Here</a> is another kata on generating md5 hashes!
21+
22+
23+
Your task is to return the cracked PIN as string.
24+
25+
This is a little fun kata, to show you, how weak PINs are and how important a bruteforce protection is, if you create your own login.
26+
27+
If you liked this kata, <a href="https://www.codewars.com/kata/59146f7b4670ba520900000a" target="_blank">here</a> is an extension with short passwords!
28+
29+
Some of my other katas:
30+
<br>
31+
<a href="https://www.codewars.com/kata/5ef9ca8b76be6d001d5e1c3e" target="_blank">Error Correction #1 - Hamming Code</a>
32+
<br>
33+
<a href="https://www.codewars.com/kata/5f0795c6e45bc600247ab794" target="_blank">Hack the NSA</a>
34+
<br>
35+
<a href="https://www.codewars.com/kata/5ef9c85dc41b4e000f9a645f" target="_blank">Decode the QR-Code</a>
36+
37+
38+
39+
"""
40+
41+
import hashlib
42+
43+
44+
def crack(hash):
45+
for n in range(100_000):
46+
pwd = f'{n:05}'
47+
my_hash = hashlib.md5(pwd.encode('utf-8')).hexdigest()
48+
if my_hash == hash:
49+
return pwd

output/find_the_strongest_apes.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""Kata - Find the Strongest Apes
2+
3+
completed at: 2024-05-14 18:45:48
4+
by: Jakub Červinka
5+
6+
Imagine that you went to a zoo and some zoologists asked you for help.
7+
They want you to find the strongest ape of its own kind and there are 4 types of apes in the zoo. (Gorilla, Gibbon, Orangutan, Chimpanzee)
8+
9+
* There will be only one parameter which is a list containing lots of dictionaries.
10+
* Each dictionary will be like this: `{"name": name, "weight": weight, "height": height, "type": type}`
11+
* To find the strongest ape, you need to compare the sum of weight and height of each apes of that kind.
12+
* The ape with the highest weight and height will be the strongest ape.
13+
* You need to return a dictionary which contains the names of the strongest apes of all kinds.`{"Gorilla": strongest_gorilla, "Gibbon": strongest_gibbon, "Orangutan": strongest_orangutan, "Chimpanzee": strongest_chimpanzee}`
14+
* There can be 2 or more apes which has the same highest weight and height. In that case, you need to sort their names by alphabet and then choose the first one as the strongest ape.
15+
* If there are no apes for a kind (e.g. Gorilla), you need to return a dictionary like this: `{"Gorilla": None, "Gibbon": strongest_gibbon, "Orangutan": strongest_orangutan, "Chimpanzee": strongest_chimpanzee}`
16+
17+
__Example 1:__
18+
```
19+
find_the_strongest_apes(
20+
[{"name": "aba", "weight": 101, "height": 99, "type": "Gorilla"},
21+
{"name": "abb", "weight": 99, "height": 101, "type": "Gorilla"},
22+
{"name": "abc", "weight": 101, "height": 99, "type": "Orangutan"},
23+
{"name": "abd", "weight": 99, "height": 101, "type": "Orangutan"}])
24+
```
25+
Should return `{'Gorilla': 'aba', 'Gibbon': None, 'Orangutan': 'abc', 'Chimpanzee': None}`
26+
27+
__Example 2:__
28+
```
29+
find_the_strongest_apes(
30+
[{"name": "aba", "weight": 140, "height": 120, "type": "Gorilla"},
31+
{"name": "abb", "weight": 20, "height": 50, "type": "Gibbon"},
32+
{"name": "abc", "weight": 75, "height": 110, "type": "Orangutan"},
33+
{"name": "abd", "weight": 50, "height": 100, "type": "Chimpanzee"}])
34+
```
35+
Should return `{'Gorilla': 'aba', 'Gibbon': 'abb', 'Orangutan': 'abc', 'Chimpanzee': 'abd'}`
36+
37+
__Example 3:__
38+
```
39+
find_the_strongest_apes(
40+
[{"name": "aba", "weight": 140, "height": 120, "type": "Gorilla"},
41+
{"name": "abb", "weight": 150, "height": 120, "type": "Gorilla"},
42+
{"name": "abc", "weight": 75, "height": 110, "type": "Orangutan"},
43+
{"name": "abd", "weight": 50, "height": 100, "type": "Chimpanzee"}])
44+
```
45+
Should return `{'Gorilla': 'abb', 'Gibbon': None, 'Orangutan': 'abc', 'Chimpanzee': 'abd'}`
46+
47+
_*English is not my native language, so some sentences may not be descriptive enough or even give incorrect information. If you find a wrong sentence, please feel free to comment._
48+
"""
49+
50+
from dataclasses import dataclass
51+
from dataclasses import field
52+
from collections import defaultdict
53+
from itertools import groupby
54+
from operator import attrgetter
55+
56+
57+
@dataclass
58+
class Ape:
59+
type: str
60+
power: int = field(init=False)
61+
weight: int
62+
height: int
63+
name: str
64+
65+
def __post_init__(self):
66+
self.power = self.weight + self.height
67+
68+
def __str__(self):
69+
return f'{self.type} {self.name!r} p: {self.power}'
70+
71+
__repr__ = __str__
72+
73+
74+
sort_fn = lambda x: (x.type, -x.power, x.name)
75+
76+
77+
def find_the_strongest_apes(_list):
78+
types = ('Gorilla', 'Gibbon', 'Orangutan', 'Chimpanzee')
79+
result = dict.fromkeys(types, None)
80+
81+
monkes = sorted((Ape(**data) for data in _list), key=sort_fn)
82+
83+
for _, apes in groupby(monkes, key=attrgetter('type')):
84+
ape = next(apes)
85+
result[ape.type] = ape.name
86+
87+
return result
88+
89+
90+

output/fizzbuzz_backwards.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Kata - FizzBuzz Backwards
2+
3+
completed at: 2024-05-14 16:53:21
4+
by: Jakub Červinka
5+
6+
Traditionally in FizzBuzz, multiples of 3 are replaced by "Fizz" and multiples of 5 are replaced by "Buzz". But we could also play FizzBuzz with any other integer pair `[n, m]` whose multiples are replaced with Fizz and Buzz.
7+
8+
For a sequence of numbers, Fizzes, Buzzes and FizzBuzzes, find the numbers whose multiples are being replaced by Fizz and Buzz. Return them as an array `[n, m]`
9+
10+
The Fizz and Buzz numbers will always be integers between 1 and 50, and the sequence will have a maximum length of 100. The Fizz and Buzz numbers might be equal, and might be equal to 1.
11+
12+
## Examples
13+
* Classic FizzBuzz; multiples of 3 are replaced by Fizz, multiples of 5 are replaced by Buzz:
14+
```
15+
[1, 2, "Fizz", 4, "Buzz", 6] ==> [3, 5]
16+
```
17+
* Multiples of 2 are replaced by Fizz, multiples of 3 are replaced by Buzz:
18+
```
19+
[1, "Fizz", "Buzz", "Fizz", 5, "FizzBuzz"] ==> [2, 3]
20+
```
21+
* Multiples of 2 are replaced by Fizz and Buzz:
22+
```
23+
[1, "FizzBuzz", 3, "FizzBuzz", 5, "FizzBuzz"] ==> [2, 2]
24+
```
25+
* Fizz = 1, Buzz = 6:
26+
```
27+
["Fizz", "Fizz", "Fizz", "Fizz", "Fizz", "FizzBuzz"] ==> [1, 6]
28+
```
29+
"""
30+
31+
def reverse_fizz_buzz(array):
32+
fizz = buzz = None
33+
for i, elem in enumerate(array, start=1):
34+
elem = str(elem)
35+
if 'Fizz' in elem and not fizz:
36+
fizz = i
37+
if 'Buzz' in elem and not buzz:
38+
buzz = i
39+
if fizz and buzz:
40+
return fizz, buzz
41+
42+

0 commit comments

Comments
 (0)