Homework #4

Due Date: May 4, 2009 Points: 100

Written Exercises

  1. (2 points each) Given the initial statements:
    import string
    s1 = "spam"
    s2 = "ni!"
    Show the results of evaluating each of the following string expressions:
    1. "The Knights who say, " + s2
    2. 3 * s1 + 2 * s2
    3. s1[1]
    4. s1[1:3]
    5. s1[2] + s2[:2]
    6. s1 + s2[-1]
    7. string.upper(s1)
    8. string.ljust(string.upper(s2),4) * 3
    [text, §4.8, Discussion problem 1]
  2. (2 points each) Show the string that would result from each of the following string formatting operations. If the operation is not legal, explain why.
    1. "Looks like %s and %s for breakfast" % ("spam", "eggs")
    2. "There is %d %s %d %s" % (1, "spam", 4, "you")
    3. "Hello %s" % ("Susan", "Computewell")
    4. "%0.2f %0.2f" % (2.3, 2.3468)
    5. "%7.5f %7.5f" % (2.3, 2.3468)
    6. "Time left %02d:%05.2f" % (1, 37.374)
    7. "%3d" % ("14")
    [text, §4.8, Discussion problem 4]

Programming Exercises

Remember to turn in your error logs and refinement files.

  1. (20 points) Numerologists claim to be able to determine a person’s character traits based on the “numeric value” of a name. The value of a name is determined by summing up the values of the letters of the name, where ‘a’ is 1, ‘b’ is 2, ‘c’ is 3 etc., up to ‘z’ being 26. For example, the name “Zelle” would have the value 26 + 5 + 12 + 12 + 5 = 60 (which happens to be a very suspicious number, by the way). Write a program that calculates the numeric value of a single name provided as input. Note that upper case letters and lower case letters have the same value. Please turn in the program in the file number.py.
    [text, §4.8, Programming Exercises problem 6, modified]
  2. (20 points) Write a function called reverse() that takes a string variable as a parameter. Then write a program that has the user enter a string, reverses it using that function, and prints both the entered string and the reversed version on a single line. You may not use recursion (that is, you cannot use the method on pp. 435–436 of the text). Please turn in the program in the file rev.py.
  3. (30 points) Word count. A common utility on Unix/Linux systems is a small program called “wc”. This program counts the number of lines, words (strings of characters separated by blanks, tabs, or new lines), and characters in a file. Write your own version of this program. The program should accept a file name as input and then print three numbers showing the count of lines, words, and characters in the file. Please turn in your program in the file wc.py.
    [text, §4.8, Programming Exercises problem 15, modified]

Extra Credit (Programming)

Remember to turn in your error logs and the refinement file.

  1. (20 points) A palindrome is a phrase that is spelled the same backwards and forwards (ignoring all non-letters, and treating upper case and lower case letters as the same). An example is “A man, a plan, a canal—Panama!” Using the unction you wrote in problem 4, write a program to determine if the string a user enters is a palindrome. Please turn in your program in the file pal.py.
    Hint: You will need to look in the book for how to test for equality and for how to print something if two strings are equal and something else if they are not equal. Look in sections 7.1 and 7.2.