Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

DSA Assignment

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 .

2. Since the length of l is not zero, it goes to the else block.

3. It calls mystery(l[:-1], l[-1] + v) , which means it calls mystery([22, 14, 19, 65,

82], 55 + 1) .

4. Now, l = [22, 14, 19, 65, 82] and v = 56 .

5. Again, the length of l is not zero, so it goes to the else block.

6. It calls mystery(l[:-1], l[-1] + v) , which means it calls mystery([22, 14, 19, 65], 82

+ 56) .

7. Now, l = [22, 14, 19, 65] and v = 138 .

8. Same procedure, it calls mystery([22, 14, 19], 65 + 138) .

9. Now, l = [22, 14, 19] and v = 203 .

10. Same procedure, it calls mystery([22, 14], 19 + 203) .

11. Now, l = [22, 14] and v = 222 .

12. Same procedure, it calls mystery([22], 14 + 222) .

13. Now, l = [22] and v = 236 .

14. Same procedure, it calls mystery([], 22 + 236) .

DSA Assignment 1
15. Now, l is an empty list, so it returns v , which is 22 + 236 = 258 .

So, mystery([22, 14, 19, 65, 82, 55], 1) returns 258 .

Let's break down the list comprehension:

triples = [(x, y, z) for x in range(2, 4) for y in range(2,


5) for z in range(5, 7) if 2 * x * y > 3 * z]

This list comprehension iterates over three nested loops:

1. The first loop iterates over x in the range from 2 to 3 (inclusive).

2. The second loop iterates over y in the range from 2 to 4 (inclusive).

3. The third loop iterates over z in the range from 5 to 6 (inclusive).

For each combination of x , y , and z , it checks if the condition 2 * x * y > 3 * z is


true.
Let's find the combinations of (x, y, z) that satisfy the condition:

When x = 2 , y = 2 , and z = 5 , the condition is 2 * 2 * 2 > 3 * 5 , which is 8 >

15 . This condition is not satisfied.

When x = 2 , y = 2 , and z = 6 , the condition is 2 * 2 * 2 > 3 * 6 , which is 8 >

18 . This condition is not satisfied.

When x = 2 , y = 3 , and z = 5 , the condition is 2 * 2 * 3 > 3 * 5 , which is 12 >

15 . This condition is not satisfied.

When x = 2 , y = 3 , and z = 6 , the condition is 2 * 2 * 3 > 3 * 6 , which is 12 >

18 . This condition is not satisfied.

When x = 2 , y = 4 , and z = 5 , the condition is 2 * 2 * 4 > 3 * 5 , which is 16 >

15 . This condition is satisfied.

DSA Assignment 2
When x = 2 , y = 4 , and z = 6 , the condition is 2 * 2 * 4 > 3 * 6 , which is 16 >

18 . This condition is not satisfied.

When x = 3 , y = 2 , and z = 5 , the condition is 2 * 3 * 2 > 3 * 5 , which is 12 >

15 . This condition is not satisfied.

When x = 3 , y = 2 , and z = 6 , the condition is 2 * 3 * 2 > 3 * 6 , which is 12 >

18 . This condition is not satisfied.

When x = 3 , y = 3 , and z = 5 , the condition is 2 * 3 * 3 > 3 * 5 , which is 18 >

15 . This condition is satisfied.

When x = 3 , y = 3 , and z = 6 , the condition is 2 * 3 * 3 > 3 * 6 , which is 18 >

18 . This condition is not satisfied.

When x = 3 , y = 4 , and z = 5 , the condition is 2 * 3 * 4 > 3 * 5 , which is 24 >

15 . This condition is satisfied.

When x = 3 , y = 4 , and z = 6 , the condition is 2 * 3 * 4 > 3 * 6 , which is 24 >

18 . This condition is satisfied.

So, the combinations that satisfy the condition are:

(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]}

Let's evaluate each option:

a. inventory["Amul"] = ["Mystic Mocha",55]: This statement assigns a list ["Mystic Mocha",


55] to the key "Amul" in the dictionary inventory . This operation is valid and will not
generate an error.

b. inventory["Amul, Mystic Mocha"] = 55 : This statement assigns the value 55 to the


key "Amul, Mystic Mocha" in the dictionary inventory . This operation is also valid and
will not generate an error.

c. inventory[["Amul","Mystic Mocha"]] = 55: This statement attempts to use a list


["Amul", "Mystic Mocha"] as a key in the dictionary inventory . However, since lists are
mutable and cannot be used as dictionary keys, this will generate an error.
d. inventory[("Amul","Mystic Mocha")] = 55 : This statement uses a tuple ("Amul", "Mystic

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.

So, the option that generates an error is option c. inventory[["Amul","Mystic Mocha"]] =

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:

#score[i] fetches the match keys info


for j in d[i]:
#score[i][j] print match ke ander player ki value
#j will fetch the player info
if j=="player1":
sum1+=d[i][j]
if j=="player2":
sum2+=d[i][j]
if j=="player3":
sum3+=d[i][j]
if j=="player4":
sum4+=d[i][j]
if d[i][j]>highest:
highest=d[i][j]
pla_high=j

#to find highest total score in the match series


total_score={"player1":sum1,"player2":sum2,"player3":sum3,"pla
highest_total=0
pla_high_total=""
for x in total_score:
if total_score[x]>highest_total:
highest_total=total_score[x]
pla_high_total=x

##fetches player with highest run in the match series


print("Highest run by a player in the whole series")
print("(",pla_high_total,",",highest_total,")")

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]

def addpoly(p1, p2):


result = []
i, j = 0, 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

while i < len(p1):


result.append(p1[i])
i += 1

while j < len(p2):


result.append(p2[j])
j += 1

return cleanup(result)

def multpoly(p1, p2):


result = []

for coef1, exp1 in p1:


for coef2, exp2 in p2:
result.append((coef1 * coef2, exp1 + exp2))

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

You might also like