Cover page images (keyboard)

Lab 12 - Recursion &
Command-line Arguments

Sue Evans




Objectives

Objectives for today's lab:











Step 0 - Set up












Overview

Palindromes

The Task

strings.dat

What does strings.dat look like?

palindromes
String manipulation and recursion
Otto
Command-line arguments
Anna
file handling
Able was I ere I saw Elba
import sys
Madam, I'm Adam.
Is this a letter?
List of strings?
Was it a cat I saw?
A man, a plan, a cat, a canal: Panama?

Program Outline

Pseudo Code

import sys and string

Make sure correct number of command-line args were entered, exiting if not

Open the file for reading
Read the lines in the file into a list of strings
(don't forget to strip the trailing whitespace)
Close the file

For each of the strings in the list of strings
    Make a temporary string that holds only the letters of the string
    all in upper case

    Determine if the temporary string is a palindrome.
    If it is, print its original string

lab12.py contains some functions that have been written for you, and a commented main() with no code.

You will have to write main() and the function isPalindrome(), which must be a recursive function.












Step 1 - Check number of Command-line Arguments












Step 2 - Get the list of strings












Step 3 - Construct tempStr












Step 4 - Write isPalindrome()

Sample Output

linuxserver1.cs.umbc.edu[138] python lab12.py strings.dat
The palindromes in the file are:
Otto
Anna
Able was I ere I saw Elba
Madam, I'm Adam.
Was it a cat I saw?
A man, a plan, a cat, a canal: Panama?
linuxserver1.cs.umbc.edu[139]