Skip to content

Commit

Permalink
Pcc01 (pybites#196)
Browse files Browse the repository at this point in the history
* initial copy from the template.

* adding misc-files to my directory in order to test my code.

* initial implementation passing all tests.
  • Loading branch information
yoavcaspi authored and pybites committed Mar 24, 2018
1 parent ce8fa31 commit d9aecd6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
File renamed without changes.
6 changes: 3 additions & 3 deletions 01/test_wordvalue.py → 01/yoavcaspi/test_wordvalue.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import unittest

from data import DICTIONARY, LETTER_SCORES
from wordvalue import load_words, calc_word_value, max_word_value
from yoavcaspi.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)
Expand All @@ -25,4 +25,4 @@ def test_max_word_value(self):
self.assertEqual(max_word_value(), 'benzalphenylhydrazone')

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

def load_words():
"""Load dictionary into a list and return list"""
lines = []
dict_path = os.path.join(os.path.curdir, 'yoavcaspi', DICTIONARY)
with open(dict_path, 'r') as f:
lines = f.read().splitlines()
return lines

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

def max_word_value(list_words = None):
"""Calculate the word with the max value, can receive a list
of words as arg, if none provided uses default DICTIONARY"""
if list_words is None:
list_words = load_words()
max_word = None
max_val = 0
for word in list_words:
cur_val = calc_word_value(word)
if cur_val > max_val:
max_val = cur_val
max_word = word
return max_word


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

0 comments on commit d9aecd6

Please sign in to comment.