|
| 1 | +def orangecap(dict1): |
| 2 | + dict2 = {} |
| 3 | + for k1 in dict1: |
| 4 | + for k2 in dict1[k1]: |
| 5 | + dict2[k2] = dict2.get(k2, 0) + dict1[k1][k2] |
| 6 | + player = max(dict2, key=dict2.get) |
| 7 | + return (player, dict2[player]) |
| 8 | + |
| 9 | +def addpoly(p1,p2): |
| 10 | + for i in range(len(p1)): |
| 11 | + for item in p2: |
| 12 | + if p1[i][1] == item[1]: |
| 13 | + p1[i] = ((p1[i][0] + item[0]),p1[i][1]) |
| 14 | + p2.remove(item) |
| 15 | + p3 = p1 + p2 |
| 16 | + for item in (p3): |
| 17 | + if item[0] == 0: |
| 18 | + p3.remove(item) |
| 19 | + return (sorted(p3)) |
| 20 | + |
| 21 | +def takeSecond(elem): |
| 22 | + return (elem[1]) |
| 23 | +def multpoly(p1,p2): |
| 24 | + res=[] |
| 25 | + lenp1=len(p1) |
| 26 | + lenp2=len(p2) |
| 27 | + for i in range(lenp1): |
| 28 | + for j in range (lenp2): |
| 29 | + p=((p1[i][0]*p2[j][0]),(p1[i][1]+p2[j][1])) |
| 30 | + res.append(p) |
| 31 | + return (cancel(res)) |
| 32 | +def cancel(l): |
| 33 | + a=[] |
| 34 | + res=[] |
| 35 | + ret=[] |
| 36 | + for i in l: |
| 37 | + l3=i[1] |
| 38 | + a.append(l3) |
| 39 | + b=(list(set(a))) |
| 40 | + for y in b: |
| 41 | + sum=0 |
| 42 | + for z in l: |
| 43 | + if (y==z[1]): |
| 44 | + sum=sum+z[0] |
| 45 | + res.append(sum) |
| 46 | + for j in range (len(b)): |
| 47 | + if (res[j]==0): |
| 48 | + continue |
| 49 | + else: |
| 50 | + to = (res[j],b[j]) |
| 51 | + ret.append(to) |
| 52 | + return (sorted(ret,key=takeSecond,reverse=True)) |
| 53 | +import ast |
| 54 | + |
| 55 | +def todict(inp): |
| 56 | + inp = ast.literal_eval(inp) |
| 57 | + return (inp) |
| 58 | + |
| 59 | +def topairoflists(inp): |
| 60 | + inp = "["+inp+"]" |
| 61 | + inp = ast.literal_eval(inp) |
| 62 | + return (inp[0],inp[1]) |
| 63 | + |
| 64 | +fncall = input() |
| 65 | +lparen = fncall.find("(") |
| 66 | +rparen = fncall.rfind(")") |
| 67 | +fname = fncall[:lparen] |
| 68 | +farg = fncall[lparen+1:rparen] |
| 69 | + |
| 70 | +if fname == "orangecap": |
| 71 | + arg = todict(farg) |
| 72 | + print(orangecap(arg)) |
| 73 | +elif fname == "addpoly": |
| 74 | + (arg1,arg2) = topairoflists(farg) |
| 75 | + print(addpoly(arg1,arg2)) |
| 76 | +elif fname == "multpoly": |
| 77 | + (arg1,arg2) = topairoflists(farg) |
| 78 | + print(multpoly(arg1,arg2)) |
| 79 | +else: |
| 80 | + print("Function", fname, "unknown") |
| 81 | + |
| 82 | + |
0 commit comments