def calc_word_value(word):
    """given a word calculate its value using LETTER_SCORES"""
    word = word.upper() # makes the word all upper case
    word = list(word) # breaks the word into a list so each char can be compared
    word_score = 0 # var for total score

    for k, v in LETTER_SCORES.items(): # for loop for each key and corresponding value in the dict
        for chars in word: # to iterate over each character in the given word
            if chars in k: # if the chars in word are in the dict keys
                word_score += v # add the corresponding dict value to word_score

    return word_score # return the final word_score
Exemple #2
0
def calc_word_value(word):
    """Calc a given word value based on Scrabble LETTER_SCORES mapping"""
    #return sum(LETTER_SCORES.get(char.upper(), 0) for char in word)
    #word = word.upper()  # makes the word all upper case if it's not already
    #word = list(word)  # breaks the word into a list so each char can be compared
    word_score = 0  # var for total score

    for k, v in LETTER_SCORES.items(
    ):  # for loop for each key and corresponding value in the dict
        for chars in word:  # to iterate over each character in the given word
            if chars in k:  # if the chars in word are in the dict keys
                word_score += v  # add the corresponding dict value to word_score

    return word_score  # return the final word_score
def max_word_value(words=None):
    """given a list of words return the word with the maximum word value"""
    words = [x.upper() for x in list(words)]  # converts the tuple to a list and performs upper on it
    max_score = 0
    word_iter = 0
    best_word = ''

    while word_iter < len(words):
        word_score = 0
        word = words.pop()  # removes the last item in the list and assigns it to word as a string
        for k, v in LETTER_SCORES.items():  # for loop for each key and corresponding value in the dict
            for chars in word:  # to iterate over each character in the given word
                if chars in k:  # if the chars in word are in the dict keys
                    word_score += v  # add the corresponding dict value to word_score
                    if word_score > max_score: # this if holds onto the best word and score
                        max_score = word_score
                        best_word = word
        word_iter += 1
    return best_word.lower()  # returns the highest scoring word in lowwer case to appease the tests xP
Exemple #4
0
def max_word_value(words):
    #calculate the highest scoring word of all possible words
    max_score = 0
    best_word = ''
    words_len = len(words)

    while words_len > 0:  #loop iterates over length of list
        word_score = 0
        word = words.pop().upper(
        )  # removes the last item in the list and assigns it to word as a string, upper to match letter scores
        for k, v in LETTER_SCORES.items(
        ):  # for loop for each key and corresponding value in the dict
            for chars in word:  # to iterate over each character in the given word
                if chars in k:  # if the chars in word are in the dict keys
                    word_score += v  # add the corresponding dict value to word_score
                    if word_score > max_score:  # this if holds onto the best word and score
                        max_score = word_score
                        best_word = word
        words_len -= 1
    return best_word.lower()  # returns the highest scoring word in lower case