Skillshare intro to Python/Unit 5: Difference between revisions

imported>Jesstess
(Created page with "right|300px == Project == It is time for our pièce de résistance! We are going to write a Scrabble cheater from scratch in Python. == Goals for this...")
 
imported>Jesstess
 
(27 intermediate revisions by the same user not shown)
Line 12:
* practice working with dictionaries and for loops
 
=== Problem statement ===
 
Write a Python script that takes a Scrabble rack (the Scrabble letters you have to play) 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.
Line 20:
<pre>
$ python scrabble.py ZAEFIEE
2 AE
17 feeze
2 AI
17 feaze
2 EA
16 faze
2 EE
15 fiz
5 EF
15 fez
5 FA
12 zee
5 FE
12 zea
5 IF
11 za
6 fieFAE
6 feeFEE
6 faeFIE
11 ZA
5 if
12 ZEA
5 fe
12 ZEE
5 fa
15 FEZ
5 ef
15 FIZ
2 ee
16 FAZE
2 ea
17 FEAZE
2 ai
17 FEEZE
2 ae
</pre>
 
===Resources===
 
* [http://courses.cms.caltechmit.edu/cs11jesstess/materialwww/advjavaSkillsharePython/lab1Unit5/sowpods.ziptxt sowpods.txt] is a text file that contains all words in the official [http://en.wikipedia.org/wiki/SOWPODS SOWPODS] Scrabble word list, one word per line.
* Here is a Python dictionary containing all letters and their Scrabble values:
 
<pre>
scores = {"aA": 1, "cC": 3, "bB": 3, "eE": 1, "dD": 2, "gG": 2,
"fF": 4, "iI": 1, "hH": 4, "kK": 5, "jJ": 8, "mM": 3,
"lL": 1, "oO": 1, "nN": 1, "qQ": 10, "pP": 3, "sS": 1,
"rR": 1, "uU": 1, "tT": 1, "wW": 4, "vV": 4, "yY": 4,
"xX": 8, "zZ": 10}
</pre>
 
===Breaking down the problem===
 
====Step 10: constructcreate a wordnew list=Python file for the project===
 
Since this Scrabble cheater is a bigger project, and something we'll want to be able to run over and over, we'll need to write it in a text file instead of interactively at the Python interpreter.
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 remove from the word.
 
Open your text editor and create a new Python file. When you save it, give it the extension <code>.py</code>.
 
 
===Step 1: construct a Python word list===
 
We need to turn the words in the <code>sowpods.txt</code> file into a Python list.
 
To do this, write the code to open and read the contents of <code>sowpods.txt</code> line by line. As you go through each line in the file, build up a Python list, where each element in the list is a word from <code>sowpods.txt</code>. Note that each line in the file ends in an invisible newline, which you'll need to remove from the word.
 
To check your work, use the <code>len</code> function to print the length of your Python word list. It should contain 267751 words.
 
<b>Step 1 resources</b>:
Line 71 ⟶ 82:
 
 
====Step 2: get the rack====
 
Write the code to get theThe Scrabble rack (the letters available to make words) fromis the command line argument passedprovided to yourthe script. Foras examplea ifcommand yourline script were called `scrabble_cheater.py`, if you ran <tt>python scrabble_cheater.py RSTLNEI</tt>, <tt>RSTLNEI</tt> would be the rackargument.
 
Write the code to get the Scrabble rack (the letters available to make words) from the command line argument passed to your script and save it in a variable.
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: 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.
 
To check your work, use the <code>print</code> function to print the Scrabble rack you've retrieved from the command line.
 
<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>
<li>Converting letters to lower case: http://docs.python.org/library/stdtypes.html#str.lower</li>
</ul>
 
 
====Step 3: find valid words====
 
Next, we need to find all of the valid sowpods words that can be made up of the letters in the rack.
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.
 
To do this, use a <code>for</code> loop to go through every word in the word list. 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 variable. Make sure you handle repeat letters: once a letter from the rack has been used, it can't be used again.
 
Hint: you will need to use a <code>for</code> loop inside of a <code>for</code> loop (the outer loop is for looping over the words, the inner loop is for looping over the letters in a word).
 
To check your work, use the <code>print</code> function to print <code>valid_words</code> after the <code>for</code> loop.
 
<b>Step 3 resources</b>:
<ul>
<li>
ListUsing manipulationlists: http://docs.python.org/tutorial/datastructures.html#more-on-lists.
</li>
<li>
Line 104 ⟶ 119:
 
 
====Step 4: scoring====
 
WriteOnce thewe codehave toa determinelist theof Scrabblevalid scoreswords, forwe eachneed validto word, usingget the Scrabble scores dictionaryfor fromeach aboveword.
 
To do this, use a <code>for</code> loop to go through each word in <code>valid_words</code>. For each word, use a counter to keep track of the score so far for the word. Then use another <code>for</code> loop to go through the word letter by letter; look up each letter in the <code>scores</code> dictionary and add the point value for that letter to the counter.
 
To check your work, use the <code>print</code> function inside the <code>for</code> loop to print each word in <code>valid_words</code> as well as its Scrabble value.
 
<b>Step 4 resources</b>:
<ul>
<li>
Dictionary manipulationDictionaries: http://docs.python.org/tutorial/datastructures.html#dictionaries.
</li>
</ul>
 
 
===CheckingStep your5: worksorting===
 
Now that we have the point values for each valid word, we need to sort them so it's easy to see what the highest-value words are.
 
<b>Step 5 resources</b>:
<ul>
<li>
Lists, including sorting lists: http://docs.python.org/2/tutorial/datastructures.html#more-on-lists
</li>
</ul>
 
==Checking your work==
 
What happens when you run your script on the following inputs?
 
<pre>
$ python scrabble.py AAA
2 AA
Usage: scrabble.py [RACK]
</pre>
 
<pre>
$ python scrabble.py AAAaaaa
2 aa
</pre>
 
<pre>
$ python scrabble.py ZZAAEEI
2 AA
22 zeze
2 AE
21 ziz
2 AI
12 zee
2 EA
12 zea
2 EE
11 za
3 aiaAIA
11 ZE
2 ee
12 ZEA
2 ea
12 ZEE
2 ai
22 ZEZE
2 ae
2 aa
</pre>
 
==Bonus challenge==
 
===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 that is perfect for cheating at Scrabble or Words with Friends. KeepThis practicingis a huge accomplishment!
 
[[File:Fireworks.png|150px]]
Anonymous user