[英]Python - scrabble word count
I need to write a short program for scrabble where given a word it can calculate the score.我需要为拼字游戏编写一个简短的程序,其中给定一个单词可以计算分数。
I am new to python and still figuring out how to combine lists.我是 python 的新手,仍在弄清楚如何组合列表。 Any help or an explanation to the solution would be much appreciated.
对解决方案的任何帮助或解释将不胜感激。
I think i have to state that each list is equal to a number and then using that work out how to look at a word and calculate that score.我想我必须声明每个列表都等于一个数字,然后使用它来计算如何查看一个单词并计算该分数。
Would anyone be able to give me a solution and talk me through the various points?有没有人能给我一个解决方案,并通过各个方面和我交谈?
one_letter_point = ['e', 'a', 'o', 't', 'i', 'n', 'r', 's', 'l', 'u']
two_letter_point = ['d', 'g']
three_letter_point = ['c', 'm', 'b', 'p']
four_letter_point = ['h', 'f', 'w', 'y', 'v']
five_letter_point = ['k']
eight_letter_point = ['j', 'x']
ten_letter_point = ['q', 'z']
def scrabble_word_count(word):
one_letter_word = 1 #do the same for the rest and then not sure what to do
return scrabble_word_count(word) # not sure if this is supposed to be done
print answer_word('zoo')
I have this alternate method, but it only looks at the first letter and gives me the point for that.我有这个替代方法,但它只查看第一个字母并给出了这一点。 eg points for apple = 1. as it only looks at the a.
例如,苹果的点数 = 1。因为它只看 a。
def scrabble_word_count(word):
score = 0
for letter in word:
if letter in one_letter_point:
score += 1
elif letter in two_letter_point:
score += 2
elif letter in three_letter_point:
score += 3
elif letter in four_letter_point:
score += 4
elif letter in five_letter_point:
score += 5
elif letter in eight_letter_point:
score += 8
elif letter in ten_letter_point:
score += 10
return score
print(scrabble_word_count('apple'))
You need to specify the correspondance between letters and value, the better to get quick access is to have a simple correspondance for each letter.您需要指定字母和值之间的对应关系,为了快速访问,每个字母都有一个简单的对应关系。
score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
Then for each letter of the word, find it in the score dict
and sum all然后对于单词的每个字母,在
score dict
找到它并求和所有
# simple for loop style
def scrabble_word_count(word):
res = 0
for letter in word:
res += score[letter]
return res
# functionnal style
def scrabble_word_count(word):
return sum(map(score.get, word))
Using separate lists :使用单独的列表:
one_letter_point = ['e', 'a', 'o', 't', 'i', 'n', 'r', 's', 'l', 'u']
two_letter_point = ['d', 'g']
three_letter_point = ['c', 'm', 'b', 'p']
four_letter_point = ['h', 'f', 'w', 'y', 'p']
five_letter_point = ['k']
eight_letter_point = ['j']
ten_letter_point = ['q', 'z']
def scrabble_word_count(word):
res = 0
for letter in word:
if letter in one_letter_point:
res += 1
elif letter in two_letter_point:
res += 2
elif letter in three_letter_point:
res += 3
elif letter in four_letter_point:
res += 4
elif letter in five_letter_point:
res += 5
elif letter in eight_letter_point:
res += 8
elif letter in ten_letter_point:
res += 10
return res
Good start, but your data structure creates a problem.好的开始,但是您的数据结构产生了问题。 Okay, so with
five_letter_point = ['k']
, we know that a 'k' is worth five points.好的,所以有了
five_letter_point = ['k']
,我们知道一个 'k' 值五分。 How can we extract that data from the fact that it's stored under a variable name containing the word "five"?我们如何从数据存储在包含单词“five”的变量名下这一事实中提取该数据? We really can't.
我们真的不能。
Let's use a dict
instead, which is also known outside the context of Python as a hashmap.让我们改用
dict
,它在 Python 的上下文之外也称为哈希图。 For example with secret_identities = {"Batman": "Bruce Wayne", "Kal-El": "Superman"}
you can look up secret_identities['Batman']
and learn that Batman is Bruce Wayne.例如,使用
secret_identities = {"Batman": "Bruce Wayne", "Kal-El": "Superman"}
您可以查找secret_identities['Batman']
并了解蝙蝠侠是布鲁斯·韦恩。 Or in your case:或者在你的情况下:
letter_scores = {
'e': 1,
'a': 1,
...
'd': 2,
...
}
Now the scores are stored as a dict, which maps each letter to the number of points that it gets.现在分数被存储为一个字典,它将每个字母映射到它得到的点数。 For instance, you can look the score for
'e'
up with letter_scores['e']
, which should return the number 1
.例如,您可以使用
letter_scores['e']
查找'e'
的分数,它应该返回数字1
。
Use multiple if
statements to add to the appropriate value to the score depending on which list the letter is in.根据字母所在的列表,使用多个
if
语句将适当的值添加到分数中。
def scabble_word_count(word):
score = 0
for letter in word:
if letter in one_letter_point:
score += 1
elif letter in two_letter_point:
score += 2
elif letter in three_letter_point:
score += 3
...
return score
You can avoid all the repeated code by putting all the letter lists into another list.您可以通过将所有字母列表放入另一个列表来避免所有重复的代码。 Use empty lists for the points that don't have any letters.
对没有任何字母的点使用空列表。
all_letters = [
one_letter_point, two_letter_point, three_letter_point, four_letter_point, five_letter_point,
[], [], [], eight_letter_point, [], ten_letter_point
]
def scrabble_word_count(word):
score = 0
for letter in word:
for index, letters in enumerate(all_letters):
if letter in letters:
score += index + 1 # indexes start at 0, points start at 1
break
return score
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.