Skip to content

Commit 082efba

Browse files
authored
Merge pull request #845 from NNRepos/community
PCC01 NNRepos
2 parents cf1d6b8 + e98d074 commit 082efba

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

01/NNRepos/wordvalue.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from data import DICTIONARY, LETTER_SCORES
2+
from typing import Optional, List
3+
4+
5+
def load_words() -> List[str]:
6+
"""Load dictionary into a list and return list"""
7+
with open(DICTIONARY) as f:
8+
return [line.strip() for line in f.readlines()]
9+
10+
11+
def calc_word_value(word: str) -> int:
12+
"""Calculate the value of the word entered into function
13+
using imported constant mapping LETTER_SCORES"""
14+
return sum(LETTER_SCORES[letter] for letter in list(word.replace('-', '').upper()))
15+
16+
17+
def max_word_value(dictionary: Optional[List[str]] = None) -> str:
18+
"""Calculate the word with the max value, can receive a list
19+
of words as arg, if none provided uses default DICTIONARY"""
20+
if dictionary is None:
21+
dictionary = load_words()
22+
print("crap:", [x for x in dictionary if "-" in x])
23+
return list(sorted(([calc_word_value(word), word] for word in dictionary), reverse=True))[0][1]

0 commit comments

Comments
 (0)