forked from pybites/challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from data import DICTIONARY, LETTER_SCORES | ||
|
||
def load_words(): | ||
"""Load dictionary into a list and return list""" | ||
with open(DICTIONARY, 'r') as f: | ||
wordlist = f.read().splitlines() | ||
return wordlist | ||
|
||
def calc_word_value(word): | ||
"""Calculate the value of the word entered into function | ||
using imported constant mapping LETTER_SCORES""" | ||
value = 0 | ||
for letter in word: | ||
value += LETTER_SCORES.get(letter.upper(),0) | ||
return value | ||
|
||
def max_word_value(wordlist=None): | ||
"""Calculate the word with the max value, can receive a list | ||
of words as arg, if none provided uses default DICTIONARY""" | ||
if wordlist is None: | ||
wordlist = load_words() | ||
word_values = {} | ||
for word in wordlist: | ||
word_values[word] = calc_word_value(word) | ||
return max(word_values.iterkeys(), key=(lambda key: word_values[key])) | ||
|
||
if __name__ == "__main__": | ||
pass # run unittests to validate |