Introduction to Programming -- CS 140
Spring, 2008
Part one. Debugging exercises
Download each of the following Python programs. Find out what the program does, and if it is buggy fix it.
Part two. A programming exercise
This program asks the user for two strings: a search string and a target string. It replies with a statement about whether the target string contains the search string, and if so the starting index of it. This is a very important problem -- the entire Human Genome project is based on recent algorithms that allow one to search for strings that are millions of characters long. Since this is your first program, we will do a very simple version making use of some built-in Python string methods.
Here is a typical run of your program. I have put in boldface what the computer types; the rest is input from the user.
Enter a search string: bob
Enter a target string: Nattering Nabobs
The target string 'Nattering Nabobs' contains the search string 'bob' starting at position 12
Here are the tools you can use for this program, in addition to the programing techniques we have discussed in class. Suppose variables s1 and s2 represent strings, such as "Bobalouie" or "snowball" or "CGGATTACGA". Then
For example, if search and target are your two strings, then target.find(search) is the index of string search within string target. If this is -1 then the search string isn't found; if it is 0 or more then the search string is found and this gives you its starting position.
After you have this working, try to modify it to make it case-insensitive. You can do this by making lower-case versions of your search and target strings: lc_search = search.lower() and lc_target = target.lower(). Do the analysis with lc_search and lc_target, then print out your answer using the original strings.