Skillshare intro to Python/Unit 5: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
imported>Jesstess
Line 56: Line 56:
==Breaking down the problem==
==Breaking down the problem==


===Step 1: construct a Python word list===
===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.
Our goal is to turn the words in the <code>sowpods.txt</code> file into a Python list.

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 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>:
<b>Step 1 resources</b>:
Line 71: Line 80:
</li>
</li>
</ul>
</ul>



===Step 2: get the rack===
===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. For example if your script were called `scrabble_cheater.py`, if you ran <tt>python scrabble_cheater.py RSTLNEI</tt>, <tt>RSTLNEI</tt> would be the rack.
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.
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>:
<b>Step 2 resources</b>:
<ul>
<ul>
<li>
Command line argument parsing: http://docs.python.org/library/argparse.html#module-argparse.
</li>
<li>
<li>
Getting and checking the number of command line arguments: http://docs.python.org/library/sys.html.
Getting and checking the number of command line arguments: http://docs.python.org/library/sys.html.
</li>
</li>
<li>Converting letters to lower case: http://docs.python.org/library/stdtypes.html#str.lower</li>
</ul>
</ul>



Revision as of 12:57, 13 June 2013

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 project

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

$ 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

Resources

  • sowpods.txt is a text file that contains all words in the official SOWPODS Scrabble word list, one word per line.
  • Here is a Python dictionary containing all letters and their Scrabble values:
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}

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


Step 2: construct a Python word list

We need to turn the words in the sowpods.txt file into a Python list.

To do this, write the code to open and read the contents of sowpods.txt 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 sowpods.txt. 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 len function to print the length of your Python word list. It should contain 267751 words.

Step 1 resources:


Step 2: get the rack

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 print function to print the Scrabble rack you've retrieved from the command line.

Step 2 resources:


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.

Step 3 resources:


Step 4: scoring

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

Step 4 resources:

Checking your work

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

$ python scrabble.py 
Usage: scrabble.py [RACK]
$ python scrabble.py AAAaaaa
2 aa
$ 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


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. This is a huge accomplishment!