-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathWeek4.Py
More file actions
85 lines (69 loc) · 1.92 KB
/
Copy pathWeek4.Py
File metadata and controls
85 lines (69 loc) · 1.92 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''Question 1'''
def orangecap(d):
res={}
for val in d.values():
for plyr,scr in val.items():
if plyr not in res:
res[plyr]=scr
else:
res[plyr]+=scr
k=max(res,key=res.get)
return (k,res[k])
'''
Example:
>>> orangecap({'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91}})
('player3', 100)
>>> orangecap({'test1':{'Ashwin':84, 'Kohli':120}, 'test2':{'ashwin':59, 'Pujara':42}})
('Kohli', 120)'''
'''Question 2'''
def del0(d):
'''Fuction to delete terms with zero coefficient'''
res={}
for key,val in d.items():
if val!=0:
res[key]=val
return res
def format(d):
'''Fuction to convert dictionary into list as per decreasing power'''
li=[]
for key,val in sorted(d.items(),reverse=True):
li.append((val,key))
return li
def multpoly(p1,p2):
'''Fuction to multiply two polynomial'''
res={}
for exp1,pwr1 in p1:
for exp2,pwr2 in p2:
if pwr1+pwr2 not in res:
res[pwr1+pwr2]=exp1*exp2
else:
res[pwr1+pwr2]+=exp1*exp2
res=del0(res)
return(format(res))
def addpoly(p1,p2):
'''Function to add two polynomial'''
res={}
for exp1,pwr1 in p1:
for exp2,pwr2 in p2:
if pwr1==pwr2:
res[pwr1]=exp1+exp2
for exp,pwr in p1:
if pwr not in res:
res[pwr]=exp
for exp,pwr in p2:
if pwr not in res:
res[pwr]=exp
res=del0(res)
return(format(res))
'''
Example:
>>> addpoly([(4,3),(3,0)],[(-4,3),(2,1)])
[(2, 1),(3, 0)]
Explanation: (4x^3 + 3) + (-4x^3 + 2x) = 2x + 3
>>> addpoly([(2,1)],[(-2,1)])
[]
Explanation: 2x + (-2x) = 0
>>> multpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)])
[(1, 3),(-1, 0)]
Explanation: (x - 1) * (x^2 + x + 1) = x^3 - 1
'''