Python 100 project #43: PyBites “Code Challenge 01 – Word Values Part I”

This time I try PyBites Code Challenge as a part of my 100 python.

The Challenge objective is as follows:

Read in dictionary.txt (a copy of /usr/share/dict/words on my Mac) and calculate the word that has the most value in Scrabble based on LETTER_SCORES which is imported in

This is my code:


def load_words(word_dict):
    """Load dictionary into a list and return list"""
    words = []
    with open(word_dict, 'r') as f:
    return words

def calc_word_value(word):
    """Calculate the value of the word entered into function
    using imported constant mapping LETTER_SCORES"""
    score = 0
    for c in word:
        if c.isalpha():
            score += LETTER_SCORES[c.upper()]
    return score

def max_word_value(word_dict=DICTIONARY):
    """Calculate the word with the max value, can receive a list
    of words as arg, if none provided uses default DICTIONARY"""
    words = load_words(word_dict)
    max_word, max_val = "", 0
    for word in words:
        val = calc_word_value(word)
        if val > max_val:
            max_word, max_val = word, val
    return max_word, max_val

if __name__ == "__main__":
    max_word, max_val = max_word_value()
    print(f"{max_word} has the biggest sum of {max_val}.")


It’s quite simple and I received the result in sub-second.