Skip to content

Commit

Permalink
PCC01 rmackley (pybites#308)
Browse files Browse the repository at this point in the history
* PCC01 rmackley

* Delete dictionary.txt

* Rename wordvalue-template.py to wordvalue.py
  • Loading branch information
rmackley authored and pybites committed Sep 18, 2018
1 parent a38165f commit 86eb7da
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 01/rmackley/data.py
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()}
32 changes: 32 additions & 0 deletions 01/rmackley/wordvalue.py
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

0 comments on commit 86eb7da

Please sign in to comment.