Skip to content

Commit

Permalink
PC001 completed. Tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
nonickid committed Jun 8, 2019
1 parent 4d7c0a0 commit 5637938
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 01/nonickid/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()}
28 changes: 28 additions & 0 deletions 01/nonickid/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()
22 changes: 22 additions & 0 deletions 01/nonickid/wordvalue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from data import DICTIONARY, LETTER_SCORES

def load_words():
"""Load dictionary into a list and return list"""
with open('../' + DICTIONARY) as fn:
return [word.strip() for word in fn.readlines()]

def calc_word_value(word):
"""Calculate the value of the word entered into function
using imported constant mapping LETTER_SCORES"""
return sum([ LETTER_SCORES.get(key.upper(), 0) for key in word])

def max_word_value(words=None):
"""Calculate the word with the max value, can receive a list
of words as arg, if none provided uses default DICTIONARY"""
if words is None:
words = load_words()
return max([ (word, calc_word_value(word)) for word in words ], key=lambda x: x[1])[0]


if __name__ == "__main__":
pass # run unittests to validate

0 comments on commit 5637938

Please sign in to comment.