Skip to content

Commit

Permalink
'PCC01\ChronoHat' (pybites#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChronoHat authored and pybites committed Jan 7, 2019
1 parent c0cb343 commit 700dc7c
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions 01/ChronoHat/wordvalue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from data import DICTIONARY, LETTER_SCORES

def load_words():
"""Load dictionary into a list and return list"""

with open(DICTIONARY) as d:
words = d.read().splitlines()

return words

def calc_word_value(word):
"""Calculate the value of the word entered into function
using imported constant mapping LETTER_SCORES"""

'Initialize score, capitalize word'
score = 0
word = word.upper()

'Add score per letter'
for letter in word:
if letter.isalpha():
score += LETTER_SCORES[letter]

return score

def max_word_value(word_list = load_words()):
"""Calculate the word with the max value, can receive a list
of words as arg, if none provided uses default DICTIONARY"""

'Initialize high score'
max_score = 0

'Calculate the score for each word'
for word in word_list:
score = calc_word_value(word)

'Capture the largest word (so far) and its score'
if score > max_score:
max_score = score
best_word = word

return best_word

if __name__ == "__main__":
import test_wordvalue as test
test()

0 comments on commit 700dc7c

Please sign in to comment.