-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.py
123 lines (105 loc) · 2.79 KB
/
test2.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
def count_words(arr):
def helper(count_elem, str):
count = 0
for i in str:
if i == count_elem:
count += 1
return count
diction = {}
for i in arr:
diction[i] = helper(i, arr)
return diction
#print(count_words(["apple", "banana", "apple", "pie"]))
def unique_words_count(arr):
count = 0
def helper(elem, lst):
for i in lst:
if i == elem:
return True
return False
arr2 = []
for i in arr:
if (helper(i, arr2) is False):
count += 1
arr2.append(i)
return count
#print(unique_words_count(["apple", "banana", "apple", "pie"]))
def groupby(func, seq):
result = {}
for item in seq:
key = func(item)
if key in result:
result[key].append(item)
else:
result[key] = [item]
return result
#print(lambda x: 'odd' if x % 2 else 'even', [1, 2, 3, 5, 8, 9, 10, 12])
def prepare_meal(number):
max = 0
result = ""
for i in range(1, number):
if number % (3 ** i) == 0:
result += ("spam ")
if i > max:
max = i
if result == "" and number % 5 == 0:
result += "eggs"
elif number % 5 == 0:
result += "and eggs"
return result
#print(prepare_meal(45))
def reduce_file_path(path):
result = ""
for i in path:
result += i
if path[i + 1] == '/' or path[i] == '.':
result -= i
if path[i] == '.' and path[i + 1] == '.':
while path[i - 1] != '/':
result -= path[i]
if path[len[path] - 1] == '/':
result -= path[i]
return result
#not working
def is_an_bn(word):
counter = 0
i = 0
if word == "":
return True
while word[i] == 'a':
counter += 1
i += 1
while word[i] == 'b' and i < len(word) - 1:
counter -= 1
i += 1
if counter == 1:
return True
return False
#print(is_an_bn("aabbaabb"))
def simplify_fraction(fraction):
def find_gcd(x, y):
while y != 0:
(x, y) = (y, x % y)
return x
return (fraction[0] // find_gcd(fraction[0], fraction[1]), fraction[1] // find_gcd(fraction[0], fraction[1]))
#print (simplify_fraction((63, 462)))
def nth_fib_lists(listA, listB, n):
if n == 1:
return listA
if n == 2:
return listB
if n == 3:
return listA + listB
return nth_fib_lists(listA, listB, n - 1) + nth_fib_lists(listA, listB, n - 2)
def sort_fractions(fractions):
my_dict = {}
arr = []
result = []
for i in fractions:
my_dict[i[0] / i[1]] = i
for i in fractions:
arr.append(i[0] / i[1])
arr.sort()
for each in arr:
result.append(my_dict[each])
return result