Lab 4: Strings and Functions

Unlike most labs, this lab is due before you leave today.

Goals

After the lab, you should be

  1. proficient at solving problems using strings
  2. more comfortable defining and using functions in your code

Linux

As usual, create a directory for the programs and output you develop in this lab.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab4.1.py through lab4.4.py.

Your programs will be graded on correctness, efficiency, style, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.

After you've developed a correct solution to each program, restart IDLE or close and reopen the IDLE "shell" by running the program again (using F5), demonstrate that the program works using several good test cases, and save the output to a file named lab3.x.out, where x is the problem number.

  1. (10 pts) Write a program that prints a string (input from a user) backwards.
  2. (15 pts) Write a program that determines if a string (input from a user) is a palindrome. A palindrome is a word that is the same fowards and backwards.
    Some example palindromes: "kayak", "A man A plan A canal Panama". Palindrome List

    In your algorithm, remove spaces from the string. You should consider upper and lower case letters as the same, .i.e., "A man A plan A canal Panama" is a palindrome. (What string method can you use to accomplish that before processing?) However, you don't need to consider punctuation as special cases.

    You may want to have a variable that keeps track of if the string is a palindrome. (What type of variable would be most suited for that task, given that a string is either a palindrome or it isn't?)

    For this problem, you should strive for efficiency, but, given our current knowledge of programming building blocks, your solution may be slightly inefficient, i.e., even though you already "know" that a string is not a palindrome, you will keep checking that if it is. You can write an efficient solution for an extra 3 points. (Talk to me about your solution if you're curious about the extra points.)

  3. (30) Write a program to use a Caesar cipher to encode and decode text messages. A Caesar cipher is a simple substitution cipher based on the idea of shifting each letter of a text message a fixed number (called the key) of positions in the alphabet. You should assume that the messages contain only lower case letters and spaces. Your program should produce the following sort of interaction with the user:
    This program encodes and decodes Ceasar ciphers.
    
    Enter some text: i like the lions at the zoo
    Enter an integer key (between -25 and + 25): 1
    The result is: j mjlf uif mjpot bu uif app 
    

    Notice that the alphabet wraps around. The 'z' in 'zoo' became an 'a' in the encoded message. You should be able to use the same program to decode this message as follows:

    This program encodes and decodes Ceasar ciphers.
    
    Enter some text: j mjlf uif mjpot bu uif app 
    Enter an integer key (between -25 and + 25): -1
    The result is: i like the lions at the zoo 
    

    Note that spaces will have to be handled specially.

    Rhetorical question: why is the limit on keys between -25 and +25?

  4. (15) Copy the palindrome program and modify it so that the palindrome functionality is in a function. The function you create should take as input a string and output whether the string is a palindrome. (What is the keyword for "output from a function?") Can you make this implementation more efficient than the previous program's implementation?

    Your script should also call the function you defined so that you can test your function.

Finishing up: What to turn in for this lab

  1. IDLE and jEdit may create backup files with the "~" extension. Delete these files from your lab directory to save paper when you print.
  2. Copy your lab4 directory into the turnin directory. (Review the UNIX handout if you don't remember how to do that.)
  3. Turn in your printed lab assignment, using the printLab.sh command.

    Again, you should probably print from the labs directory.

    Print the file using the lpr command introduced in the first lab.

Unlike most labs, this lab is due before you leave today.

Grading (70 pts)