diff --git a/02/README.md b/02/README.md index a77c58021..af07eb354 100644 --- a/02/README.md +++ b/02/README.md @@ -2,9 +2,9 @@ > This week, each one of you has a homework assignment ... - Tyler Durden (Fight club) -### Given a random set of 7 letters build the most valuable word +### Given a random set of 7 letters build the most valuable word -Building upon the [last challenge](http://pybit.es/codechallenge01.html) this week part II: a simple game. +Using what we've learned [the last challenge](http://pybit.es/codechallenge01.html) this week we build a simple game. ### Requirements / steps @@ -12,31 +12,65 @@ Building upon the [last challenge](http://pybit.es/codechallenge01.html) this we * draw 7 random letters from SCRABBLE_POUCH (see [Scrabble rules](http://www.scrabblepages.com/scrabble/rules/), this will be a lite version, no board and no bonus points, keeping it simple). +['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'D', 'D', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'F', 'F', 'G', 'G', 'G', 'H', 'H', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'I', 'J', 'K', 'L', 'L', 'L', 'L', 'M', 'M', 'N', 'N', 'N', 'N', 'N', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'P', 'P', 'Q', 'R', 'R', 'R', 'R', 'R', 'R', 'S', 'S', 'S', 'S', 'T', 'T', 'T', 'T', 'T', 'T', 'U', 'U', 'U', 'U', 'V', 'V', 'W', 'W', 'X', 'Y', 'Y', 'Z'] + * ask the user to form a word with the 7 letters. - * check that only letters of the draw are used. - * check if a valid word, for that import the DICTIONARY set from data.py (I provided a set for O(1) lookup) + * check that only letters of the draw are used. + * check if a valid word, for that import the DICTIONARY set from data.py (I provided a set for O(1) lookup) + +Stop basic +Print score -* now the checker side: get all permutations for the 7 letter draw (hint: [itertools.permutations](http://pybit.es/itertools-examples.html)) +## Advanced: give a score based on optimal (computer calculated) solution + +* now the computer side: get all permutations for the 7 letter draw (hint: [itertools.permutations](http://pybit.es/itertools-examples.html)) * cross check the collection built in last step with the dictionary to filter out valid words only. * calculate the word values of each of the remaining words, you can use code from [last challenge](http://pybit.es/codechallenge01.html) here. Import LETTER_VALUES from data.py to use in your calculations. + + +# re-use from challenge 01 +def calc_word_value(word): + """Calc a given word value based on Scrabble LETTER_SCORES mapping""" + return sum(LETTER_SCORES.get(char.upper(), 0) for char in word) + + +# re-use from challenge 01 +def max_word_value(words): + """Calc the max value of a collection of words""" + return max(words, key=lambda w: calc_word_value(w)) + + + + * present the user with the highest vs the one he/she created and calculate a simple score (user word value / max word value). - * optional variation: give the user n attempts (defined as a constant NUM_ATTEMPTS) and feedback each time how far he/she is off from the max score. + * optional variation: give the user n attempts (defined as a constant NUM_ATTEMPTS) and feedback each time how far he/she is off from the max score. + -### Bonus + + +### Bonus (not required) This might be quite a challenge, but it is also a highly addictive one I think. If you want to get creative you can further build this out: * keep scores in a shelve (or sqlite3 db) and feedback each game if user reached a new max score. -* work with hints (penalizing end score). +* work with hints: show some chars of the optimal word (e.g. g____y) - substract some points for using the hint -* convert this in a pygame / web app. / Android app +* give 20 extra points if all letters are used + +* convert this in a pygame / web app. / Android app ### Possible console output +Letters drawn: G, A, R, Y, T, E, V +Form a valid word: gary +Word chosen: GARY with value: 8 +Max word: garvey with value: 13 +You scored: 61.53846153846154 + ### Good luck! We can't wait for your inputs! @@ -51,6 +85,6 @@ And above all: have fun!! * [Fork this project and start coding](https://github.com/pybites/challenges) -### About PyBites Code Challenges +### About PyBites Code Challenges Background in [our intro post](http://pybit.es/codechallenge01.html). diff --git a/02/data.py b/02/data.py index bae0c596a..b711163bb 100644 --- a/02/data.py +++ b/02/data.py @@ -13,11 +13,11 @@ def _load_words(): # generated with https://github.com/pybites/blog_code/blob/master/BeautifulSoup/scrabble_distribution.py distribution = [Letter(name='A', amount='9', value='1'), Letter(name='B', amount='2', value='3'), Letter(name='C', amount='2', value='3'), Letter(name='D', amount='4', value='2'), Letter(name='E', amount='12', value='1'), Letter(name='F', amount='2', value='4'), Letter(name='G', amount='3', value='2'), Letter(name='H', amount='2', value='4'), Letter(name='I', amount='9', value='1'), Letter(name='J', amount='1', value='8'), Letter(name='K', amount='1', value='5'), Letter(name='L', amount='4', value='1'), Letter(name='M', amount='2', value='3'), Letter(name='N', amount='6', value='1'), Letter(name='O', amount='8', value='1'), Letter(name='P', amount='2', value='3'), Letter(name='Q', amount='1', value='10'), Letter(name='R', amount='6', value='1'), Letter(name='S', amount='4', value='1'), Letter(name='T', amount='6', value='1'), Letter(name='U', amount='4', value='1'), Letter(name='V', amount='2', value='4'), Letter(name='W', amount='2', value='4'), Letter(name='X', amount='1', value='8'), Letter(name='Y', amount='2', value='4'), Letter(name='Z', amount='1', value='10')] -POUNCH = list(''.join( +POUCH = list(''.join( list(letter.name * int(letter.amount) for letter in distribution)) ) -assert len(POUNCH) == 98 # no wildcards in this simple game +assert len(POUCH) == 98 # no wildcards in this simple game LETTER_SCORES = dict(zip( diff --git a/02/game-help.py b/02/game-help.py index 6d2457357..5e295f1fd 100644 --- a/02/game-help.py +++ b/02/game-help.py @@ -5,7 +5,7 @@ import itertools import random -from data import DICTIONARY, LETTER_SCORES, POUNCH +from data import DICTIONARY, LETTER_SCORES, POUCH NUM_LETTERS = 7 diff --git a/02/game-nohelp.py b/02/game-nohelp.py index 6b357be16..d7da5efc2 100644 --- a/02/game-nohelp.py +++ b/02/game-nohelp.py @@ -2,7 +2,7 @@ # Code Challenge 02 - Word Values Part II - a simple game # http://pybit.es/codechallenge02.html -from data import DICTIONARY, LETTER_VALUES, POUNCH +from data import DICTIONARY, LETTER_VALUES, POUCH NUM_LETTERS = 7