Lab 5: Advanced String Problems, ASCII Representations

Goals

After the lab, you should be proficient at

  1. solving advanced string problems, using methods, loops, and the substring operator
  2. using the ASCII representations of characters with built-in functions

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 lab5.1.py through lab5.5.py.

Your programs will be graded on correctness, style, efficiency, 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 lab5.x.out, where x is the problem number.

  1. (15 pts) Write a program that takes as input 3 strings and prints out the alphabetically first word of the 3 strings. (You don't need to worry about capital vs lowercase letters.) Example output:

    Enter the first word: zebra
    Enter the second word: monkey
    Enter the third word: pig
    The alphabetically first word is monkey
    
    Enter the first word: dog
    Enter the second word: black cat
    Enter the third word: 13
    The alphabetically first word is 13
    
    Enter the first word: black
    Enter the second word: black cat
    Enter the third word: blacky
    The alphabetically first word is black
    
  2. (15 pts) Web servers and Web browsers need to know a file's type so they know how to display the file. The file's type is based on the file name's extension. For example, we name our Python scripts with the .py extension and our HTML files with the .html extension. Using help(str) from the Python interpreter, look at how to use the rfind method and compare with how the find method is used. Use the rfind method to determine any file's type based on its name. Example output:
    What is the name of your file? index.html
    index.html is a html file.
    
    What is the name of your file? picture.jpg
    picture.jpg is a jpg file.
    
    What is the name of your file? 15.strings.pdf
    15.strings.pdf is a pdf file.
    
  3. (10 pts) Write a program that prints a string (input by a user) backwards. Notice the lack of (extra) spaces in between letters in the output. Example output:

    What is your string? The Beatles
    Your string in reverse is seltaeB ehT
    
    What is your string? Help!
    Your string in reverse is !pleH
    
  4. (15 pts) Write a program that determines if a string (input by 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

    You should remove spaces from the string before processing and consider upper and lower case letters as the same, i.e., "A man A plan A canal Panama" is a palindrome. (What string methods can you use to accomplish these tasks 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 inefficient, i.e., even though you already "know" that a string is not a palindrome, you may keep checking if it is. You can write an efficient solution for an extra 5 points. (After you've finished the other programs, talk to me about your solution if you're curious about the extra points.)

    Example output:

    What is your phrase? noon
    noon is a palindrome.
    
    What is your phrase? noon.
    noon. is not a palindrome.
    
    What is your phrase? A man a plan a canal Panama
    A man a plan a canal Panama is a palindrome.
    
    What is your phrase? A man. A plan. A canal. Panama.
    A man. A plan. A canal. Panama. is not a palindrome.
    

  5. (20 pts) Write a program that randomly generates passwords. Your program will generate a password that is randomly between 6 and 8 characters long. Each character in the password is either a number, an uppercase letter, or a lowercase letter. For each character in the password, first randomly decide if the character is going to be a number, uppercase letter, or a lowercase letter. Then, randomly generate a number or letter, as appropriate. You'll need to use the ASCII values and the chr function. (Rhetorical: How could this program be used for evil instead of good? How does software prevent others from using this type of program for evil?) Use constants, as appropriate. Example output:
    The randomly generated password is eNu7LFaW
    
    The randomly generated password is uEW7j9s
    
    The randomly generated password is 7KslNy
    
  6. (25) 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 lowercase letters and spaces.

    Give your neighbor a message to decode, along with the original key used to encode the message. Put your neighbor's name, the original message, and the decoded message in comments. If you did not finish when others were around, decode this message, which I generated using the key 12: yaowoazhqzfuaz otaeq dayzqk

    Your program should produce the following 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 encoded text 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 encoded text 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?

Extra Credit (up to 10 pts)

Write a program that implements a Vigenere cipher. Some example output:

This program encodes Vignere ciphers.

Enter some text: the eagle flies at midnight
Enter a keyword: key

Encoded text: dlc iyqpc jjsiq er qgnrgqlr

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 lab5 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. You should probably print from the labs directory.

    Before you print, view the file to make sure it's not too long or has weird characters in it from the .pyc file. One command to view the file is gv lab5.ps

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

Labs are due at the beginning of Friday's class. You should hand in the printed copy at the beginning of class, and the electronic version should be in the turnin directory before 1:20 p.m. on Friday.

Ask well before the deadline if you need help turning in your assignment!

Grading (100 pts)