CS 161 Workshop 3 - Practice with Expressions, Loops, and Lists

Week of Sept 27-30




1. Rewrite the following series of Python statements so as to obtain the same effect with a single statement.


# Program to convert temperature in Celcius (C) to Farenheit (F)

X = C * 9

Y = X / 5.0

F = 32 + Y


2. Rewrite the following Python statements into a series of statements, each of which performs only one mathematical operation, so as to have the same effect.


# Program to find the positive root of a quadratic equation, ax2+bx +c

x = ( -b + math.sqrt(b**2 - 4.0*a*c) / (2.0 * a)


3. The University of Rachacha (UR) has spring break April 8-15. The Rachacha Institute of Technology (RIT) has spring break April 13-20. Suppose the variable D contains an integer representing a day in the month of April. The following expression evaluates to True if and only if D is during UR's spring break:


D >= 8 and D <= 15


Write expressions that true in exactly the following circumstances:

(a) D is a day both schools are on spring break.


(b) D is a day that neither school is on spring break.


(c) D is a day that one or the other of the schools, but not both, are on spring break.




4. Write a program to find the sum of the first N natural numbers, where the value of N is input by the user.


5. A Fibonacci sequence is a sequence of numbers where each successive number is the sum of the previous two. The classic Fibonacci sequence begins: 1, 1, 2, 3, 5, 8, 13, ... Define a function Fib(N) that prints out the first N Fibonacci numbers.




6. What is the value of C after evaluating the following statements?


A = 'bst'

B = 'uop'

C = A[0] + B[0] + A[1] + A[1:3] + B[1:]


7. What is the output of the following statements?


A = ['Mary', 'John', 'Sam']

B = [85, 78, 94]

C = A + B

for i in range(3):

print 'Grade for ', C[i], ' is ', C[i+3]


8. A substitution cypher is a code that uses a table to replace letters with other letters. Consider the following substitution code:

letter a b c d e f g h i j k l m n o p q r s t u v w x y z  
code X z 8 & % 2 c E * ! 7 2 5 1 ; + g G   q } 6 = 9 w Q p

Note that a blank entry indicates the space character. Start by practicing encoding by hand the message "hello world".


You will write two different versions of a program that encodes messages.


(a) The first version uses lists. The code table is represented by the two lists created as follows:

Letter = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z ', ' ')

Code = ['X', 'z', '8', '&', '%', '2', 'c', 'E', '*', '!', '7', '2', '5', '1', ';', '+', 'g', 'G', ' ', 'q', '}', '6', '=', '9', 'w', 'Q', 'p']


You program should read a line of text from the terminal and print out its encoded version. The program should begin by reading the line and converting it into a list of characters as follows:


Message = list( raw_input('Message: ') )


You program should ONLY use loops, assignment statements, if statements, indexing (square brackets), equality comparisons ==, input, print, and the list-conversion operator list() used above. Note the following special fact about the print statement: if the print statement ends with a comma, a "new line" is NOT printed at the end of the line. For example,


print 'C',

print 'a ',

print 't'






all on one line. Hint: your program will use two loops, one inside the other. The outer loop will process each character of the input message. The inner loop will be used to determine how to encode that letter. Before writing Python code, write down the steps of your algorithm in English!


(b) Now write a second version of the program that uses only strings and no lists. It begin by defining the translation table as follows:


Letter = 'abcdefghijklmnopqrstuvwxyz '

Code = 'Xz8&%2cE*!7251;+gG q}6=9wQp'


Your program should use just one loop, for processing each character of the input message. Your program should use only loops, assignment statements, input, print, indexing (square brackets), and the string library function


string.find(<string>, <substring>)


which returns the starting index of <substring> in <string>. If <substring> does not appear in <string>, it returns 0. For example,


string.find('abc', 'b')


returns 1. Before writing Python code, write down the steps of your algorithm in English!


9. An anagram is a re-arrangement of the letters in a word or phrase that forms another word or phrase. Design a program that reads in a line, and prints out all the possible ways of rearranging the letters in the line. For example, if you input




the program should (in some order, not necessarily the order shown)









Finally, modify your program so that instead of printing out the rearrangements, it stores them as a list in strings in the variable Result. For example, the value of Result at the end of the program might be:

['cat', 'cta', 'act', 'atc', 'tac', 'tca']

Before writing Python code, write down the steps of your algorithm in English!