Anonymous user
Scrabble challenge: Difference between revisions
→Step 2: get the rack
imported>Jesstess (Created page with '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 implementing an idea from scratch. Th…') |
|||
(31 intermediate revisions by 11 users not shown) | |||
Line 1:
[[File:Scrabble.jpg|right|300px]]
== Project ==
Write a Scrabble cheater from scratch.
== Goals ==
* practice breaking down a problem and solving it in Python from scratch
* practice command line argument parsing
* practice reading from files
* practice working with dictionaries and for loops
=== Problem Statement ===
Line 7 ⟶ 16:
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>[[Media:]]
$ python scrabble.py ZAEFIEE
17 feeze
Line 32 ⟶ 41:
===Resources===
* http://
* Here is a dictionary containing all letters and their Scrabble values:
Line 47 ⟶ 56:
====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
<b>Step 1 resources</b>:
Line 62 ⟶ 71:
====Step 2: get the rack====
Write the code to get the Scrabble rack
Handle the case where a user forgets to supply a rack; in this case, print an error message saying they need to supply some letters, and then exit the program using the <code>exit()</code> function. Make sure you are consistent about capitalization
<b>Step 2 resources</b>:
Line 72 ⟶ 83:
Getting and checking the number of command line arguments: http://docs.python.org/library/sys.html.
</li>
<li>Converting letters to lower case: http://docs.python.org/library/stdtypes.html#str.lower</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 <tt>valid_words</tt> 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>:
Line 83 ⟶ 94:
<li>
List manipulation: http://docs.python.org/tutorial/datastructures.html#more-on-lists.
</li>
<li>
<tt>for</tt> loops: http://docs.python.org/tutorial/controlflow.html#for-statements
</li>
</ul>
Line 98 ⟶ 112:
</ul>
===Checking your work===
Line 119 ⟶ 129:
<pre>
$ python scrabble.py ZZAAEEI
22
21
12
12
11
3
2
2
2
2
2
</pre>
===Bonus challenge===
===Congr!===
You've implemented a substantial, useful script in Python from scratch that is perfect for cheating at Scrabble or Words with Friends. Keep practicing!
[[File:Fireworks.png|150px]]
[[File:Balloos.png|150px]]
|