diff --git a/01/joeriksson/test_wordvalue.py b/01/joeriksson/test_wordvalue.py new file mode 100644 index 000000000..0a2ae9bb8 --- /dev/null +++ b/01/joeriksson/test_wordvalue.py @@ -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() diff --git a/01/joeriksson/wordvalue-template.py b/01/joeriksson/wordvalue-template.py new file mode 100644 index 000000000..f92b2eb87 --- /dev/null +++ b/01/joeriksson/wordvalue-template.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 diff --git a/01/joeriksson/wordvalue.py b/01/joeriksson/wordvalue.py new file mode 100644 index 000000000..536d40381 --- /dev/null +++ b/01/joeriksson/wordvalue.py @@ -0,0 +1,35 @@ +from data import DICTIONARY, LETTER_SCORES + + +def load_words(): + """Load dictionary into a list and return list""" + return_list = [] + with open(DICTIONARY, "r") as file: + for line in file: + return_list.append(line.strip("\n")) + + return return_list + + +def calc_word_value(word): + """Calculate the value of the word entered into function + using imported constant mapping LETTER_SCORES""" + scores = 0 + for char in word: + try: + scores += LETTER_SCORES[char.upper()] + except KeyError: + continue + + return scores + + +def max_word_value(list_of_words=load_words()): + """Calculate the word with the max value, can receive a list + of words as arg, if none provided uses default DICTIONARY""" + value_dict = {} + + for word in list_of_words: + value_dict[word] = calc_word_value(word) + max_word = sorted(value_dict, key=value_dict.get, reverse=True) + return "".join(max_word[:1])