Skip to content

Commit

Permalink
PCC01 joeriksson
Browse files Browse the repository at this point in the history
  • Loading branch information
J-O Eriksson committed Aug 13, 2019
1 parent 0eda951 commit 6a4077e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 01/joeriksson/test_wordvalue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import unittest

from data import DICTIONARY, LETTER_SCORES
from wordvalue import load_words, calc_word_value, max_word_value

TEST_WORDS = ('bob', 'julian', 'pybites', 'quit', 'barbeque')

class TestWordValue(unittest.TestCase):

def test_load_words(self):
words = load_words()
self.assertEqual(len(words), 235886)
self.assertEqual(words[0], 'A')
self.assertEqual(words[-1], 'Zyzzogeton')
self.assertNotIn(' ', ''.join(words))

def test_calc_word_value(self):
self.assertEqual(calc_word_value('bob'), 7)
self.assertEqual(calc_word_value('JuliaN'), 13)
self.assertEqual(calc_word_value('PyBites'), 14)
self.assertEqual(calc_word_value('benzalphenylhydrazone'), 56)

def test_max_word_value(self):
self.assertEqual(max_word_value(TEST_WORDS), 'barbeque')
self.assertEqual(max_word_value(), 'benzalphenylhydrazone')

if __name__ == "__main__":
unittest.main()
18 changes: 18 additions & 0 deletions 01/joeriksson/wordvalue-template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from data import DICTIONARY, LETTER_SCORES

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

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

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

if __name__ == "__main__":
pass # run unittests to validate
35 changes: 35 additions & 0 deletions 01/joeriksson/wordvalue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from data import DICTIONARY, LETTER_SCORES


def load_words():
"""Load dictionary into a list and return list"""
return_list = []
with open(DICTIONARY, "r") as file:
for line in file:
return_list.append(line.strip("\n"))

return return_list


def calc_word_value(word):
"""Calculate the value of the word entered into function
using imported constant mapping LETTER_SCORES"""
scores = 0
for char in word:
try:
scores += LETTER_SCORES[char.upper()]
except KeyError:
continue

return scores


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

for word in list_of_words:
value_dict[word] = calc_word_value(word)
max_word = sorted(value_dict, key=value_dict.get, reverse=True)
return "".join(max_word[:1])

0 comments on commit 6a4077e

Please sign in to comment.