[英]scrabble word finder in Python
Implement the word_calculator
method to return the correct scrabble word score.实施
word_calculator
方法以返回正确的拼字游戏单词分数。 The scores are already set up to be use and are managed within dictionaries:分数已经设置为可以使用并在字典中进行管理:
two = ['d', 'g']
three = ['b', 'c', 'm', 'p']
four = ['f', 'h', 'v', 'w', 'y']
five = ['k']
eight = ['j', 'x']
ten = ['q', 'z']
with having these list, i need to produce a function def word_calculator(word):
where i need to pass a string to the method.有了这些列表,我需要生成一个 function
def word_calculator(word):
我需要将一个字符串传递给该方法。 the parameter zoo
should return 12
, and bus
should return 5
.参数
zoo
应该return 12
,而bus
应该return 5
。
Can anyone help me to produce the function please?有人可以帮我制作 function 吗?
This, like many problems in computer science, is much easier to do if you use a better data-structure.与计算机科学中的许多问题一样,如果您使用更好的数据结构,这将更容易解决。
As you have it, you will have to loop over all six lists for each letter, check if the letter is in each of them, and then hard code the score for each list programmatically so that you can add it to some total.正如您所拥有的那样,您将必须为每个字母遍历所有六个列表,检查字母是否在每个列表中,然后以编程方式对每个列表的分数进行硬编码,以便您可以将其添加到某个总数中。 That's a lot of work, and a lot of messy code.
这是很多工作,还有很多混乱的代码。
However, you could use a dictionary instead.但是,您可以改用字典。 A dictionary lets you map any value to any other value.
字典可让您将任何值 map 转换为任何其他值。 So, for example
所以,例如
x = {"A": 1, "B": 3}
Means that x["A"]
is 1, and that x["B"]
is 3, etc.意味着
x["A"]
是 1, x["B"]
是 3,等等。
Lets imagine we had a dictionary containing the mapping of every letter to its scrabble value:假设我们有一个字典,其中包含每个字母与其拼字游戏值的映射:
scores = {"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": 1, "R": 1, "S": 1, "T": 1, "U": 1, "V": 4, "W": 4, "X": 8, "Y": 4, "Z": 10}
How would we find the word score for "ZOO"
?我们如何找到
"ZOO"
的单词得分? We would need to:我们需要:
scores[letter]
)scores[letter]
)For example,例如,
total = 0
for letter in "ZOO" :
total = total + score[letter]
print(total) # Prints 12
If you want to be fancy, this can also be done all on the one line using a list comprehension:如果你想花哨一点,这也可以使用列表理解在一行中完成:
sum([scores[letter] for letter in "ZOO"])
Further reading:延伸阅读:
https://docs.python.org/3/tutorial/datastructures.html https://docs.python.org/3/tutorial/datastructures.html
(explains dictionaries very well, and also talks about list comprehensions) (很好地解释了字典,还谈到了列表推导)
# the dictionary of scores to letter lists
score_letters = {
2: ['d', 'g'],
3: ['b', 'c', 'm', 'p'],
4: ['f', 'h', 'v', 'w', 'y'],
5: ['k'],
8: ['j', 'x'],
10: ['q', 'z']
}
# use `score_letters` to make a list from letters to score values
letter_scores = {letter: score for score, letters in score_letters.items() for letter in letters}
# equivalent to:
# letter_scores={}
# for score, letters in score_letters.items():
# for letter in letters:
# letter_scores[letter] = score
print('letter_scores =', letter_scores)
def word_calculator(word):
# sum the scores of each letter in the word
# `letter_scores.get(letter, 1)` means
# "get the value associated with the
# key `letter`, or if there is no such
# key in the dictionary, use the
# default value 1"
return sum(letter_scores.get(letter, 1) for letter in word)
print('zoo', word_calculator('zoo'))
print('bus', word_calculator('bus'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.