Define a Python function "orangecap(d)" that reads a dictionaryd of this form and identifies the player with the highest total score.  Your function should return a pair(playername,topscore) whereplayername is a string, the name of the player with the highest score, andtopscore is an integer, the total score of playername. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Define a Python function "orangecap(d)" that reads a dictionaryd of this form and identifies the player with the highest total score.  Your function should return a pair(playername,topscore) whereplayername is a string, the name of the player with the highest score, andtopscore is an integer, the total score of playername.

We represent scores of batsmen across a sequence of matches in a two level dictionary as follows: {'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91} Each match is identified by a string, as is each player.  The scores are all integers.  The names associated with the matches are not fixed (here they are'match1','match2','match3'), nor are the names of the players.  A player need not have a score recorded in all matches The input will be such that there are never any ties for highest total score.

23rd Aug 2016, 3:27 PM
Gaurav Kumar Tiwari
Gaurav Kumar Tiwari - avatar
3 Answers
0
try something like code below. I think you can handle with proper names by yourself, but the function does it's job, although this solution is not really elegant, there must be something more 'pythonic' games = {'match1': {'plr1': 15, 'plr2': 25}, 'match2': {'plr2': 30, 'plr3': 40}} def findbestplr (d): totalscore = dict() for match in d: for plr in d[match]: if plr in totalscore: totalscore[plr] += d[match][plr] else: totalscore[plr] = d[match][plr] best = max(totalscore, key=totalscore.get) return best, totalscore[best] print(findbestplr(games))
23rd Aug 2016, 8:36 PM
Demeth
Demeth - avatar
0
We represent scores of batsmen across a sequence of matches in a two level dictionary as follows: {'match1':{'player1':57, 'player2':38}, 'match2':{'player3':9, 'player1':42}, 'match3':{'player2':41, 'player4':63, 'player3':91} Each match is identified by a string, as is each player. The scores are all integers. The names associated with the matches are not fixed (here they are 'match1','match2','match3'), nor are the names of the players. A player need not have a score recorded in all matches. Define a Python function orangecap(d) that reads a dictionary d of this form and identifies the player with the highest total score. Your function should return a pair (playername,topscore) where playername is a string, the name of the player with the highest score, and topscore is an integer, the total score of playername. The input will be such that there are never any ties for highest total score. For instance: >>> 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)
13th Sep 2018, 5:10 PM
arpita halder
arpita halder - avatar
0
def orangecap(d): dict2 = {} for k1 in d: for k2 in d[k1]: dict2[k2] = dict2.get(k2, 0) + d[k1][k2] player = max(dict2, key=dict2.get) return player, dict2[player] # Hidden code below import ast def todict(inp): inp = ast.literal_eval(inp) return (inp) def topairoflists(inp): inp = "["+inp+"]" inp = ast.literal_eval(inp) return (inp[0],inp[1]) def tostring(s): lquote = s.find('"') rquote = s.rfind('"') return(s[lquote+1:rquote]) def tolist(s): lbrack = s.find('[') rbrack = s.rfind(']') slist = s[lbrack+1:rbrack].split(',') if slist == ['']: slist = [] else: for i in range(0,len(slist)): slist[i] = int(slist[i]) return(slist) fncall = input() lparen = fncall.find("(") rparen = fncall.rfind(")") fname = fncall[:lparen] farg = fncall[lparen+1:rparen] if fname == "orangecap": arg = todict(farg) print(orangecap(arg),end="") elif fname == "addpoly": (arg1,arg2) = topairoflists(farg) print(addpoly(arg1,arg2),end="") elif fname == "multpoly": (arg1,arg2) = topairoflists(farg) print(multpoly(arg1,arg2),end="") else: print("Function", fname, "unknown")
21st Apr 2020, 2:03 PM
DebRaj Mondal
DebRaj Mondal - avatar