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