Skip to content

Commit

Permalink
start challenge 02
Browse files Browse the repository at this point in the history
  • Loading branch information
pybites committed Jan 13, 2017
1 parent 2030b78 commit 4ad51ed
Show file tree
Hide file tree
Showing 3 changed files with 235,969 additions and 0 deletions.
56 changes: 56 additions & 0 deletions 02/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
## Code Challenge 02 - Word Values Part II - a simple game

> 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

Building upon the [last challenge](http://pybit.es/codechallenge01.html) this week part II: a simple game.

### Requirements / steps

* this will be a console game. Import SCRABBLE_POUCH from data.py to get the complete pool of letters

* 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).

* 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)

* now the checker 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.

* 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.

### Bonus

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).

* convert this in a pygame / web app. / Android app

### Possible console output

### Good luck!

We can't wait for your inputs!

Remember: there is no best solution, only learning and getting better Pythonistas.

And above all: have fun!!

---

* [Corresponding blog post](http://pybit.es/codechallenge02.html)

* [Fork this project and start coding](https://github.com/pybites/challenges)

### About PyBites Code Challenges

Background in [our intro post](http://pybit.es/codechallenge01.html).
27 changes: 27 additions & 0 deletions 02/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from collections import namedtuple

Letter = namedtuple('Letter', 'name amount value')

# 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(
list(letter.name * int(letter.amount)
for letter in distribution))
)
assert len(POUNCH) == 98 # no wildcards in this simple game

LETTER_VALUES = dict(zip(
[letter.name for letter in distribution],
[int(letter.value) for letter in distribution]
))

assert LETTER_VALUES['A'] == 1
assert LETTER_VALUES['Q'] == 10
assert sum(LETTER_VALUES.values()) == 87

def _load_words():
with open('dictionary.txt') as f:
return set([word.strip() for word in f.read().split()])

DICTIONARY = _load_words()
Loading

0 comments on commit 4ad51ed

Please sign in to comment.