[英]Write a function that takes a list of words as a parameter and that double every word
[英]Function that takes a string of letters, outputs a list of words from a Valid Word List then finds the word with the highest scrabble score
我一直在嘗試構建一個 function,它接受一串字母,從有效單詞列表中輸出單詞列表,然后從該列表中找到拼字游戲得分最高的單詞
我設計了一個 function 從字符串中輸出所有可能的單詞,一個 function 從單詞列表中計算拼字游戲分數,一個 function 輸出最高分數
但是,我正在努力:
計算拼字游戲分數的 Function
def scrabble_score(word):
total = 0 # Create score var
for i in word: # Loop through given word
total += score[i.lower()] #Lookup in dict, add total
return totaldef charCount(word):
dict = {}
for i in word:
dict[i] = dict.get(i, 0) + 1
return dict
Function 輸出可能的話
def possible_words(lwords, charSet):
for word in lwords:
flag = 1
chars = charCount(word)
for key in chars:
if key not in charSet:
flag = 0
elif charSet.count(key) != chars[key]:
flag = 0 #for word in word_list:
if flag == 1:
#word_value_dict = {}
firstList = []
#word_value_dict[word] = get_word_value(word, letter_values)
firstList.append(word)
#return word_value_dict
print(scrabble_score(word), (word))
print(firstList)if __name__ == "__main__":
input = ['goo', 'bat', 'me', 'eat', 'goal', 'boy', 'run']
charSet = ['e', 'o', 'b', 'a', 'm', 'g', 'l', 'b']
possible_words(input, charSet)
Function 可以從列表中找到得分最高的單詞
def score(word):
dic = {"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}
total = 0
for char in word:
total += dic.get(char.upper(), 0)
return total
#return highest score
def best(lista):
return max(lista, key=score)best(['goo', 'bat', 'me', 'eat', 'run'])
當前 Output:
4 me
['me']
5 goal
['goal']
所需的 output:所有可能單詞的列表
['me', 'goal']
或者一個字典(或類似結構),可能的詞作為鍵,分數作為值
{'me':4, 'goal':5]
AND 得分最高的單詞
'goal':5
我需要一種從第一個 function 返回列表的方法,並將兩者結合起來以找到該列表中的最高分
保持好狀況
您的 function 定義中有一些錯誤。 我已經復制了下面的更正版本,並在我修改某些內容的地方添加了注釋:
def scrabble_score(word):
total = 0
for i in word:
total += score(i.lower()) # Changed square brackets to normal brackets
return total
def charCount(word):
dict = {}
for i in word:
dict[i] = dict.get(i, 0) + 1
return dict
def score(word):
dic = {"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}
total = 0
for char in word:
total += dic.get(char.lower(), 0) # Change .upper() to .lower() or else it will return only zero
return total
def possible_words(lwords, charSet):
firstList = {} # Made this a dictionary instead of list
for word in lwords:
flag = 1
chars = charCount(word)
for key in chars:
if key not in charSet:
flag = 0
elif charSet.count(key) != chars[key]:
flag = 0
if flag == 1:
firstList[word] = scrabble_score(word) # Adding to the dictionary
print(firstList)
best(firstList)
def best(lista):
print("Best word is '{}' with score {}".format(max(lista, key=score), lista[max(lista, key=score)])) # Changed as per requirements
使用上述 function 定義和以下輸入:
input_words = ['goo', 'bat', 'me', 'eat', 'goal', 'boy', 'run']
charSet = ['e', 'o', 'b', 'a', 'm', 'g', 'l', 'b']
我的 output for possible_words(input_words, charSet)
如下:
{'me': 4, 'goal': 5}
Best word is 'goal' with score 5
,這是所希望的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.