Anonymous user
Skillshare intro to Python/Unit 5: Difference between revisions
→Checking your work
imported>Jesstess No edit summary |
imported>Jesstess |
||
(24 intermediate revisions by the same user not shown) | |||
Line 20:
<pre>
$ python scrabble.py ZAEFIEE
2 AE
2 AI
2 EA
2 EE
5 EF
5 FA
5 FE
5 IF
6
6
6
11 ZA
12 ZEA
12 ZEE
15 FEZ
15 FIZ
16 FAZE
17 FEAZE
17 FEEZE
</pre>
==Resources==
* [http://
* Here is a Python dictionary containing all letters and their Scrabble values:
<pre>
scores = {"
"
"
"
"
</pre>
==Breaking down the problem==
===Step
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.
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 73 ⟶ 84:
===Step 2: get the rack===
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.
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>
Getting and checking the number of command line arguments: http://docs.python.org/library/sys.html.
</li>
</ul>
Line 91 ⟶ 100:
===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.
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>
</li>
<li>
Line 104 ⟶ 119:
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>
</li>
</ul>
===Step 5: sorting===
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==
Line 121 ⟶ 151:
<pre>
$ python scrabble.py AAA
2 AA
</pre>
<pre>
$ python scrabble.py ZZAAEEI
2 AA
2 AE
2 AI
2 EA
2 EE
3
11 ZE
12 ZEA
12 ZEE
22 ZEZE
</pre>
==Bonus challenge==
|