Lab 6: Advanced String Problems, ASCII Representations, and Functions

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
  3. reorganizing code with functions

Linux

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

Copy fines.py from /home/courses/cs111/handouts/lab6/ into your lab6 directory.

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file.

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 lab6.x.out, where x is the problem number.

  1. (10 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 extension of the file's name. For example, we name our Python scripts with the .py extension and our HTML files with the .html extension. Using help(str), look at how to use the rfind method and compare with how the find method is used. Use the rfind method to determine a file's type. Example output:
    What is the name of your file? index.html
    That is a html file.
    
    What is the name of your file? picture.jpg
    That is a jpg file.
    
    What is the name of your file? 16.functions.pdf
    That is a pdf file.
    
  2. (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
    
  3. (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. 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 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.
    

  4. (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. (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
    
  5. (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 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: eqoazp tmxr nqsuze zai

    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 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?

  6. (15) Several weeks ago, we wrote fines.py, which calculated a fine for speeding. Rename the program as lab6.6.py. Then, define a function that takes as parameters the speed limit and the clocked speed and returns the computed fine. (What should the calculated fine be if the person wasn't speeding?) The code that calls the function will print an appropriate message based on the returned fine. Write an appropriate comment for the function you defined.

    Then, put the driver part of the program (i.e., the part that gets input from the user, calls the function, and displays the output) into a main 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 lab6 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.

    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)