forked from pybites/challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nonickid
committed
Jun 8, 2019
1 parent
4d7c0a0
commit 5637938
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
DICTIONARY = 'dictionary.txt' | ||
|
||
scrabble_scores = [(1, "E A O I N R T L S U"), (2, "D G"), (3, "B C M P"), | ||
(4, "F H V W Y"), (5, "K"), (8, "J X"), (10, "Q Z")] | ||
LETTER_SCORES = {letter: score for score, letters in scrabble_scores | ||
for letter in letters.split()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from data import DICTIONARY, LETTER_SCORES | ||
|
||
def load_words(): | ||
"""Load dictionary into a list and return list""" | ||
with open('../' + DICTIONARY) as fn: | ||
return [word.strip() for word in fn.readlines()] | ||
|
||
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(key.upper(), 0) for key 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([ (word, calc_word_value(word)) for word in words ], key=lambda x: x[1])[0] | ||
|
||
|
||
if __name__ == "__main__": | ||
pass # run unittests to validate |