From 16b8d5a3790b2c420a4f0be266fc2f7bb6304610 Mon Sep 17 00:00:00 2001 From: Yoav Caspi Date: Fri, 5 Jan 2018 14:14:31 +0200 Subject: [PATCH 1/3] initial copy from the template. --- 01/yoavcaspi/wordvalue.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 01/yoavcaspi/wordvalue.py diff --git a/01/yoavcaspi/wordvalue.py b/01/yoavcaspi/wordvalue.py new file mode 100644 index 000000000..f92b2eb87 --- /dev/null +++ b/01/yoavcaspi/wordvalue.py @@ -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 From f15184688e892f35f840785769c17b79aa47127c Mon Sep 17 00:00:00 2001 From: Yoav Caspi Date: Fri, 5 Jan 2018 14:39:13 +0200 Subject: [PATCH 2/3] adding misc-files to my directory in order to test my code. --- 01/{clamytoe => yoavcaspi}/dictionary.txt | 0 01/{ => yoavcaspi}/test_wordvalue.py | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) rename 01/{clamytoe => yoavcaspi}/dictionary.txt (100%) rename 01/{ => yoavcaspi}/test_wordvalue.py (90%) diff --git a/01/clamytoe/dictionary.txt b/01/yoavcaspi/dictionary.txt similarity index 100% rename from 01/clamytoe/dictionary.txt rename to 01/yoavcaspi/dictionary.txt diff --git a/01/test_wordvalue.py b/01/yoavcaspi/test_wordvalue.py similarity index 90% rename from 01/test_wordvalue.py rename to 01/yoavcaspi/test_wordvalue.py index 0a2ae9bb8..dc4f890ae 100644 --- a/01/test_wordvalue.py +++ b/01/yoavcaspi/test_wordvalue.py @@ -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) @@ -25,4 +25,4 @@ def test_max_word_value(self): self.assertEqual(max_word_value(), 'benzalphenylhydrazone') if __name__ == "__main__": - unittest.main() + unittest.main() From 9bd5ceb9c5f1e1467e955edf4ef2555fa420ae04 Mon Sep 17 00:00:00 2001 From: Yoav Caspi Date: Fri, 5 Jan 2018 14:42:33 +0200 Subject: [PATCH 3/3] initial implementation passing all tests. --- 01/yoavcaspi/wordvalue.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/01/yoavcaspi/wordvalue.py b/01/yoavcaspi/wordvalue.py index f92b2eb87..f0ea08757 100644 --- a/01/yoavcaspi/wordvalue.py +++ b/01/yoavcaspi/wordvalue.py @@ -1,18 +1,33 @@ +import os from data import DICTIONARY, LETTER_SCORES def load_words(): """Load dictionary into a list and return list""" - pass + 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(): +def calc_word_value(word): """Calculate the value of the word entered into function using imported constant mapping LETTER_SCORES""" - pass + return sum(LETTER_SCORES[c.upper()] if c.isalpha() else 0 for c in word) -def max_word_value(): +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""" - pass + 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