Problem 6 (200pts)
实现always_roll,这是一个高阶函数,它接受一个骰子数n并返回一个总是掷n个骰子的战略。因此,always_roll(5)将等同于always_roll_5。
在编写任何代码之前,请解锁测试以验证您对问题的理解:
python ok -q 06 -u
解锁完成后,开始实现您的解决方案。您可以使用以下命令检查正确性:
python ok -q 06
Solutions
def always_roll(n):
"""Return a strategy that always rolls N dice.
A strategy is a function that takes two total scores as arguments (the
current player's score, and the opponent's score), and returns a number of
dice that the current player will roll this turn.
>>> strategy = always_roll(3)
>>> strategy(0, 0)
3
>>> strategy(99, 99)
3
"""
assert n >= 0 and n <= 10
# BEGIN PROBLEM 6
return lambda x, y: n
# END PROBLEM 6