-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
152 lines (120 loc) · 3.54 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def fizz_buzz(number: int) -> str:
if number % 3 == 0 and number % 3 == 0:
return "Fizz Buzz"
elif number % 3 == 0:
return "Fizz"
elif number % 5 == 0:
return "Buzz"
else:
return str(number)
# adds comma in the end of the sentence
def correct_sentence(text: str) -> str:
if text[-1] == ".":
return text[0].upper() + text[1:]
else:
return text[0].upper() + text[1:] + "."
def first_word(text: str) -> str:
result = ""
text = text.replace(".", " ").replace(",", " ").lstrip()
for i in text:
if i == " ":
break
result += i
return result
# returns a tuple with 3 elements - first, third and second to the last
def easy_unpack(elements: tuple) -> tuple:
return elements[0], elements[2], elements[-2]
# returns the second index of a symbol in a given text
def second_index(text: str, symbol: str) -> [int, None]:
try:
return text.index(symbol, text.index(symbol) + 1)
except ValueError:
return None
# returns substring between two given markers
def between_markers(text: str, begin: str, end: str) -> str:
returning_text = ""
if text.find(begin) != -1:
start_index = text.find(begin) + len(begin)
else:
start_index = 0
if text.find(end) != -1:
end_index = text.find(end)
else:
end_index = len(text)
if start_index > end_index:
return ""
while start_index != end_index:
returning_text += text[start_index]
start_index += 1
return returning_text
def difference_min_max(*args):
if args:
return max(args) - min(args)
return 0
# Find Nth power of the element with index N.
def index_power(array: list, n: int) -> int:
if n < len(array):
return array[n] ** n
return -1
# sums even-indexes elements and multiply at the last
def even_indexes_and_multiply_the_last(array):
sum = 0
if len(array) == 0:
return 0
for x in range(len(array)):
if x % 2 == 0:
sum += array[x]
sum *= array[-1]
return sum
# sort by abs
def sort_by_abs(numbers_array: tuple) -> list:
return sorted(numbers_array, key=abs)
# Find a secret message
def find_message(text: str) -> str:
result = ""
for x in text:
if x.isupper():
result += x
return result
# Join strings and replace "right" to "left"
def left_join(phrases):
return ",".join(phrases).replace("right", "left")
# three words in a row
def words_row(words: str) -> bool:
count = 0
for x in words.split():
if x.isalpha():
count += 1
else:
count = 0
if count == 3:
return True
return False
# multiple all digits in number
def digits_multiple(number: int) -> int:
result = 1
for x in str(number):
if x == '0':
continue
result *= int(x)
return result
# data - dict like {'example' : 100, ...}
def best_stock(data):
price = 0
stock_name = ""
for x in data:
if data[x] > price:
price = data[x]
stock_name = x
return stock_name
# return dict with words and count of them in text
def popular_words(text: str, words: list) -> dict:
text = text.lower()
result = {}
splited_text = text.split()
for x in words:
result[x] = splited_text.count(x)
return result
# TOP most expensive goods params limit of selection, list of dicts
def bigger_price(limit: int, data: list) -> list:
return sorted(data, key=lambda x: x['price'], reverse=True)[:limit]