forked from pybites/challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* PCC01 rmackley * Delete dictionary.txt * Rename wordvalue-template.py to wordvalue.py
- Loading branch information
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
DICTIONARY = 'dictionary.txt' | ||
|
||
scrabble_scores = [(1, "E A O I N R T L S U"), (2, "D G"), (3, "B C M P"), | ||
(4, "F H V W Y"), (5, "K"), (8, "J X"), (10, "Q Z")] | ||
LETTER_SCORES = {letter: score for score, letters in scrabble_scores | ||
for letter in letters.split()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from data import DICTIONARY, LETTER_SCORES | ||
|
||
def load_words(dict): | ||
"""Load dictionary into a list and return list""" | ||
dict_list = [] | ||
with open(dict) as f: | ||
for line in f: | ||
dict_list.append(line.rstrip()) | ||
return (dict_list) | ||
|
||
def calc_word_value(test_str): | ||
"""Calculate the value of the word entered into function | ||
using imported constant mapping LETTER_SCORES""" | ||
score = 0 | ||
for letter in test_str: | ||
if letter not in [".",",","-","_",":",";"]: | ||
score += LETTER_SCORES.get(letter.upper()) | ||
return score | ||
|
||
def max_word_value(words_list=DICTIONARY): | ||
"""Calculate the word with the max value, can receive a list | ||
of words as arg, if none provided uses default DICTIONARY""" | ||
max = 0 | ||
for item in load_words(words_list): | ||
val = calc_word_value(item) | ||
if val > max: | ||
max_str = item | ||
max = val | ||
return max_str | ||
|
||
if __name__ == "__main__": | ||
pass # run unittests to validate |