Go to: Course home page  

Exercise Assignments

Exercise 1

The goal of this exercise is to practice Python in IDLE: aim for an hour or so. You should also try the two problem sets below. Where a command is obscured, it is your job to figure out the missing part that will produce the output shown. Where an output is blocked out, make a guess at it first, and then execute the command to see if you were correct.
  • Set 1
  • Set 2 (Restart your shell first by Ctrl+F6.)
Then, continue to review what we learned in class. Feel free to explore and try your own commands. Don't worry about mistakes and error messages -- you should be making lots of them!

When you're done, submit two things through the "Exercise #1" assignment link on CourseWeb:

  1. Answers to the two problem sets. Write it in the text box.
  2. Your IDLE shell session saved as a text file. Name it "ex1.Your-last-name.txt". Submit as a file attachment.
That's it! If you are looking to learn more, try A Beginner's Python Tutorial, Lesson 2 and Lesson 3.


Exercise 2

In this exercise, you will write your own Python script file. The file should be named "ex2.Your-last-name.py". (NOTE: Do NOT do this exercise as an IDLE shell session. You should be writing and executing a Python script file.)

Start your script by declaring the variable sent as follows:

sent = 'Roses are red, violets are blue.'
And then follow it with these two print commands:
print sent
print sent.upper()
Save your script and execute it in the IDLE shell to make sure that it works. You should have an output like this. Now, your task is to compose seven additional print commands, such that your script will produce the following nine lines of output when executed:
Roses are red, violets are blue.
ROSES ARE RED, VIOLETS ARE BLUE.
Roses-are-red,-violets-are-blue.
Rosxxs arxx rxxd, violxxts arxx bluxx.
Roses are very red, violets are very blue.
She said: Roses are red, violets are blue.
['Roses', 'are', 'red,', 'violets', 'are', 'blue.']
6
The sentence is 6 words long.
Note that you should not just print the lines above verbatim (that wouldn't make much of an exercise now, would it?). Instead, each print command of yours should produce the desired output by manipulating the string content of sent. That is, each print command must include a reference to sent, utilizing string operations we learned in class today. You shouldn't need any additional commands or statements: basically, your script should look like this.

Hint: the best approach is to make sure each print command works as intended before moving on to the next one. That is, proceed in cycle: compose a print command -> save -> execute script -> verify output -> repeat.

If you have trouble, go back and review the course slides from today. When you are done, upload your "ex2.Your-last-name.py" file onto Exercise #2 item on CourseWeb.

[5 BONUS POINTS] Compose two additional print commands for the lines below. The bottom one uses a string method we didn't learn in class: find it in the string operation table in Python 2.7 Quick Reference.

She said: ROSES-ARE-RED,-VIOLETS-ARE-BLUE.
Roses Are Red, Violets Are Blue.


Exercise 3

For this exercise, review what we covered in class today in IDLE shell. Aim for an hour or so. Go over the examples covered in class, and also try your own examples. Feel free to explore! When you feel you've learned enough, save your IDLE shell session as a text file and upload it.

Exercise 4

This exercise has two parts.

1. A Palindrome Program that Won't Quit
... Until you give it a palindrome, that is. Modify the palindrome program you wrote for HW#1 so that it loops back until the user has given a palinedrome. In other words, when a user-supplied string fails to be a palindrome, your script should automatically prompt again for a word, which continues until the typed input is finally a palindrome. The program should
work like this.

  • What you need to use is the while keyword we learned in class today. Note that you will need to adjust the indentation of many of your code blocks; be careful with them!
  • Also, make your palindrome program begin with a greeting when executed:
    Hello! Let's find a palindrome.
    and end with the following:
    Goodbye.
    Note that these two should be outside the while loop.
When you're done, upload your script to the Exercise 4 item on CourseWeb.

2. Trying Out Scripts
For each of the scripts below, do the following.

Examine it first and figure out what it does just by looking. Write down how you think the process works, and what the output will be (pre-execution guess). Then, type up the script and execute it. Examine the output and write down: if your guess was correct/incorrect, and additional explanations on where your guess went wrong and why the program does what it does (post-execution summary).

Submit a word document (PDF also works) containing the following items, per script:

  1. The script itself. You may use screenshot* OR copy-and-paste the text from the IDLE editor window.
  2. The program output. Again, you can use screenshot or copy-and-paste the text from the IDLE shell window.
  3. Your pre-execution guess
  4. Your post-execution summary

*Crop your image properly when using screenshot -- no gigantic image of your entire desktop please! To find out how cropping works in MS Word, see this image or this video tutorial.



Exercise 5

In this exercise, you will be completing a script file that processes a short text for basic stats. Download this Ex5.tale.TEMPLATE.py file. At its initial state, it produces this output when run. Your job is to flesh out the script so that it ultimately produces this complete output. Also, when completed, your two functions should work like this when called in your IDLE shell following an execution of your script.

There are a total of 8 points of edit, marked with [1] -- [8]. Start with the first one and proceed to the next. Pay attention to the function definitions and also the instructions embedded as comments. Make sure your script output matches the intended correct output.

When you're done, save your script as Ex5.tale.YOUR-LAST-NAME.py and upload it on CourseWeb.

Exercise 6

[2/27 (Thu) 1pm: There were a couple of bugs in the original template py file and the example output file. If you downloaded the script before today, please re-download it.]
[2/28 (Fri) 3pm: I added a
screenshot showing how getFreq() function should work.]
In this exercise, you will be completing a script file that processes a text file -- the famous Gettysburg Address. Download the template script file: Ex6.gettysburg.TEMPLATE.py and the Gettysburg Adress text file: gettysburg-address.txt. Your job is to flesh out the script so that it ultimately produces an output file that contains some basic statistics of the Gettysburg Address.

Your getFreq() function should work like this. I won't show you the output for the Gettysburg Address, but for your reference, here's a sample output file produced when the script is run on this file instead.

There are a total of 8 points of edit, marked with [1] -- [8]. Start with the first one and proceed to the next. Pay attention to the function definitions and also the instructions embedded as comments.

When you're done, upload two files on CourseWeb: your script saved as Ex6.gettysburg.YOUR-LAST-NAME.py, and the output file.


Exercise 7

For this exercise, you will be working with a word list file and your textproc.py, operating inside your IDLE shell (not in a script!). There will be lots of list-comprehending. Follow the steps below.

STEP 1
Edit your textproc.py to include the isPalindrome() function below. You may copy and paste, but watch the indentation.

def isPalindrome(wd) :
    "Takes a word, returns True/False for palindrome-hood"
    rwd = list(wd)        # rwd is a list of characters
    rwd.reverse()         # rwd is a reversed list of chars
    rwd = ''.join(rwd)    # rwd is joined back to a string
        # return lowercase comparison, so 'Level' is positive:
    return wd.lower() == rwd.lower()  

STEP 2
Open up your IDLE shell, and then import the textproc module. Try out the new isPalindrome function:

>>> textproc.isPalindrome('Level')
True
>>> textproc.isPalindrome('elephant')
False

STEP 3
Download this file:
words.txt. This is an open-source list of English word types. Open it in your IDLE shell and then read in the LINES in the file as a list of strings, using .readlines() method. Close up your file.

>>> f = open(r'text-processing\words.txt')
>>> wlist = f.readlines()
>>> f.close()
Now answer the following questions. With the exception of Question 2, every solution must involve use of list comprehension. In a document (.txt, .docx, or .pdf), write up your answers to each question and your Python solution (paste the portion of your IDLE shell that produced the answers -- see *Note below).
  • Question 1: Remove line breaks from words
    You will notice that the words in the list have a line break '\n' character at the end. Compose a one-liner list comprehension that creates a new list of words without line breaks, e.g.:
    >>> wlist[:10]
    ['A\n', 'a\n', 'aa\n', 'aal\n', 'aalii\n', 'aam\n', 'Aani\n', 
    'aardvark\n', 'aardwolf\n', 'Aaron\n']
    >>> wlist = [               ??                ]
    >>> wlist[:10]
    ['A', 'a', 'aa', 'aal', 'aalii', 'aam', 'Aani', 'aardvark', 
    'aardwolf', 'Aaron']
    
  • Question 2: Explore the word list
    How many words are in this list? What's the first word? What's the 1000th word? What's the last word? You probably realized that the list is not in fact alphabetically ordered. Sort the list, and then find the last word: what is it?
  • Question 3: Words with certain substrings
    What are the words that have 'vv' in them? How many words start with 'un' and end in 'able'? What's the longest 'un...able' word?
  • Question 4: With vowels and without
    How many words have all 5 English vowels? How many have no vowels?
  • Question 5: Length
    How many words are 15 letters or longer? How long is the longest word? What's the average length of all words?
  • Question 6: Palindrome
    Find all palindromes that are 5 characters or longer. How many are there?
  • Question 7: Word-initial characters
    What are the top 3 most common letters beginning a word? What are their counts?
  • Question 8: No repeat
    What is the longest word where each letter occurs only once?
[5 BONUS POINTS: Q9 & Q10]
  • Question 9: Maximum variety
    What's the largest # of different alphabetic characters present in a single word? What's the shortest word to have that many?
  • Question 10: Your own question
    Anything else you want to investigate? Pick your own question and find the answer.
When you're done, upload your write-up document to CourseWeb.

*Note: Python code in a document looks better in fixed-width fonts such as Consolas or Courier, which keep characters neatly aligned. Alternatively, you may paste in screen-shots of your IDLE shell window in a MS-Word document.



Exercise 8

For this exercise, study Python for 1+ hour and turn in evidence of your work. The evidence can be in the form of: saved IDLE shell sessions, your scripts, your notes, and others. Suggested activities:
  • Try out Python commands we covered in class
  • Compile a summary note on Python commands and functions
  • Write your own Python programs