ECS 10, Basic Concepts of Computing Spring Quarter 2009
Version of April 22, 2009 at 11:26 PM Page 1 of 2
Homework 4
Due Date: May 4, 2009 at 5:00PM 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:
a. "The Knights who say, " + s2
b. 3 * s1 + 2 * s2
c. s1[1]
d. s1[1:3]
e. s1[2] + s2[:2]
f. s1 + s2[-1]
g. string.upper(s1)
h. 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.
a. "Looks like %s and %s for breakfast" % ("spam", "eggs")
b. "There is %d %s %d %s" % (1, "spam", 4, "you")
c. "Hello %s" % ("Susan", "Computewell")
d. "%0.2f %0.2f" % (2.3, 2.3468)
e. "%7.5f %7.5f" % (2.3, 2.3468)
f. "Time left %02d:%05.2f" % (1, 37.374)
g. "%3d" % ("14")
[text, §4.8, Discussion problem 4]
Programming Exercises
Remember to turn in your error logs and refinement files.
3. (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]
4. (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