Scrabble challenge: Difference between revisions

From OpenHatch wiki
Content added Content deleted
(zgtSHjvhWqzHIsPac)
Line 1: Line 1:
How neat! Is it really this smiple? You make it look easy.
There's a huge leap between having learned the fundamentals of a programming language and being able to sit down at a blank text editor and implement an idea from scratch.

Getting over the "I sit down at a text editor with an idea -- now what?" hump takes practice! Here's a fun practice problem that provides some guidance by breaking down the implementation steps and suggesting resources.

=== Problem Statement ===

Write a Python script that takes a Scrabble rack as a command-line argument and prints all valid Scrabble words that can be constructed from that rack, along with their Scrabble scores, sorted by score. An example invocation and output:

<pre>
$ python scrabble.py ZAEFIEE
17 feeze
17 feaze
16 faze
15 fiz
15 fez
12 zee
12 zea
11 za
6 fie
6 fee
6 fae
5 if
5 fe
5 fa
5 ef
2 ee
2 ea
2 ai
2 ae
</pre>

===Resources===

* http://www.isc.ro/lists/sowpods.zip contains all words in the official [http://en.wikipedia.org/wiki/SOWPODS SOWPODS] word list, one word per line.
* Here is a dictionary containing all letters and their Scrabble values:

<pre>
scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
</pre>

===Breaking down the problem===

====Step 1: construct a word list====

Write the code to open and read the sowpods word file. Create a list, where each element is a word in the sowpods word file. Note that each line in the file ends in a newline, which you'll need to strip.

<b>Step 1 resources</b>:
<ul>
<li>
File input and output: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files.
</li>
<li>
Stripping characters (like whitespace and newlines) from a string: http://docs.python.org/library/stdtypes.html#str.strip.
</li>
</ul>


====Step 2: get the rack====

Write the code to get the Scrabble rack from a command line argument. Handle the case where a user forgets to supply a rack. Make sure you are consistent about capitalization: if your scores dictionary is lowercase, the letters supplied by the user need to be converted to lowercase at some point before you compare them.

<b>Step 2 resources</b>:
<ul>
<li>
Command line argument parsing: http://docs.python.org/library/argparse.html#module-argparse.
</li>
<li>
Getting and checking the number of command line arguments: http://docs.python.org/library/sys.html.
</li>
</ul>


====Step 3: find valid words====

Write the code to find all words from the word list that are made of letters that are a subset of the rack letters. There are many ways to do this, but here's one way that is easy to reason about and is fast enough for our purposes: go through every word in the word list, and for every letter in that word, see if that letter is contained in the rack. If it is, save the word in a valid_words list. Make sure you handle repeat letters: once a letter from the rack has been used, it can't be used again.

<b>Step 3 resources</b>:
<ul>
<li>
List manipulation: http://docs.python.org/tutorial/datastructures.html#more-on-lists.
</li>
</ul>


====Step 4: scoring====

Write the code to determine the Scrabble scores for each valid word, using the scores dictionary from above.

<b>Step 4 resources</b>:
<ul>
<li>
Dictionary manipulation: http://docs.python.org/tutorial/datastructures.html#dictionaries.
</li>
</ul>


I could watch Schildner's List and still be happy after reading this.

===Checking your work===

What happens when you run your script on the following inputs?

<pre>
$ python scrabble.py
Usage: scrabble.py [RACK]
</pre>

<pre>
$ python scrabble.py AAAaaaa
2 aa
</pre>

<pre>
$ python scrabble.py ZZAAEEI
22 zeze
21 ziz
12 zee
12 zea
11 za
3 aia
2 ee
2 ea
2 ai
2 ae
2 aa
</pre>

===Bonus challenge===

Modify your script to handle blank tiles. Blank tiles have a score of 0 but can be used to represent any letter.

===Congratulations!===

You've implemented a substantial, useful script in Python from scratch. Keep practicing!

Revision as of 16:00, 15 January 2012

How neat! Is it really this smiple? You make it look easy.