|
CPSC 115L: Introduction to Computing
|
Fall 2010
|
Laboratory 7: Iteration on strings
October 20, 21
As usual, you are expected to work with an assigned partner as a pair. Both
you and your partner will receive the same grade. Both of you should always
save your laboratory work on your own accounts.
Objectives
The main objectives of this laboratory are
- to learn how to design and implement loop constructs, and
- to learn how to work with strings.
1. Warm-up exercise
Alyssa is preparing for an annual spelling bee, and she would like to write a
Python script to extract and print each letter of a given word. Here is her
I/O specification:
Input: a word
Output: the letters of the given word, each in a separate line
That is, she would like to write a script that behaves as follows:
Enter a word: trinity
The letters of the word "trinity" are:
t
r
i
n
i
t
y
How can a Python script traverse a word to extract its letters one by one?
Alyssa thinks using a while statement might work and comes up with the
following algorithm:
- Input a word in a string s.
- Compute s's length by invoking the function len. Call
this l.
- Initialize a counter i with 0.
- While i < l do the following:
Print the ith letter of s.
Increment the value of i by 1.
Alyssa then translates her algorithm into a Python script as follows:
s = raw_input('Enter a word: ')
l = len(s)
i = 0
print 'The letters of the word "' + s + '" are:'
while i < l:
print s[i]
i = i + 1
Note that Alyssa uses the function raw_input to read a word. This is
Python's build-in function that reads and returns an input string. Also, note
the function len. This function returns the length of a given
string.
1.1.
Modify Alyssa's solution so that it prints the letters of a given word in the
reverse order. That is, your script should behave as follows:
Enter a word: trinity
The letters of the word "trinity" in the reverse order are:
y
t
i
n
i
r
t
Implement your solution in a Python script named backward.py. Run
your script with five test cases and save the snapshots of your test runs in a
text file named backward.out.
1.2.
Now, combine the two solutions together to list the letters of a given word
in both the given and reverse orders. In particular, your script should
behave as follows:
Enter a word: trinity
The letters of the word "trinity" are:
t y
r t
i i
n n
i i
t r
y t
Implement your solution in a Python script named forbackward.py. Run
your script with five test cases and save the snapshots of your test runs in a
text file named forbackward.out. When completed, show your work to
the instructor or TA.
1.3
Write a recursive function that returns the reverse of a given string. So,
for example, reverse('trinity') would return 'ytinirt'.
2. Recognizing palindromes
A palindrome is a word, phrase or sentence that is spelled the same
forward and backward. Here are some well-known examples of such words:
civic, dad, kayak, level, mom, radar
and rotator. When we consider a phrase or sentence, we usually ignore
space, punctuation and (upper and lower) letter case. Here are some
well-known examples: Go hang a salami I'm a lasagna hog. Madam, I'm
Adam.
2.1.
Design and implement a Python script that determines whether or not a given
word is a palindrome. You may assume that words are given in lower-case
letters. Your script should behave as follows:
Enter a word in lower-case letters: reviver
The word "reviver" is a palindrome.
Enter a word in lower-case letters: trinity
The word "trinity" is not a palindrome.
For this, first write down on a piece of paper your I/O specification and
algorithm. Then implement your algorithm in a Python script named
palindrome1.py. Run your script with five test cases and save the
snapshots of your test runs in a text file named palindrome1.out.
2.2.
Now use your recursive reverse() function to write a script that
tests for palindromes. It should follow the same I/O specs as the previous
exercise.
2.3.
Next, extend your solution to 2.1 so that it can also recognize phrases and
sentences. For this, your script must somehow ignore space, punctuation and
(upper and lower) letter case when matching letters. Your script should
behave as follows:
Enter a word, phrase or sentence: race car
"race car" is a palindrome.
Enter a word, phrase or sentence: Madam, I'm Adam.
"Madam, I'm Adam." is a palindrome.
Enter a word, phrase or sentence: Sir, I'm Eve.
"Sir, I'm Eve." is not a palindrome.
To ignore letter case, you should first covert all upper-case letters in a
given string to the corresponding lower-case letters by invoking Python's
built-in string method lower. This method may be used in the
following way:
>>> s = "Trinity"
>>> t = s.lower()
>>> print t
trinity
Redesign your algorithm and implement it in a Python script named
palindrome2.py. Run your script with five test cases and save the
snapshots of your test runs in a text file named palindrome2.out.
When completed, show your work to the instructor or TA.
What to hand in
Upon completion of your laboratory, submit the following in paper. Be sure to
put a file header at the top of each file.
- Your I/O specifications and algorithms for 2.1 and 2.2
- Printouts of the Python scripts backward.py,
forbackward.py, palindrome1.py and
palindrome2.py.
- Printouts of the output files backward.py,
forbackward.out, palindrome1.out and
palindrome2.out.