Skip to content

Commit 0b31be7

Browse files
cactailpybites
authored andcommitted
Finished PCC01 (pybites#207)
1 parent 67f0763 commit 0b31be7

File tree

2 files changed

+235918
-0
lines changed

2 files changed

+235918
-0
lines changed

01/cactail/wordvalue.py

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

0 commit comments

Comments
 (0)