求助!大佬们!
题目意思我有点没太理解,想请教一下如何做这道题。题目(中文版的):在掷骰子游戏中,玩家的手由任意数量的随机骰子掷骰组成,并通过以下方式进行估值:
在此游戏中,奔跑是一系列骰子值,从1开始,例如123、12345、1234、1。
每个骰子都是从1开始的骰子游程的一部分,其值等于骰子数。任何骰子的值(作为奔跑的一部分)都将添加到手得分中。
如果一手骰子中没有1,则整手得分为0。
一手骰子可以包含一个以上的骰子。
研究以下示例骰子手及其相应的估值。确保您了解手的价值:
的值为0
的值为2(包含只用骰子进行的一次分析,而只用进行第二次的分析)
的值为21(包含骰子为的一轮)
的值为19(包含三个独立的骰子运行和另一个包含骰子的运行)
的值为37(包含一个骰子为,第二次以运行,第三次以骰子运行
定义get_dice_score()函数,该函数将传递骰子列表并根据上述规则返回手的值。
重要提示:您的代码不应更改参数列表(即,您需要复制参数列表并操纵该副本)。
Test: result:
print("1:", get_dice_score()) 1: 0
print("2:", get_dice_score()) 2: 2
print("3:", get_dice_score()) 3:21
print("4:", get_dice_score()) 4:19
print("5:", get_dice_score()) 5:37
print("6:", get_dice_score([])) 6:0
英文的题目:怕中文有些翻译看不懂
In a dice rolling game a player's hand is made up of any number of random dice rolls and is valued in the following way:
In this game a run is a sequence of dice values starting from 1, e.g. 123, 12345, 1234, 1.
Each dice which is part of a run of dice starting from a 1 has a value which is equivalent to the dice number. The value of any dice which is part of a run is added to the hand score.
If there is no 1 in a hand of dice, the score for the whole hand is 0.
A hand of dice can contain more than one run.
Study the following example hands of dice and their corresponding valuation.Make sure you understand how the hands are valued:
has value 0
has value 2 (contains one run with just the dice and a second run with just )
has value 21 (contains one run with the dice )
has value 19 (contains three separate runs with the dice and a second run with the dice
has value 37 (contains one run with the dice , a second run with and a third run with the dice )
Define the get_dice_score() function which is passed a list of dice rolls and returns the value of the hand according to the rules described above.
IMPORTANT:your code should not change the parameter list (i.e. you need to make a copy of the parameter list and manipulate the copy).
def get_dice_score(listing):
ls = listing[:]
if 1 not in ls:
return 0
ls.sort()
count = []
while 1 in ls:
for i in range(1,len(ls)):
if i in ls:
count.append(i)
ls = ''
continue
break
return sum(count)
print(get_dice_score())
print(get_dice_score())
print(get_dice_score())
print(get_dice_score())
print(get_dice_score())
print(get_dice_score([])) 你不理解题的意思?
就是给你一组骰子数,计算这一组骰子数的得分值
得分规则:
从这一组数中,由1向上连续取数,直到不连续为止,进行求和;
然后从剩下的一组数,再次由1向上连续取数,直到不连续为止,进行求和;
直到无法取数为止,将各次的求和进行相加为最终得分。
看给出的例子
,没有1,所以无法取数,最终得分为0
,第一次取数能得到1,没2,得分1;第二次取数依然只有 1,得分1,;最终得分2
,第一次取数能得到1 2 3 4 5 6,求和为21,剩下的数中没有1了,无法取数,最终得分21
,第一次取数得到1 2 3,求和为6,第二次取数能得到1 2 3 ,求和为6,第三次取数能得到1 2 3 ,求和为6,第四次取数得到1,得分1;最终得分19
def get_dice_score(ls):
score=0
counts=ls.count(1)
for i in range(1,7):
if not i in ls:
break
if counts>ls.count(i):
counts=ls.count(i)
score+=i*counts
return score
print("1:", get_dice_score())
print("2:", get_dice_score())
print("3:", get_dice_score())
print("4:", get_dice_score())
print("5:", get_dice_score())
print("6:", get_dice_score([]))
页:
[1]