From 1f8e6c4e30a179bcfdff0acb3a0cfa9db710f0f5 Mon Sep 17 00:00:00 2001 From: Marc Falzon Date: Wed, 2 Oct 2019 20:55:56 +1000 Subject: [PATCH] PCC01 Fuzzygwalchmei (#595) --- 01/fuzzygwalchmei/wordvalue.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 01/fuzzygwalchmei/wordvalue.py diff --git a/01/fuzzygwalchmei/wordvalue.py b/01/fuzzygwalchmei/wordvalue.py new file mode 100644 index 000000000..2ff8205e7 --- /dev/null +++ b/01/fuzzygwalchmei/wordvalue.py @@ -0,0 +1,20 @@ +from data import DICTIONARY, LETTER_SCORES + +def load_words(): + """Load dictionary into a list and return list""" + return [line.rstrip('\n') for line in open(DICTIONARY)] + +def calc_word_value(word): + """Calculate the value of the word entered into function + using imported constant mapping LETTER_SCORES""" + return sum(LETTER_SCORES.get(letter.upper(), 0) for letter in word) + +def max_word_value(words=None): + """Calculate the word with the max value, can receive a list + of words as arg, if none provided uses default DICTIONARY""" + if words is None: + words = load_words() + return max(words, key=calc_word_value) + +if __name__ == "__main__": + pass # run unittests to validate