打卡记录
- 学号:1901090022
- 学习课程:Introduction to Computer Science and Programming in Python
- 学习内容:Problem Set 3 (ZIP)
- 打卡天数:D08
作业(Problem 1: Word scores):
The first step is to implement a function that calculates the score for a single word. Fill in the code for get_word_score in ps3.py according to the function specifications.
As a reminder, here are the rules for scoring a word:
- The score for a word is the product of two components:
- First component: the sum of the points for letters in the word.
- Second component: either [7 * word_length - 3 * (n-word_length)] or 1, whichever value is greater, where:
- word_length is the number of letters used in the word
- n is the number of letters available in the current hand
You should use the SCRABBLE_LETTER_VALUES dictionary defined at the top of ps3.py. Do not assume that there are always 7 letters in a hand! The parameter n is the total number of letters in the hand when the word was entered.
Finally, you may find the str.lower function helpful:
1 | s = “My string” |
If you don’t know what this does you could try typing help(str.lower) in your Spyder shell to see the documentation for the functions.
作业心得
这里有个2关键点:
- [7 * word_length - 3 * (n-word_length)],如果计算值小于0,则 Second component 为1
- 一定要认真看 Introduction章节的Scoring部分,从中得知这道题是要计算 First component 和 Second componen 相乘的结果
- 要认真看 test_ps3.py 的 test_get_word_score 函数,参考这个函数可以知道如何测试
关键函数的理解
deal_hand(n):
该函数返回一个字典类型数据,包括 ceil(n/3) 个元音和若干个辅音与对应的次数,元音与辅音可重复
update_hand(hand, word):
将存在 hand字典中 key 为 word 的元素删除并返回,但不修改原 hand 变量
学习的新函数:
程序代码
1 | # 6.0001 Problem Set 3 |