Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pcc01 #196

Merged
merged 3 commits into from
Mar 24, 2018
Merged

Pcc01 #196

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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