【问题标题】: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接受一串字母的函数,从有效单词列表中输出单词列表,然后找到拼字游戏得分最高的单词
【发布时间】:2021-05-16 22:21:25
【问题描述】:

我一直在尝试构建一个函数,它接收一串字母,从有效单词列表中输出单词列表,然后从该列表中找到拼字游戏得分最高的单词

我设计了一个从字符串中输出所有可能单词的函数,一个从单词列表中计算拼字游戏分数的函数,以及一个输出最高分数的函数

但是,我正在努力:

  1. 结合三者(计算可能得分最高的输出列表) 和
  2. 使用第二个单词生成函数返回一个包含所有可能单词的列表,目前,它在单独的列表中输出单词

计算拼字游戏分数的功能

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

输出可能单词的函数

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) 

可以从列表中找到得分最高的单词的功能

 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'])

电流输出:

4 me
['me']
5 goal
['goal']

期望的输出:所有可能单词的列表

['me', 'goal']

或一个字典(或类似结构),可能的词作为键,分数作为值

{'me':4, 'goal':5]

AND 得分最高的单词

'goal':5

我需要一种方法从第一个函数返回一个列表,然后将两者结合起来找到该列表中的最高分

保持真棒

【问题讨论】:

  • 我无法重现您的 scrabble_score 函数,存在一些格式错误。请更正
  • 下面的答案解决了你的问题吗?
  • 是的,它绝对完美运行我一直想感谢你——你是一个传奇如果有什么我能为你做的——0742 9191 217
  • 目前我正在解决这个问题。 stackoverflow.com/questions/66201580/… 我的计划是创建许多小的数据透视表,在每个列/问答对上,然后连接,然后自连接 唯一的问题是列中混合了数据类型,因此数据透视表不适用于某些列
  • 哈哈,有点太感谢了! :) 然后您可以接受答案(在答案的投票部分下方打勾)以确认它是解决方案。同时,让我看看是否可以帮助您解决其他问题。

标签: python list function for-loop data-structures


【解决方案1】:

您的函数定义中有一些错误。我已经复制了下面的更正版本,无论我修改了什么地方都有 cmets:

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

使用上面的函数定义和下面的输入:

input_words = ['goo', 'bat', 'me', 'eat', 'goal', 'boy', 'run'] 
charSet = ['e', 'o', 'b', 'a', 'm', 'g', 'l', 'b'] 

possible_words(input_words, charSet) 的输出如下:

{'me': 4, 'goal': 5}
Best word is 'goal' with score 5

,如你所愿。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2019-06-16
    • 2010-10-27
    • 2020-07-21
    相关资源
    最近更新 更多