File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ from data import DICTIONARY , LETTER_SCORES
2
+ import functools as ft
3
+
4
+
5
+ def load_words ():
6
+ """Load dictionary into a list and return list"""
7
+ words = []
8
+ with open (DICTIONARY , 'r' ) as input :
9
+ for line in input :
10
+ line = line .strip ()
11
+ if line != '' :
12
+ words .append (line )
13
+ return words
14
+
15
+
16
+ def calc_word_value (input ):
17
+ """Calculate the value of the word entered into function
18
+ using imported constant mapping LETTER_SCORES"""
19
+ return ft .reduce (lambda x , y : x + LETTER_SCORES .get (y .upper (), 0 ),
20
+ input , 0 )
21
+
22
+
23
+ def max_word_value (dictionary = None ):
24
+ """Calculate the word with the max value, can receive a list
25
+ of words as arg, if none provided uses default DICTIONARY"""
26
+ dictionary = dictionary if dictionary is not None else load_words ()
27
+ max_word = ''
28
+ max_value = 0
29
+ for w in dictionary :
30
+ value = calc_word_value (w )
31
+ if value > max_value :
32
+ max_word = w
33
+ max_value = value
34
+ # print(f'new max value found: {max_word} => {max_value}')
35
+ return max_word
You can’t perform that action at this time.
0 commit comments