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.
* PCC01 Tom-a-Hawk * PCC01 Tom-a-Hawk * PCC01 Tom-a-Hawk
- Loading branch information
1 parent
82a7ced
commit 1d2aeb5
Showing
3 changed files
with
75 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,10 @@ | ||
DICTIONARY = "C:/Users/tommy/challenges/01/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,30 @@ | ||
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,35 @@ | ||
from data import DICTIONARY, LETTER_SCORES | ||
|
||
def load_words(): | ||
"""Load dictionary into a list and return list""" | ||
with open(DICTIONARY) as fin: | ||
x = fin.read().splitlines() | ||
return x | ||
|
||
def calc_word_value(word): | ||
"""Calculate the value of the word entered into function | ||
using imported constant mapping LETTER_SCORES""" | ||
x = sum([LETTER_SCORES.get(char.upper(), 0) for char in word]) | ||
return x | ||
|
||
def max_word_value(text=None): | ||
"""Calculate the word with the max value, can receive a list | ||
of words as arg, if none provided uses default DICTIONARY""" | ||
if text is None: | ||
x = load_words() | ||
wordScores = [sum([LETTER_SCORES.get(char.upper(), 0) for char in i]) for i in x] | ||
d = dict(zip(x, wordScores)) | ||
maximum = max(d, key=d.get) | ||
return maximum | ||
else: | ||
|
||
wordScores = [sum([LETTER_SCORES.get(char.upper(), 0) for char in i]) for i in text] | ||
d = dict(zip(text, wordScores)) | ||
maximum = max(d, key=d.get) | ||
return maximum | ||
|
||
if __name__ == "__main__": | ||
load_words() | ||
calc_word_value() | ||
max_word_value() | ||
#pass # run unittests to validate |