From acfa185f135368c727265365d6be0b774234ae89 Mon Sep 17 00:00:00 2001 From: ryanh153 <35610632+ryanh153@users.noreply.github.com> Date: Tue, 24 Sep 2019 01:02:30 -0600 Subject: [PATCH] PCC01 ryanh153 (#571) * PCC01 ryanh153 * Delete dictionary.txt This file is not part of your solution and rather big. You don't have to commit files of the original challenge :) * Delete wordvalue-template.py Again, you don't have to add template files to your solution, just your solution :) * Delete data.py This file is part of the challenge and not your code so I deleted it from the PR * Delete test_wordvalue.py Tests are part of the challenge and not your contribution so I deleted them from the pR :) --- 01/ryanh153/README.md | 5 +++++ 01/ryanh153/wordvalue.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 01/ryanh153/README.md create mode 100644 01/ryanh153/wordvalue.py diff --git a/01/ryanh153/README.md b/01/ryanh153/README.md new file mode 100644 index 000000000..f1676d1ff --- /dev/null +++ b/01/ryanh153/README.md @@ -0,0 +1,5 @@ +## Code Challenge 01 - Word Values Part I + +Instructions [here](http://pybit.es/codechallenge01.html). + +Previous challenges and About [here](http://pybit.es/pages/challenges.html). diff --git a/01/ryanh153/wordvalue.py b/01/ryanh153/wordvalue.py new file mode 100644 index 000000000..2733ba49d --- /dev/null +++ b/01/ryanh153/wordvalue.py @@ -0,0 +1,31 @@ +from data import DICTIONARY, LETTER_SCORES + +"""Load dictionary into a list and return list""" +def load_words(): + + with open(DICTIONARY,'r') as dicFile: + dictionary = dicFile.readlines() + + dictionary = [word.strip() for word in dictionary] + + return dictionary + + +"""Calculate the value of the word entered into function +using imported constant mapping LETTER_SCORES""" +def calc_word_value(word): + return sum([LETTER_SCORES[char] for char in word.upper() if char in LETTER_SCORES.keys()]) + + +"""Calculate the word with the max value, can receive a list +of words as arg, if none provided uses default DICTIONARY""" +def max_word_value(words=None): + if words == None: + words = load_words() + + scores = [calc_word_value(word) for word in words] + + return words[scores.index(max(scores))] + +if __name__ == "__main__": + pass # run unittests to validate