Skip to content

Commit

Permalink
Pcc01 (pybites#428)
Browse files Browse the repository at this point in the history
* PCC01 Tom-a-Hawk

* PCC01 Tom-a-Hawk

* PCC01 Tom-a-Hawk
  • Loading branch information
Tom-a-Hawk authored and pybites committed Jan 7, 2019
1 parent 82a7ced commit 1d2aeb5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
10 changes: 10 additions & 0 deletions 01/Tom-a-Hawk/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DICTIONARY = "C:/Users/tommy/challenges/01/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()}
30 changes: 30 additions & 0 deletions 01/Tom-a-Hawk/test_wordvalue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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()
35 changes: 35 additions & 0 deletions 01/Tom-a-Hawk/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"""
with open(DICTIONARY) as fin:
x = fin.read().splitlines()
return x

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

def max_word_value(text=None):
"""Calculate the word with the max value, can receive a list
of words as arg, if none provided uses default DICTIONARY"""
if text is None:
x = load_words()
wordScores = [sum([LETTER_SCORES.get(char.upper(), 0) for char in i]) for i in x]
d = dict(zip(x, wordScores))
maximum = max(d, key=d.get)
return maximum
else:

wordScores = [sum([LETTER_SCORES.get(char.upper(), 0) for char in i]) for i in text]
d = dict(zip(text, wordScores))
maximum = max(d, key=d.get)
return maximum

if __name__ == "__main__":
load_words()
calc_word_value()
max_word_value()
#pass # run unittests to validate

0 comments on commit 1d2aeb5

Please sign in to comment.