Python Programming Chapter 9 Exercises Zelle

1. Given the initial statements:

import string s1 = "spam" s2 = "ni!"

Show the result of evaluating each of the following string expressions.

(g) string.upper(s1)

2. Given the same initial statements as in the previous problem, show a Python expression that could construct each of the following results by performing string operations on s1 and s2 .

3. Show the output that would be generated by each of the following program fragments.

print ch

(b) for w in string.split("Now is the winter of our discontent..."):

print w

(c) for w in string.split("Mississippi", "i"):

print w,

for s in string.split("secret","e"):

for ch in "secret":

4. 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"

(b)

"There is %d

%s %d %s"

% (1,"spam", 4, "you")

(c)

"Hello %s" %

("Suzie",

"Programmer")

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

)

5. Explain why public key encryption is more useful for securing communications on the Internet than private (shared) key encryption.

6. A certain CS professor gives 5-point quizzes that are graded on the scale 5-A, 4-B, 3-C, 2-D, 1-F, 0-F. Write a program that accepts a quiz score as an input and prints out the corresponding grade.

7. A certain CS professor gives 100-point exams that are graded on the scale 90-100:A, 80-89:B, 70-79:C, 60-69:D, <60:F. Write a program that accepts an exam score as input and prints out the corresponding grade.

8. An acronym is a word formed by taking the first letters of the words in a phrase and making a word from them. For example, RAM is an acronym for "random access memory." Write a program that allows the user to type in a phrase and outputs the acronym for that phrase. Note: the acronym should be all uppercase, even if the words in the phrase are not capitalized.

9. 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 auspicious number, by the way). Write a program that calculates the numeric value of a single name provided as input.

10. Expand your solution to the previous problem to allow the calculation of a complete name such as "John Marvin Zelle" or "John Jacob Jingleheimer Smith." The total value is just the sum of the numeric value for each name.

11. A Caesar cipher is a simple substitution cipher based on the idea of shifting each letter of the plaintext message a fixed number (called the key) of positions in the alphabet. For example, if the key value is 2, the word "Sourpuss" would be encoded as "Uqwtrwuu." The original message can be recovered by "reencoding" it using the negative of the key.

Write a program that can encode and decode Caesar ciphers. The input to the program will be a string of plaintext and the value of the key. The output will be an encoded message where each character in the original message is replaced by shifting it key characters in the ASCII character set. For example, if ch is a character in the string and key is the amount to shift, then the character that replaces ch can be calculated as: chr(ord(ch) + key).

12. One problem with the previous exercise is that it does not deal with the case when we "drop off the end" of the alphabet (or ASCII encodings). A true Caesar cipher does the shifting in a circular fashion where the next character after "z" is "a". Modify your solution to the previous problem to make it circular. You may assume that the input consists only of letters and spaces.

13. Write a program that counts the number of words in a sentence entered by the user.

14. Write a program that calculates the average word length in a sentence entered by the user.

15. Write an improved version of the Chaos program from Chapter 1 that allows a user to input two initial values and the number of iterations and then prints a nicely formatted table showing how the values change over time. For example, if the starting values were .25 and .26 with 10 iterations, the table might look like this:

index

0.25

0.26

1

0.

731250

0.

750360

2

0.

766441

0.

730547

3

0.

.698135

0.

767707

4

0.

821896

0.

695499

5

0.

570894

0.

825942

6

0.

955399

0.

560671

7

0.

166187

0.

960644

8

0.

540418

0.

147447

9

0.

968629

0.

490255

10

0.

118509

0.

974630

16. Write an improved version of the future value program from Chapter 2. Your program will prompt the user for the amount of the investment, the annualized interest rate, and the number of years of the investment. The program will then output a nicely formatted table that tracks the value of the investment year by year. Your output might look something like this:

Years Value

0 $2000.00

1 $2200.00

2 $2420.00

3 $2662.00

4 $2928.20

5 $3221.02

6 $3542.12

7 $3897.43

17. Redo any of the previous programming problems to make them batch oriented (using text files for input and output).

18. Word count. A common utility on Unix/Linux systems is a small program called "wc." This program analyzes a file to determine the number of lines, words, and characters contained therein. Write your own version of wc. 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.

+1 0

Post a comment