3.19python学堂在线,四号习题集
使用伪代码,来检查你的错误,使得设计变得更简单
为什么要使用伪代码?
这是学堂在线的帮助文件
http://www.xuetangx.com/asset-v1:MITx+6_00_1x+sp+type@asset+block/files_ps04_files_WhyPseudocode.pdf
学习问题的好方法,读别人的代码
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:
>>> hand[‘e’] #这种方式可能会导致错误发生
Traceback (most recent call last):
File “”, line 1, in
KeyError: ‘e’
>>> hand.get(‘e’, 0) #这种方式就没有错误
0
设计playHand游戏
第一部分模块,已经给了你,帮助你获得词频和读取单词列表以待查询
# 6.00x Problem Set 4A Template
#
# The 6.00 Word Game
# Created by: Kevin Luu <luuk> and Jenna Wiens <jwiens>
# Modified by: Sarina Canelake <sarina>
#
'''
Scrabble游戏的字母分值如下:
1 A, E, I, L, N, O, R, S, T, U
2 D, G
3 B, C, M, P
4 F, H, V, W, Y
5 K
8 j, X
10 Q, Z
分值是按照字母出现的频率来计分,字母出现的频率越频繁,分值越低
'''
'''
你可以假设输入总是小写字母串或者空串 "".
'''
import random
import string
VOWELS = 'aeiou'
CONSONANTS = 'bcdfghjklmnpqrstvwxyz'
HAND_SIZE = 7
SCRABBLE_LETTER_VALUES = {
'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1,
'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1,
's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10
}
# -----------------------------------
# Helper code
# (you don't need to understand this helper code)
WORDLIST_FILENAME = "words.txt"
def loadWords():
"""
Returns a list of valid words. Words are strings of lowercase letters.
Depending on the size of the word list, this function may
take a while to finish.
"""
print ("Loading word list from file...")
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r')
# wordList: list of strings
wordList = []
for line in inFile:
wordList.append(line.strip().lower())
print (" ", len(wordList), "words loaded.")
return wordList
def getFrequencyDict(sequence):
"""
Returns a dictionary where the keys are elements of the sequence
and the values are integer counts, for the number of times that
an element is repeated in the sequence.
sequence: string or list
return: dictionary
"""
# freqs: dictionary (element_type -> int)
freq = {
}
for x in sequence:
freq[x] = freq.get(x,0) + 1
return freq
# (end of helper code)
# -----------------------------------
#
# Problem #1: Scoring a word
#
获取字符串模块
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
"""
for i in word:
assert (i in string.ascii_lowercase or ''),'必须输入小写字母或空字符串'
score=0
for l in word:
SCRABBLE_LETTER_VALUES[l]
print('The score of letter ',l,' is',SCRABBLE_LETTER_VALUES[l])
score+=SCRABBLE_LETTER_VALUES[l]