Skip to content

Commit e20fcba

Browse files
hyojjxipitugpybites
authored andcommitted
PCC01 benoitroman (pybites#269)
1 parent 9eaf9b9 commit e20fcba

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

01/benoitroman/wordvalue.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

0 commit comments

Comments
 (0)