使用Python的橙色帽子 [英] Orange cap using Python

查看:70
本文介绍了使用Python的橙色帽子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在两级词典中代表一系列击球手的得分如下:



{'match1':{'player1':57,' player2':38},'match2':{'player3':9,'player1':42},'match3':{'player2':41,'player4':63,'player3':91}



每个匹配都由一个字符串标识,每个玩家都是如此。分数都是整数。与比赛相关联的名称不固定(这里是'match1','match2','match3'),也不是球员的名字。玩家不需要在所有比赛中都记录得分。



定义一个Python函数orangecap(d),它读取此表单的字典d并使用最高总分。你的函数应该返回一对(playername,topscore),其中playername是一个字符串,得分最高的玩家的名字,topscore是一个整数,玩家名称的总得分。



输入将是最高总分从来没有任何联系。



例如:



>>> 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)



我的尝试:



l = []

p = []

s1 = []

s2 = []

def orangecap(d) :

t = 0

temp = -1

如果len(d)== 0:

return { }

for d in d:

for x in d [z]:

p.append(x)

a = d [z] [x]

l.append(a)

for i in p:

temp = temp + 1

如果len(s1)== 0:

s1.append(i)

s2.append(l [0])



否则:

范围内的j(len(s1)):

如果i == s1 [j ]:

s2 [j] = s2 [j] + l [temp]

t = t + 1

如果t == 0:

s1.ap pend(i)

s2.append(l [p.index(i)])

t = 0

返回s1 [s2.index (max(s2))],max(s2)



我做了整个程序,但我仍然可以只清除10个10个测试用例。

即使我以不同的方式尝试了它,但它仍然只清除了9个私人测试用例10

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)

What I have tried:

l=[]
p=[]
s1=[]
s2=[]
def orangecap(d):
t=0
temp=-1
if len(d)==0:
return {}
for z in d:
for x in d[z]:
p.append(x)
a=d[z][x]
l.append(a)
for i in p:
temp=temp+1
if len(s1)==0:
s1.append(i)
s2.append(l[0])

else:
for j in range(len(s1)):
if i==s1[j]:
s2[j]=s2[j]+l[temp]
t=t+1
if t==0:
s1.append(i)
s2.append(l[p.index(i)])
t=0
return s1[s2.index(max(s2))],max(s2)

I did the whole Program but still i can Clear only the 9 Testcases of 10.
Even I tried it in different ways but still it clears only 9 Private Testcases of 10

推荐答案

引用:

我做了整个程序,但我仍然可以只清除10个10个测试用例。

I did the whole Program but still i can Clear only the 9 Testcases of 10.



看你的代码表现怎么样?



您的代码没有按照您的预期行事,或者您不明白为什么!



几乎是通用的解决方案:一步一步地在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的代码是什么反对,它没有发现错误,只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]

调试器只向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。


What about watching your code perform ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于使用Python的橙色帽子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆