Skip to content

Commit f4f61a0

Browse files
Add files via upload
1 parent b487ec0 commit f4f61a0

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

Week 5/Edit distance.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import numpy as np
2+
import sys
3+
4+
5+
def edit_distance(s, t):
6+
temp = np.zeros(shape=(len(s) + 1, len(t) + 1), dtype=int)
7+
8+
for i in range(len(s)+1):
9+
temp[i, 0] = i
10+
for i in range(len(t)+1):
11+
temp[0, i] = i
12+
13+
for i in range(1, len(s) + 1):
14+
for j in range(1, len(t) + 1):
15+
if s[i - 1] == t[j - 1]:
16+
temp[i, j] = temp[i - 1, j - 1]
17+
else:
18+
temp[i, j] = 1 + min(temp[i - 1, j], temp[i - 1, j - 1], temp[i, j - 1])
19+
20+
return (temp[len(s), len(t)])
21+
22+
23+
if __name__ == "__main__":
24+
input = sys.stdin.read()
25+
input = list(map(str, input.split('\n')))
26+
# print(edit_distance(input(), input()))
27+
print(edit_distance(input[0], input[1]))

Week 5/Moneychange.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import math
2+
3+
money = int(input())
4+
denominations = [1, 3, 4]
5+
minCoins = [0] + [math.inf]*money
6+
7+
for i in range(1, money+1):
8+
for j in denominations:
9+
if i>=j:
10+
coins = minCoins[i-j]+1
11+
if coins < minCoins[i]:
12+
minCoins[i] = coins
13+
14+
print(minCoins[money])

Week 5/Primitive Calculator.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import math
2+
3+
n = int(input())
4+
5+
# number of operations required for getting 0, 1, 2,.. , n
6+
num_operations = [0, 0] + [math.inf]*(n-1)
7+
8+
for i in range(2, n+1):
9+
temp1, temp2, temp3 = [math.inf]*3
10+
11+
temp1 = num_operations[i-1] + 1
12+
if i%2 == 0: temp2 = num_operations[i//2] + 1
13+
if i%3 == 0: temp3 = num_operations[i//3] + 1
14+
min_ops = min(temp1, temp2, temp3)
15+
num_operations[i] = min_ops
16+
17+
print(num_operations[n])
18+
19+
# Backtracking the numbers leading to n
20+
nums = [n]
21+
while n!=1:
22+
if n%3 ==0 and num_operations[n]-1 == num_operations[n//3]:
23+
nums += [n//3]
24+
n = n//3
25+
elif n%2 ==0 and num_operations[n]-1 == num_operations[n//2]:
26+
nums += [n//2]
27+
n = n//2
28+
else:
29+
nums += [n-1]
30+
n = n - 1
31+
32+
print(' '.join([str(i) for i in nums][::-1]))

0 commit comments

Comments
 (0)