Skillshare intro to Python/Unit 5: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
No edit summary
imported>Jesstess
No edit summary
Line 1: Line 1:
== Goal #1: reading from files ==
[[File:Scrabble.jpg|right|300px]]


To get some more practice with reading from files, we're going to read, run, and extend some Python scripts.
== Project ==


For each of the following Python scripts, please do the following:
It is time for our pièce de résistance! We are going to write a Scrabble cheater from scratch in Python.


<ol>
== Goals for this project ==
<li>Download the script and save it to your Desktop. Be sure to save it as a <code>.py</code> file.</li>
<li>Open the script in your text editor.</li>
<li>Read through the script. Answer these questions:


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

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.

Here are 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://mit.edu/jesstess/www/SkillsharePython/Unit5/sowpods.txt 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 = {"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: create a new 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.

Open your text editor and create a new Python file. When you save it, give it the extension <code>.py</code>.


===Step 2: 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>:
<ul>
<ul>
<li>Overall, what does this script do?</li>
<li>
<li>What variables and data types are used? Where are the strings, integers, and floats?</li>
File input and output: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files.
<li>What functions from the <code>random</code> module are used, and why?</li>
</li>
<li>
Stripping characters (like whitespace and newlines) from a string: http://docs.python.org/library/stdtypes.html#str.strip.
</li>
</ul>
</ul>
</li>
<li>Once you have a good sense of what the script does, open a terminal, navigate to the directory where you saved the script, and run it. Does it do what you expected?</li>
</ol>


<b>Here are you scripts to read and run</b>. Have fun with them!


* [http://web.mit.edu/jesstess/www/SkillsharePython/Unit5/countries.txt countries.txt]
===Step 2: get the rack===
* [http://web.mit.edu/jesstess/www/SkillsharePython/Unit5/countries.py countries.py]

The Scrabble rack (the letters available to make words) is provided to the script as a command line argument.

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>


===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>
Using lists: 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>


===Step 4: scoring===

Once we have a list of valid words, we need to get the Scrabble scores for each word.

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 manipulation: http://docs.python.org/tutorial/datastructures.html#dictionaries.
</li>
</ul>


==Checking your work==

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

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


===Step 5: sorting===

Now that we have the point values for each valid word, we need to sort them so we can print the highest-value words first.



* [http://web.mit.edu/jesstess/www/SkillsharePython/Unit4/password_generator password_generator.py]
==Bonus challenge==


For <code>die.py</code>, how would you change the program to simulate rolling 2 dices?
Modify your script to handle blank tiles. Blank tiles have a score of 0 but can be used to represent any letter.


For <code>password_generator.py</code>, how would you generate a password that was, instead of a fixed 10 characters long, a random length between 10 and 14 characters?


==Congratulations!==
==Success!==


You have master lists and loops, two of the most important concepts in this course. Keep practicing!
You've implemented a substantial, useful script in Python from scratch that is perfect for cheating at Scrabble or Words with Friends. This is a huge accomplishment!


[[File:Fireworks.png|150px]]
[[File:Champagne.png|100px]][[File:Party.png|125px]]
[[File:Balloons.png|150px]]

Revision as of 15:16, 13 June 2013

Goal #1: reading from files

To get some more practice with reading from files, we're going to read, run, and extend some Python scripts.

For each of the following Python scripts, please do the following:

  1. Download the script and save it to your Desktop. Be sure to save it as a .py file.
  2. Open the script in your text editor.
  3. Read through the script. Answer these questions:
    • Overall, what does this script do?
    • What variables and data types are used? Where are the strings, integers, and floats?
    • What functions from the random module are used, and why?
  4. Once you have a good sense of what the script does, open a terminal, navigate to the directory where you saved the script, and run it. Does it do what you expected?

Here are you scripts to read and run. Have fun with them!

For die.py, how would you change the program to simulate rolling 2 dices?

For password_generator.py, how would you generate a password that was, instead of a fixed 10 characters long, a random length between 10 and 14 characters?

Success!

You have master lists and loops, two of the most important concepts in this course. Keep practicing!