Skip to main content.

Goals

After the lab, you should be proficient at solving advanced string problems using methods, loops, format operator, escape sequences, methods, and the substring operator

Objective: Review

Review the slides for today.

Objective: Set Up

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

Run runHelpClient &

Objective: Programming in Python

We'll practice writing several Python programs, each in their own text file. Name the files lab5.1.py, lab5.2.py, etc.

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) Translate a string into pirate speak. Write a program that takes a string as input, converts that string to pirate speak, and displays it.

    To convert a string to pirate speak, replace every 'r' in the string with 'rrr'. For example:

    Enter a string to convert to pirate speak: hello there, friends.
    hello therrre, frrriends. 
    
    Enter a string to convert to pirate speak: generals
    generrrals
    

    If the string doesn't have the letter 'r' in it, add an ', arrrr' to the end of the string to make it more pirate like.

    Enter a string to convert to pirate speak: washington and lee
    washington and lee, arrrr
    

    (Did you know that programming had such practical uses?)

  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. Handle error cases appropriately.

    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) Using a for loop, draw a diagonal line that looks like:
    \
     \
      \
       \
        \
    

    Think about the pattern of what is getting printed.

    After you have that working, have the user enter the size of the diagonal line and draw a line of the appropriate size.

  4. (12 pts) Create a program that converts a given Fahrenheit temperature to Celsius and always displays the result to 2 decimal places. The conversion formula is C=5/9(F-32). Output should look similar to:
    This program prints the Celsius temperature
    given a Fahrenheit temperature.
    
    Enter a Fahrenheit temperature: 70
    70 degrees F is 21.11 degrees C
    
  5. (20 pts) Write a program that creates a table of Olympic competition running distances in meters, kilometers, yards, and miles. The following distances should be used: 100 m, 200 m, 400 m, 800 m, and 1600m.

    Note that 1 m = .001 km = 1.094 yds = .0006215 mi

    Calculate and display the results, formatted in the following manner:

    
    Meters   Kilometers     Yards    Miles
    ---------------------------------------
       100        0.100     109.4    0.062
       200        -----     -----    -----
       400        -----     -----    -----
       800        -----     -----    -----
      1600        -----     -----    -----
    

    Note: Make sure your output looks exactly as above, but you will have all the converted values filled in.

    Your final solution should not have a lot of repeated code. How can you solve this problem without repeated code?

  6. (13 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. To make the next problem easier, create a variable that represents the string backwards.

    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
    
  7. (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.

    For this problem, you should strive for efficiency, but, given our current knowledge of programming building blocks, your solution may be inefficient. For example, you may require an extra string variable or 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.
    

Objective: Extra Credit (up to 5 points)

Sometimes, lotteries pay out money for getting a few matches (the correct number in the same spot). For example, if the user guessed "1234" and the winning number is "1784", then the user had two correct numbers and maybe wins a little bit of money.

The task: update our Pick 4 solution to tell the user how many numbers they got right too. If they matched 2 or 3 numbers, congratulate the user on getting some right.

Save this problem and its output with the name lab5.ec.

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 your turnin directory.
  3. Perform the following steps from your cs111 directory.
    Note that each command below links to a page with more information about using the command.

  4. Create the printable lab assignment, using the createPrintableLab command:
    createPrintableLab <labdirname>
  5. View your file using the evince command.
  6. Print the file using the lpr command.
  7. Log out of your machine when you are done.

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 class on Friday.

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

Grading (100 pts)