Professional Documents
Culture Documents
DSA Assignment 3 Sol
DSA Assignment 3 Sol
Assignment 3
Let's trace through the function mystery with the input mystery([22,14,19,65,82,55],
1) :
1. On the first call, l = [22, 14, 19, 65, 82, 55] and v = 1 .
3. It calls mystery(l[:-1], l[-1] + v) , which means it calls mystery([22, 14, 19, 65,
82], 55 + 1) .
6. It calls mystery(l[:-1], l[-1] + v) , which means it calls mystery([22, 14, 19, 65], 82
+ 56) .
DSA Assignment 1
15. Now, l is an empty list, so it returns v , which is 22 + 236 = 258 .
DSA Assignment 2
When x = 2 , y = 4 , and z = 6 , the condition is 2 * 2 * 4 > 3 * 6 , which is 16 >
(2, 4, 5)
(3, 3, 5)
(3, 4, 5)
(3, 4, 6)
Therefore, triples will be equal to [(2, 4, 5), (3, 3, 5), (3, 4, 5), (3, 4, 6)] .
DSA Assignment 3
# option d is right, rest generates error
Output-{'Sharma': [37, 99], 'Kohli': [63, 47], 'Rahul': [74]}
Mocha") as a key in the dictionary inventory . Tuples can be used as dictionary keys
as long as they contain immutable elements. Since strings (such as "Amul" and
"Mystic Mocha" ) are immutable, this operation is valid and will not generate an error.
55 .
DSA Assignment 4
def orangecap(d):
sum1=0
sum2=0
sum3=0
sum4=0
highest=0
pla_high=""
DSA Assignment 5
for i in d:
DSA Assignment 6
d={"match1":{"player1":57,"player2":38},"match2":{"player3":9,"p
orangecap(d)
def cleanup(poly):
return [(coef, exp) for coef, exp in poly if coef != 0]
DSA Assignment 7
while i < len(p1) and j < len(p2):
coef1, exp1 = p1[i]
coef2, exp2 = p2[j]
if exp1 == exp2:
result.append((coef1 + coef2, exp1))
i += 1
j += 1
elif exp1 > exp2:
result.append((coef1, exp1))
i += 1
else:
result.append((coef2, exp2))
j += 1
return cleanup(result)
return cleanup(result)
# Examples
DSA Assignment 8
print(addpoly([(4, 3), (3, 0)], [(-4, 3), (2, 1)])) # Outpu
t: [(3, 0), (2, 1)]
print(addpoly([(2, 1)], [(-2, 1)])) # Output: []
print(multpoly([(1, 1), (-1, 0)], [(1, 2), (1, 1), (1, 0)]))
# Output: [(1, 3), (-1, 0)]
DSA Assignment 9