Docsity

Homework #4 - Basic Concepts of Computing | ECS 010, Assignments of Computer Science

Toggle sidebar
bg1
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
pf2

Partial preview of the text

Download Homework #4 - Basic Concepts of Computing | ECS 010 and more Assignments Computer Science in PDF only on Docsity!

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, " + s 2 b. 3 * s1 + 2 * s 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.

  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

ECS 10, Basic Concepts of Computing Spring Quarter 2009 Version of April 22, 2009 at 11 : 26 PM Page 2 of 2 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.

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