简体   繁体   中英

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

I've been trying to build a 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 from this list

I have designed a function that outputs all possible words from a string, a function to calculate scrabble scores from a list of words and a function that outputs the highest score

However, I am struggling to:

  1. Combine the three (to calculate the output list with the highest possible score) and
  2. Return a list containing all possible words with this second word generating function, currently, it outputs words in separate lists

Function that calculates scrabble score

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 that outputs possible words

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 that can find the word with the highest score from a list

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

Current Output:

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

Desired output: A list of all possible words

['me', 'goal']

OR A dictionary (or similar structure) with possible words as keys and score as values

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

AND the word with the highest score

'goal':5

I need a way of returning a list from the first function, and combining the two to find the highest score in that list

Stay awesome

There are a few errors in your function definitions. I have copied the corrected version below, with comments wherever I have modified something:

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

Using the above function definitions and the following input:

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

my output for possible_words(input_words, charSet) is as follows:

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

,which is as desired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM