Skip to content

Commit f4f28fc

Browse files
committed
Code cleaning
1 parent 21bccfb commit f4f28fc

17 files changed

+185
-163
lines changed

Chekio/Acceptable_Password.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# the length should be bigger than 6;
2-
# should contain at least one digit.
1+
# The length should be bigger than 6;
2+
# Should contain at least one digit.
33

44
def is_acceptable_password_2(password: str) -> bool:
55
# your code here
@@ -15,8 +15,8 @@ def is_acceptable_password_2(password: str) -> bool:
1515
print(is_acceptable_password_2('sh5')) #== False
1616

1717

18-
# the length should be bigger than 6;
19-
# should contain at least one digit, but cannot consist of just digits.
18+
# The length should be bigger than 6;
19+
# Should contain at least one digit, but cannot consist of just digits.
2020
def is_acceptable_password_3(password: str) -> bool:
2121
# your code here
2222
return True if len(password)>6 and not(password.isnumeric()) and any([x.isdigit() for x in password]) else False

Chekio/All_the_same.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
def all_the_same(elements):
1+
def is_all_the_same(elements: list) -> bool:
2+
"""
3+
Check if elements are all the same in list. If it's so - return True,
4+
else return False. Also return True if list consist of no elements or only one element.
5+
"""
26
if elements:
3-
k = elements[0]
4-
for el in elements:
5-
if k != el:
7+
first_element = elements[0]
8+
for element in elements:
9+
if first_element != element:
610
return False
711
return True
812
else:
913
return True
1014

11-
print(all_the_same(['a', 'a', 'ab']), all_the_same([2]), all_the_same([]))
15+
if __name__ == "__main__":
16+
print(is_all_the_same(['a', 'a', 'ab'])) # return False
17+
print(is_all_the_same(['amogus', 'amogus'])) # return True
18+
print(is_all_the_same([2])) # return True
19+
print(is_all_the_same([])) # return True

Chekio/Digits_multiplication.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
def dig_mul(number: int) -> int:
2-
res = 1
3-
for el in [int(d) for d in str(number) if d != '0']:
4-
res = res*el
5-
return res
6-
1+
def multiply_digits(number: int) -> int:
2+
"""
3+
Return multiplied digit eqivalent for digits in argument number.
4+
"""
5+
result = 1
6+
for el in [int(digit) for digit in str(number) if digit != '0']:
7+
result = result * el
8+
return result
9+
710
if __name__ == '__main__':
8-
print('Example:')
9-
print(dig_mul(123405), '\n',
10-
dig_mul(123405), '\n', #120
11-
dig_mul(999), '\n', #729
12-
dig_mul(1000), '\n', #1
13-
dig_mul(1111)) #1
11+
print('Example: ')
12+
print(multiply_digits(123405)) #120
13+
print(multiply_digits(999)) #729
14+
print(multiply_digits(1000)) #1
15+
print(multiply_digits(1111)) #1

Chekio/Map_filter_reduce.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
map_result = list(map(lambda x: round(x*x, 3), my_floats))
1212
filter_result = list(filter(lambda name: len(name) <=7, my_names))
1313
reduce_result = reduce(lambda num1, num2: num1 * num2, my_numbers)
14-
print(map_result)
15-
print(filter_result)
16-
print(reduce_result)
17-
#test_output_contains("[18.922, 37.088, 10.562, 95.453, 4.666, 78.854, 21.068]")
18-
#test_output_contains("['olumide', 'josiah', 'omoseun']")
19-
#test_output_contains("24840")
20-
#success_msg("Congrats! Nice work.")
14+
print(map_result) # ("[18.922, 37.088, 10.562, 95.453, 4.666, 78.854, 21.068]")
15+
print(filter_result) # ("['olumide', 'josiah', 'omoseun']")
16+
print(reduce_result) # ("24840")

Chekio/Pawn_Brotherhood.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
def safe_pawns(pawns: set) -> int:
2-
# count will be returned as number of 'safe' pawns
2+
"""
3+
Return number of 'safe' pawns by taking pawns position inside.
4+
"""
35
global count
46
count = 0
57
safe_list = []
68

7-
# check for pawns on borders
9+
# Check for pawns on borders
810
def left_border(i, pawns):
11+
global count
912
for el in pawns:
1013
if el[0] == chr(ord(i[0]) + 1) and int(el[1]) == int(i[1])-1 and i not in safe_list:
1114
count += 1
@@ -22,7 +25,7 @@ def right_border(i, pawns):
2225
#print(i, ", safe")
2326
return count
2427

25-
# check for pawns inside
28+
# Check for pawns inside playdesk
2629
def inside(i, pawns):
2730
global count
2831
for el in pawns:
@@ -36,7 +39,7 @@ def inside(i, pawns):
3639
#print(i, ", safe")
3740
return count
3841

39-
# main loop, that maintain upper functions, depending on each pawn spot
42+
# Main loop, that maintain upper functions, depending on each pawn spot
4043

4144
for element in pawns:
4245
if '1' in element:
@@ -47,9 +50,11 @@ def inside(i, pawns):
4750
right_border(element, pawns)
4851
else:
4952
inside(element, pawns)
50-
51-
return count
5253

54+
return count
5355

54-
print(safe_pawns({"b4", "d4", "f4", "c3", "e3", "g5", "d2"}), '\t',
55-
safe_pawns({"b4", "c4", "d4", "e4", "f4", "g4", "e5"}))
56+
if __name__ == "__main__":
57+
print(safe_pawns({"b4", "d4", "f4", "c3", "e3", "g5", "d2"})) # 6
58+
print(safe_pawns({"b4", "c4", "d4", "e4", "f4", "g4", "e5"})) # 1
59+
print(safe_pawns({"b4", "c4", "d4"})) # 0
60+
print(safe_pawns({"b4", "d4", "c3"})) # 1

Chekio/Popular_words.py

+18-32
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
1-
def popular_words(text: str, words: list) -> dict:
2-
# your code here
1+
def count_popular_words(text: str, words: list) -> dict:
2+
"""
3+
Count popular words (that you give as second argument) in text line with ''' format.
4+
"""
35
result = {}
4-
temp_2 = []
5-
temp_3 = []
6-
76
text = text.lower()
8-
temp_1 = text.split("\n")
9-
10-
for i in temp_1:
11-
if i:
12-
temp_2.append(i.split(" "))
13-
14-
15-
for j in temp_2:
16-
for i in j:
17-
temp_3.append(i)
18-
19-
print(temp_3)
7+
for symbol in range(0, len(text)):
8+
if text[symbol] == "\n":
9+
text = text[:symbol] + " " + text[symbol+1:]
2010

21-
for k in words:
22-
lim = 0
23-
if k in temp_3:
24-
for element in temp_3:
25-
if element == k:
26-
lim+=1
27-
result[k] = lim
11+
text_list = text.split(" ")
12+
print(text_list)
13+
for word in words:
14+
result[word] = text_list.count(word)
2815
return result
2916

30-
3117
if __name__ == '__main__':
32-
print("Example:")
33-
print(popular_words('''
34-
When I was One
35-
I had just begun
36-
When I was Two
37-
I was nearly new
38-
''', ['i', 'was', 'three', 'near']))
18+
print("Example: ")
19+
print(count_popular_words(
20+
'''When I was One
21+
I had just begun
22+
When I was Two
23+
I was nearly new''',
24+
['i', 'was', 'three', 'near'])) # {'i': 4, 'was': 3, 'three': 0, 'near': 0}
+15-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
def frequency_sort(items):
2-
# your code here
3-
res = []
1+
def frequency_sort(items: list) -> list:
2+
"""
3+
Sort elements in list by their frequency and output all of them in that order.
4+
"""
5+
result = []
46
temp_dict = {}
5-
for i in items:
6-
temp_dict[i] = items.count(i)
7-
temp = sorted(temp_dict.items(), key=lambda x: x[1], reverse=True)
8-
for el in temp:
9-
for j in range(el[1]):
10-
res.append(el[0])
11-
return res
7+
for item in items:
8+
temp_dict[item] = items.count(item)
9+
sorted_dict = sorted(temp_dict.items(), key=lambda x: x[1], reverse=True)
10+
for element in sorted_dict:
11+
for j in range(element[1]):
12+
result.append(element[0])
13+
return result
14+
1215
if __name__ == '__main__':
1316
print("Example:")
14-
print(frequency_sort([9, 0, 4, 6, 2, 2, 6, 4, 4, 4, 0]))
17+
print(frequency_sort([9, 0, 4, 6, 2, 2, 6, 4, 4, 4, 0])) # [4, 4, 4, 4, 0, 0, 6, 6, 2, 2, 9]
18+
print(frequency_sort([3, 2, 3, 3, 2, 8, 6, 4, 4, 1, 1])) # [3, 3, 3, 2, 2, 4, 4, 1, 1, 8, 6]

Chekio/Sort_by_extension.py

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from typing import List
22

3-
def ext(file):
3+
def ext(file: list) -> list:
4+
"""
5+
Control extensions order
6+
"""
47
if '.config' in file:
58
temp = ['.config', '']
69
temp = file.split('.')
@@ -11,27 +14,20 @@ def ext(file):
1114
return temp[::-1]
1215

1316
def sort_by_ext(files: List[str]) -> List[str]:
17+
"""
18+
Main function that sort the result after ext function
19+
"""
1420
return sorted(files, key=ext)
1521

1622

1723
if __name__ == '__main__':
1824

1925
print("Example:")
20-
print(sort_by_ext(['1.cad', '1.bat', '1.aa']))
21-
22-
# These are used for self-checking
2326
print( '\n',
24-
sort_by_ext(['1.cad', '1.bat', '1.aa']), '\n',
25-
#== ['1.aa', '1.bat', '1.cad']
26-
sort_by_ext(['1.cad', '1.bat', '1.aa', '2.bat']), '\n',
27-
#== ['1.aa', '1.bat', '2.bat', '1.cad']
28-
sort_by_ext(['1.cad', '1.bat', '1.aa', '.bat']), '\n',
29-
#== ['.bat', '1.aa', '1.bat', '1.cad']
30-
sort_by_ext(['1.cad', '1.bat', '.aa', '.bat']), '\n',
31-
#== ['.aa', '.bat', '1.bat', '1.cad']
32-
sort_by_ext(['1.cad', '1.', '1.aa']), '\n',
33-
#== ['1.', '1.aa', '1.cad']
34-
sort_by_ext(['1.cad', '1.bat', '1.aa', '1.aa.doc']), '\n',
35-
#== ['1.aa', '1.bat', '1.cad', '1.aa.doc']
36-
sort_by_ext(['1.cad', '1.bat', 'config.aa', '.aa.doc']))
37-
#== ['config.aa', '1.bat', '1.cad', '.aa.doc']
27+
sort_by_ext(['1.cad', '1.bat', '1.aa']), '\n', # ['1.aa', '1.bat', '1.cad']
28+
sort_by_ext(['1.cad', '1.bat', '1.aa', '2.bat']), '\n', # ['1.aa', '1.bat', '2.bat', '1.cad']
29+
sort_by_ext(['1.cad', '1.bat', '1.aa', '.bat']), '\n', # ['.bat', '1.aa', '1.bat', '1.cad']
30+
sort_by_ext(['1.cad', '1.bat', '.aa', '.bat']), '\n', # ['.aa', '.bat', '1.bat', '1.cad']
31+
sort_by_ext(['1.cad', '1.', '1.aa']), '\n', # ['1.', '1.aa', '1.cad']
32+
sort_by_ext(['1.cad', '1.bat', '1.aa', '1.aa.doc']), '\n', # ['1.aa', '1.bat', '1.cad', '1.aa.doc']
33+
sort_by_ext(['1.cad', '1.bat', 'config.aa', '.aa.doc'])) # ['config.aa', '1.bat', '1.cad', '.aa.doc']

Chekio/Split_List_Into_Two.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
def split_list(items: list) -> list:
2+
"""
3+
Split list into two separate lists inside itself. When size is odd number - left side is bigger on one.
4+
"""
25
if len(items) % 2 == 0:
36
return [items[:int(len(items)/2)], items[int(len(items)/2):]]
47
else:
58
return [items[:int(len(items)/2)+1], items[1+int(len(items)/2):]]
69

7-
print(split_list([1, 2, 3, 4, 5, 6, 7]))
10+
if __name__ == "__main__":
11+
print(split_list([1, 2, 3, 4, 5, 6, 7])) # [[1, 2, 3, 4], [5, 6, 7]]
12+
print(split_list([1, 2, 3, 4, 5, 6])) # [[1, 2, 3], [4, 5, 6]]
13+
print(split_list([1])) # [[1], []]
14+
print(split_list([])) # [[], []]

Chekio/Sun_Angle.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from typing import Union
22

3-
43
def sun_angle(time: str) -> Union[int, str]:
5-
# check if it's not the night time
4+
"""
5+
Check if it's not the night time. If so - return sun angle. If not - return message "I don't see the sun!".
6+
"""
67
hour = int(time[0])*10 + int(time[1])
78
minutes = int(time[3])*10 + int(time[4])
89

@@ -12,10 +13,11 @@ def sun_angle(time: str) -> Union[int, str]:
1213
return "I don't see the sun!"
1314

1415
# formula
15-
res = float((hour - 6) * 15 + minutes * 0.25)
16-
17-
return res
18-
16+
result = float((hour - 6) * 15 + minutes * 0.25)
17+
return result
1918

2019
if __name__ == '__main__':
21-
print(sun_angle("12:15"), sun_angle("18:01"))
20+
print(sun_angle("12:15")) # 93.75
21+
print(sun_angle("12:00")) # 90.0
22+
print(sun_angle("16:20")) # 155.0
23+
print(sun_angle("18:01")) # I don't see the sun!

Chekio/Words_Order.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
# I know this isn't the best solution and not even clear one, i just make it work
12
def words_order(text: str, words: list) -> bool:
2-
# i know this isn't the best solution and not even clear one, i just make it work
3+
"""
4+
Checks if words in text are in the same order as you write in arguments.
5+
First argument - text(string), second one - words (list of strings)
6+
"""
37
if len(words) == 1 and words[0] in text:
48
return True
59
elif not text:
@@ -8,30 +12,30 @@ def words_order(text: str, words: list) -> bool:
812
text_list = text.split(" ")
913

1014
try:
11-
temp = text_list.index(words[0])
15+
first_word = text_list.index(words[0])
1216
except ValueError: return False
1317

14-
k = []
18+
result = []
1519

16-
for i in words[1:]:
20+
for word in words[1:]:
1721
try:
18-
p = text_list.index(i)
22+
word_index = text_list.index(word)
1923
except ValueError: return False
2024

21-
if i in text_list and temp < p:
22-
k.append(True)
25+
if word in text_list and first_word < word_index:
26+
result.append(True)
2327
else:
24-
k.append(False)
25-
26-
return all(k)
28+
result.append(False)
29+
30+
return all(result)
2731

2832
if __name__ == '__main__':
29-
print("Example:")
33+
print("Example: ")
3034
print(words_order("I am here", ['I', 'here'])) #True
3135
print(words_order("I am here", ['I', 'I'])) #False
3236
print(words_order("I am here", ['here', 'am'])) #False
3337
print(words_order("", ['I', 'am'])) #False
3438
print(words_order("I am here", ['I'])) #True
3539
print(words_order('hi world im here', ['world', 'here', 'hi'])) #False
3640
print(words_order('hi world im here', ['world', 'hi', 'here'])) #False
37-
print(words_order('hi world im here', ['country', 'world'])) #False
41+
print(words_order('hi world im here', ['country', 'world'])) #False

0 commit comments

Comments
 (0)