Repository navigation
Expand file tree
/
Copy path5.py
More file actions
68 lines (62 loc) · 1.6 KB
/
Copy path5.py
File metadata and controls
68 lines (62 loc) · 1.6 KB
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
# [5]Pete, the baker
def cakes(recipe, available):
count = []
for i,u in recipe.items():
count.extend([math.floor(h/u) for j,h in available.items() if i == j])
return min(count) if len(count) == len(recipe) else 0
# [5]Maximum subarray sum
def max_sequence(arr):
current = 0
biggest = 0
for i in arr:
current +=i
if current < 0:
current = 0
if current > biggest:
biggest = current
return biggest
# [5]Square Matrix Multiplication
def matrix_mult(a, b):
new = []
for i in range(len(a)):
new_row = []
for j in range(len(b)):
s = sum([a[i][k] * b[k][j] for k in range(len(b[j]))])
new_row.append(s)
new.append(new_row)
return new
# [5]Directions Reduction
def dirReduc(arr):
dict = {"NORTH":"SOUTH","SOUTH":"NORTH","EAST":"WEST","WEST":"EAST"}
res = []
for i in arr:
if res and dict[i] == res[-1]:
res.pop()
else:
res.append(i)
return res
# [5]Primes in numbers
def primeFactors(n):
i = 2
res = {}
while n/i != 1:
if n%i == 0:
if i in res:
res[i] = res[i]+1
else:
res[i] = 1
n = n/i
else:
i+=1
if i in res:
res[i] = res[i]+1
else:
res[i] = 1
t = ''
res = sorted(res.items(),key = lambda a:a[0])
for key in res:
if key[1] == 1:
t = t + '('+str(key[0]) +')'
else:
t = t + '(' +str(key[0]) + '**' + str(key[1]) + ')'
return t