MIT 6.00.1x 计算机科学和Python编程导论 Set 4

Word Scores 为单词算分值

感谢 glhezjnucn 童鞋的给力翻译!!
The first step is to implement some code that allows us to calculate the score for a single word. The function getWordScore should accept as input a string of lowercase letters (a word) and return the integer score for that word, using the game’s scoring rules.
代码设计的第一步是让我们为一个单词计算它的得分。函数getWordScore 接收一个小写字母串 (一个单词),然后按照游戏的积分规则返回它的得分值 。

Hints

  • You may assume that the input word is always either a string of lowercase letters, or the empty string “”. 你可以假设输入总是小写字母串或者空串 “”.

  • You will want to use the SCRABBLE_LETTER_VALUES dictionary defined at the top of ps4a.py. You should not change its value. 你可能要用到SCRABBLE_LETTER_VALUES词典常量,文件ps4a.py开始部分中定义的. 不可以改变它的值.

  • Do not assume that there are always 7 letters in a hand! The parameter n is the number of letters required for a bonus score (the maximum number of letters in the hand). Our goal is to keep the code modular - if you want to try playing your word game with n=10 or n=4, you will be able to do it by simply changing the value of HAND_SIZE! 不能假定每手总是7张牌! 参数 n 用于判断是否给奖励分(手上牌的最大张数). 我们的目标是模块化设计- 你可以玩n=10 或 n=4的游戏, 只须更改 HAND_SIZE的值来达到这个目的!

  • Testing: If this function is implemented properly, and you run test_ps4a.py, you should see that the test_getWordScore() tests pass. Also test your implementation of getWordScore, using some reasonable English words. 测试: 如果函数被正确的编写,你运行test_ps4a.py, 你将看到test_getWordScore() 检测通过. 用你的函数getWordScore检测一些合理单词的分值.

def getWordScore(word, n):
    """
    Returns the score for a word. Assumes the word is a valid word.

    The score for a word is the sum of the points for letters in the
    word, multiplied by the length of the word, PLUS 50 points if all n
    letters are used on the first turn.

    Letters are scored as in Scrabble; A is worth 1, B is worth 3, C is
    worth 3, D is worth 2, E is worth 1, and so on (see SCRABBLE_LETTER_VALUES)

    word: string (lowercase letters)
    n: integer (HAND_SIZE; i.e., hand size required for additional points)
    returns: int >= 0
    """
    letter_score,extra_score = 0,0
    for letter in word:
        letter_score +=SCRABBLE_LETTER_VALUES[letter]
    if len(word) == n:
        extra_score = 50
    return letter_score * len(word) + extra_score

Dealing With Hands 一手牌的处理过程

Please read this problem entirely!!请完整阅读这个问题!! The majority of this problem consists of learning how to read code, which is an incredibly useful and important skill. At the end, you will implement a short function. Be sure to take your time on this problem - it may seem easy, but reading someone else’s code can be challenging and this is an important exercise.这个问题的一个重要目的是学习如何去阅读代码,这是相当有用且重要的技能。在结尾,你将写一个短小的函数。在这个问题上你该确保花点时间-它看起来简单,但读别人的代码又是具有挑战性的,这是一次很重要的练习。

Representing hands 表示一手牌

A hand is the set of letters held by a player during the game. The player is initially dealt a set of random letters. For example, the player could start out with the following hand: a, q, l, m, u, i, l. In our program, a hand will be represented as a dictionary: the keys are (lowercase) letters and the values are the number of times the particular letter is repeated in that hand. For example, the above hand would be represented as:
一手牌是指游戏过程中玩家手上的字母集合。玩家最初被随机分配到一手牌。例如玩家手中开始时的牌: a, q, l, m, u, i, l. 在我们的程序中,一手牌,表示为一个词典变量: 小写字母作为键值,对应的值是字母出现的次数,上面的这手字母牌表示为:
hand = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1}
Notice how the repeated letter ‘l’ is represented. Remember that with a dictionary, the usual way to access a value is hand[‘a’], where ‘a’ is the key we want to find. However, this only works if the key is in the dictionary; otherwise, we get a KeyError. To avoid this, we can use the call hand.get(‘a’,0). This is the “safe” way to access a value if we are not sure the key is in the dictionary. d.get(key,default) returns the value for key if key is in the dictionary d, else default. If default is not given, it returns None, so that this method never raises a KeyError. For example:
注意重复字母l的表示.记住从词典变量中得到一个值通常的方法是hand[‘a’], 其中’a’ 是我们要找的键值.不过,这个方式只有当键值在词典中的时候才可以,否则会导致KeyError错误.避免这个,可以用hand.get(‘a’,0). 这种形式比较安全些,如果我们不太确定键值是否在词典中. d.get(key,default)将返回对应值(键值存在),或者返回缺省值(这里是0),如果不给缺省值,那返回None,因此这个方法不会引发KeyError. 例如:

>>> hand['e']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'e'
>>> hand.get('e', 0)
0
Converting words into dictionary representation

One useful function we’ve defined for you is getFrequencyDict, defined near the top of ps4a.py. When given a string of letters as an input, it returns a dictionary where the keys are letters and the values are the number of times that letter is represented in the input string. For example:
我们已经为你设计了一个有用的函数getFrequencyDict,给它一个字母串,它返回这个字母串所表示的一个词典(即一手牌)

>>> getFrequencyDict("hello")
{
  'h': 1, 'e': 1, 'l': 2, 'o': 1}

As you can see, this is the same kind of dictionary we use to represent hands. 这就是一手牌的表示形式。

Displaying a hand 显示一手牌

Given a hand represented as a dictionary, we want to display it in a user-friendly way. We have provided the implementation for this in the displayHand function. Take a few minutes right now to read through this function carefully and understand what it does and how it works.
以词典形式给定一手牌之后,我们想将牌以用户-友好的形式显示出来,我们为你提供了函数displayHand. 现在就花几分钟去仔细阅读这个函数,看看它是怎么工作的。

G
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值