HW 3

Functions and Lists

Introduction to Programming -- CS 140
Spring, 2008

This assignment is due on Friday, March 7

 

This assignment will give you more practice designing code with functions.

Part one.  Debugging

Here is more practice for your debugging skills. These each contain a common bug for programming with functions. See if you can make each program work correctly.

 

Part two:

Here is a nice dictionary of English words. This is part of the Mozilla Thunderbird implementation; don't try to sell it to anyone. You should download this and put it in the same folder as the program you will write for this lab. If the dictinary isn't in the same folder as the program, your program won't work. The name of the file you save should be "dict.txt"

First of all, here is a small program that prints the first 20 words of the dictionary:

   def main():
        F = open("dict.txt", "r")
        counter = 0
        for w in F:
             w = w.strip()
             print w
             counter = counter + 1
             if counter > 20:
                   break

   main()

 

You can download this program as hw3.py. We did something like this in class on February 25. The line
        F=open("dict", "r")
says to open a file named "dict" in a mode that lets you read words out of the file. The line
        for w in F:
is just a for-loop that runs through all of the words in the file. The line
        w = w.strip()
deletes the carriage return that this file places at the end of each word. The stuff with the counter is just there to stop printing after 20 words. This is a very large file and will take a while to print if you let it run without the counter.

Make sure that this much works for you before you do anything else. If you put the dict.txt file and the hw3.py program in the same folder and run hw3 you shouls see 20 words starting with the letter 'a' as output.

Your assignment is to take this file and make some changes to it. You need to add 2 functions:

A) Function reverse(s) takes a string s and reverses it, so "Oberlin" becomes "nilrebO". There is no buiilt-in function for this so you need to write it yourself. I suggest using a WHILE loop for this. Have a variable "answer" that starts as an empty string, and an index i that starts at the last index of string s and works its way down to 0; at each step take the letter at index i and add (concatenate) it onto answer. Remember that for a word with 4 letters, such as "Leap", the indexes are 3, 2, 1, and 0. So you want i to start at len(s)-1 and work down to 0.

B) Function isPalindrome(s) returns True if s is a palindrome and False if s isn't. This should be easy:s is a palindrome if s == reverse(s)

If you want to test these out, you might put these functions in a file of their own and try reversing strings and detecting palindromes.

Once you have written these two functions change the main() function to read the whole file (remove the if counter > 20: break lines) and print every word in the file that is a palindrome. You should find well over 100 palindromes.

Finally, I would like you to add something to the end of the program that prints out a statement saying how many words were in the dictionary and how many of them were palindromes and what the longest palindrome was. You will have to add some new variables to the main() function for this.

If you have this finished and want more, here is a little challenge for you: the dictionary contains the name Anna, but it doesn't get recognized as a palindrome because Python considers 'A' and 'a' to be different letters. Fix this. You can do so very easily if you use the lower() method for strings. If s is a string, then s.lower() is a new string that contains the same letters as s but all in lowercase. So of s is "Anna" then s.lower() is "anna". You need to figure out which strings to lower to determine that "Anna" is a palindrome.

This is due at midnight at the end of Friday, March 7.