From 4f4af5e8fd9aa16257fb187a52b1227e3ce695a2 Mon Sep 17 00:00:00 2001 From: Juha Loukaja Date: Sun, 22 Sep 2024 11:21:47 +0300 Subject: [PATCH 1/2] PCC01 loukaja --- 01/loukaja/wordvalue.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 01/loukaja/wordvalue.py diff --git a/01/loukaja/wordvalue.py b/01/loukaja/wordvalue.py new file mode 100644 index 00000000..e0cc6a1b --- /dev/null +++ b/01/loukaja/wordvalue.py @@ -0,0 +1,40 @@ +import timeit + +from data import DICTIONARY, LETTER_SCORES + +def load_words(): + """Load dictionary into a list and return list""" + with open(DICTIONARY) as f: + return (f.read().split()) + +def calc_word_value(word): + """Calculate the value of the word entered into function + using imported constant mapping LETTER_SCORES""" + value = 0 + for char in word: + if char.isalpha(): + value += LETTER_SCORES[char.upper()] + + return value + +def max_word_value(word_list=None): + """Calculate the word with the max value, can receive a list + of words as arg, if none provided uses default DICTIONARY""" + if word_list is None: + word_list = load_words() + + max_value = 0 + max_word = '' + + for word in word_list: + if len(word) * 10 < max_value: + continue # no point in calculating if the value can't reach above current max + value = calc_word_value(word) + if value > max_value: + max_word = word + max_value = value + + return max_word + +if __name__ == "__main__": + pass # run unittests to validate From 5e82f7e1d4d52f4abb357eb6309be9869ffa3a72 Mon Sep 17 00:00:00 2001 From: Juha Loukaja Date: Sun, 22 Sep 2024 11:29:38 +0300 Subject: [PATCH 2/2] Remove unneccessary import --- 01/loukaja/wordvalue.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/01/loukaja/wordvalue.py b/01/loukaja/wordvalue.py index e0cc6a1b..fa580f77 100644 --- a/01/loukaja/wordvalue.py +++ b/01/loukaja/wordvalue.py @@ -1,5 +1,3 @@ -import timeit - from data import DICTIONARY, LETTER_SCORES def load_words():