File tree Expand file tree Collapse file tree 2 files changed +235918
-0
lines changed Expand file tree Collapse file tree 2 files changed +235918
-0
lines changed Original file line number Diff line number Diff line change
1
+ from data import DICTIONARY , LETTER_SCORES
2
+
3
+
4
+ def load_words ():
5
+ """Load dictionary into a list and return list"""
6
+ file_ = DICTIONARY
7
+ with open (file_ ) as f :
8
+ words = f .readlines ()
9
+ return [word .strip () for word in words ]
10
+
11
+
12
+ def calc_word_value (word ):
13
+ """Calculate the value of the word entered into function
14
+ using imported constant mapping LETTER_SCORES"""
15
+ value = 0
16
+ for letter in word :
17
+ if letter .isalpha ():
18
+ value += LETTER_SCORES [letter .capitalize ()]
19
+ return value
20
+
21
+
22
+ def max_word_value (list_of_words = load_words ()):
23
+ """Calculate the word with the max value, can receive a list
24
+ of words as arg, if none provided uses default DICTIONARY"""
25
+ word_position = max ([(pos , calc_word_value (word ))
26
+ for pos , word
27
+ in enumerate (list_of_words )], key = lambda x : x [1 ])[0 ]
28
+ return list_of_words [word_position ]
29
+
30
+
31
+ if __name__ == "__main__" :
32
+ pass # run unittests to validate
You can’t perform that action at this time.
0 commit comments