Skip to content

Commit 394c5e0

Browse files
committed
add python kyu6 kata
1 parent 6e2e8be commit 394c5e0

5 files changed

+54
-51
lines changed

python/kyu5_calculate_variance.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
def variance(numbers):
2-
""" Write a function which will accept a sequence of numbers and calculate the variance for the sequence;
3-
The variance for a set of numbers is found by subtracting the mean from every value, squaring the results, adding
4-
them all up and dividing by the number of elements;
1+
""" Write a function which will accept a sequence of numbers and calculate the variance for the sequence;
2+
The variance for a set of numbers is found by subtracting the mean from every value, squaring the results, adding
3+
them all up and dividing by the number of elements;
4+
5+
Examples:
6+
variance([8, 9, 10, 11, 12]), 2)
7+
variance([1.5, 2.5, 4, 2, 1, 1]), 1.0833333333333333)
58
6-
Examples:
7-
variance([8, 9, 10, 11, 12]), 2)
8-
variance([1.5, 2.5, 4, 2, 1, 1]), 1.0833333333333333)
9+
"""
910

10-
Args:
11-
numbers: the sequence of numbers to calculate the variance for
1211

13-
Returns:
14-
float: a variance for given sequence
15-
"""
12+
def variance(numbers):
1613
mean = sum(numbers) / len(numbers)
1714
return sum(list(map(lambda x: (x - mean) ** 2, numbers))) / len(numbers)

python/kyu5_number_of_trailing_zeros_of_N.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
def zeros(n):
2-
""" Write a program that will calculate the number of trailing zeros in a factorial of a given number;
3-
N! = 1 * 2 * 3 * ... * N
4-
Be careful 1000! has 2568 digits...
1+
""" Write a program that will calculate the number of trailing zeros in a factorial of a given number;
2+
N! = 1 * 2 * 3 * ... * N
3+
Be careful 1000! has 2568 digits...
4+
5+
Hint: You're not meant to calculate the factorial. Find another way to find the number of zeros;
56
6-
Hint: You're not meant to calculate the factorial. Find another way to find the number of zeros;
7+
Examples:
8+
zeros(6) = 1
9+
# 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720 --> 1 trailing zero
710
8-
Examples:
9-
zeros(6) = 1
10-
# 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720 --> 1 trailing zero
11+
zeros(12) = 2
12+
# 12! = 479001600 --> 2 trailing zeros
1113
12-
zeros(12) = 2
13-
# 12! = 479001600 --> 2 trailing zeros
14+
"""
1415

15-
Args:
16-
n (int): the number in the factorial of which we want to count the number of trailing zeros;
1716

18-
Returns:
19-
int: number of trailing zeros
20-
"""
17+
def zeros(n):
2118
count, i = 0, 5
2219
while n // i:
2320
count, i = count + n // i, i * 5

python/kyu6_array_diff.py

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
def array_diff(a, b):
2-
""" Your goal in this kata is to implement a difference function, which subtracts one list
3-
from another and returns the result;
1+
""" Your goal in this kata is to implement a difference function, which subtracts one list
2+
from another and returns the result;
3+
4+
It should remove all values from list a, which are present in list b;
5+
If a value is present in b, all of its occurrences must be removed from the other;
46
5-
It should remove all values from list a, which are present in list b;
6-
If a value is present in b, all of its occurrences must be removed from the other;
7+
Examples:
8+
array_diff([1,2],[1]) == [2]
9+
array_diff([1,2,2,2,3],[2]) == [1,3]
710
8-
Examples:
9-
array_diff([1,2],[1]) == [2]
10-
array_diff([1,2,2,2,3],[2]) == [1,3]
11+
"""
1112

12-
Args:
13-
a (list): first list
14-
b (list): second list
1513

16-
Returns:
17-
list: difference for lists 'a' and 'b'
18-
"""
14+
def array_diff(a, b):
1915
return [x for x in a if x not in b]

python/kyu6_the_vowel_code.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@
1515
above
1616
For example, decode("h3 th2r2") would return "hi there"
1717
For the sake of simplicity, you can assume that any numbers passed into the function will correspond to vowels
18+
1819
"""
1920

21+
vowels = {'a': 1, 'e': 2, 'i': 3, 'o': 4, 'u': 5, 'A': 1, 'E': 2, 'I': 3, 'O': 4, 'U': 5,}
22+
2023

2124
def encode(st):
22-
encoded = ''
23-
vowels = {'a': 1, 'e': 2, 'i': 3, 'o': 4, 'u': 5}
24-
for char in st:
25-
encoded += str(vowels[str(char).lower()]) if str(char).lower() in vowels.keys() else str(char)
26-
return encoded
25+
encode_char = lambda char: char if char not in vowels.keys() else str(vowels.get(char, ''))
26+
return ''.join([encode_char(a) for a in st])
2727

2828

2929
def decode(st):
30-
decoded = ''
31-
numbers = {'1': 'a', '2': 'e', '3': 'i', '4': 'o', '5': 'u'}
32-
for char in st:
33-
decoded += str(numbers[str(char)]) if char in numbers.keys() else char
34-
return decoded
30+
inversed_vowels = {str(v): str(k) for k, v in vowels.items()}
31+
decode_char = lambda char: char if char not in inversed_vowels.keys() else str(
32+
inversed_vowels.get(char, '')).lower()
33+
return ''.join([decode_char(a) for a in st])

python/kyu7_is_isogram.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
""" An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that
2+
determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram;
3+
Ignore letter case;
4+
5+
Example: (Input --> Output)
6+
"Dermatoglyphics" --> true
7+
"aba" --> false
8+
"moOse" --> false (ignore letter case)
9+
10+
"""
11+
12+
13+
def is_isogram(string):
14+
return len(list(set(string.lower()))) == len(list(string.lower()))

0 commit comments

Comments
 (0)