|
| 1 | +from data import DICTIONARY, LETTER_SCORES |
| 2 | +import types |
| 3 | + |
| 4 | +def load_words(): |
| 5 | + """Load dictionary into a list and return list""" |
| 6 | + with open(DICTIONARY) as f: |
| 7 | + return [word.strip() for word in f.read().split()] |
| 8 | + #wordlist = [] |
| 9 | + #if isinstance(openfile, (tuple, list)): |
| 10 | + # for element in openfile: |
| 11 | + # wordlist.append(element.strip()) |
| 12 | + #else: |
| 13 | + # with open(openfile) as file: |
| 14 | + # for line in file: |
| 15 | + # wordlist.append(line.strip()) |
| 16 | + #return wordlist |
| 17 | + |
| 18 | +def calc_word_value(word): |
| 19 | + """Calculate the value of the word entered into function |
| 20 | + using imported constant mapping LETTER_SCORES""" |
| 21 | + return sum(LETTER_SCORES.get(letter.upper(), 0) for letter in word) |
| 22 | + #value = 0 |
| 23 | + #for letter in word: |
| 24 | + # if letter.upper() in LETTER_SCORES: |
| 25 | + # value = value + LETTER_SCORES[letter.upper()] |
| 26 | + #return value |
| 27 | + |
| 28 | +def max_word_value(words=None): |
| 29 | + """Calculate the word with the max value, can receive a list |
| 30 | + of words as arg, if none provided uses default DICTIONARY""" |
| 31 | + if words is None: |
| 32 | + words = load_words() |
| 33 | + #highword = ["",0] |
| 34 | + #for word in words: |
| 35 | + # currentword = [word, calc_word_value(word)] |
| 36 | + # if currentword[1] > highword[1]: |
| 37 | + # highword = currentword |
| 38 | + #return highword[0] |
| 39 | + return max(words, key=calc_word_value) |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + pass |
0 commit comments