CS 303e
Exam 3 Review Sheet
Some Solutions
The practice problems on this review
sheet are not complete - it provides extra practice over topics that
are covered on the exam, but it is not an exhaustive review of
everything we've covered or everything that you will be asked on the
exam. This sheet is no substitute for reviewing your class notes and
re-working examples and exercises that we did in class.
I will not provide solutions for these problems. You will have time to
ask questions about them in discussion, class and office hours before
the exam.
1. Re-work the review sheets for exam 1 and exam 2.
2.
Re-work the examples and exercises that are described in the online
class notes or that were assigned through discussion assignments or
puzzlers.
3. Create a dictionary that contains key-value
pairs, in which the key is a person's name (a string) and the value is
a phone number. Read a file phonelist.txt that has the following format:
Sam
455-6788
Elvis
366-7666
Nancy
222-3456
Sam
288-9000
Add all the entries in the file to your dictionary. If a name occurs
more than once, the value associated with that name should be a tuple
that contains all the phone numbers for that name. For the above file,
the dictionary should look like this:
{'Nancy': '222-3456', 'Sam': ('455-6788', '288-9000'), 'Elvis': '366-7666'}
def makePhoneBook(filename):
infile = open(filename, "r")
phoneBook = {}
# read the first name
name = infile.readline()
while name != "":
#check for newline
if name[-1] == "\n":
name = name[0:-1]
#read the associated number
number = infile.readline()
#check for newline
print number, len(number)
if number[-1] == "\n":
number = number[0:-1]
#if the name is in the phonebook
if name in phoneBook:
#it has at least one number
entry = phoneBook[name]
if type(entry) != str:
#must be a tuple, use tuple concatination
phoneBook[name] = entry + (number,)
else:
#must be a single number
phoneBook[name] = (entry, number)
#name not in phonebook
else:
phoneBook[name] = number
#read the next name
name = infile.readline()
return phoneBook
def main():
print makePhoneBook("phonelist.txt")
main()
4. Write a function getFew(alist, num) that takes a list alist and an
integer num, and returns a list that contains the first num values in
alist. If num is not positive, return an empty list. If num is greater
than the length of alist, return a copy of alist.
getFew([1, 7, 6, 5], 2) --> [1, 7]
getFew(["apple", "peach", "melon", "kiwi"], -3) --> []
getFew([18, 5, 3], 5) --> [18, 5, 3]
def getFew(alist, num):
if num <= 0:
return []
else:
length = min(num, len(alist))
myList = []
for i in range(length):
myList.append(alist[i])
return myList
5. Read a string from the user, and then print the following:
- the reverse of the string
- the number of digits that appear in the string
- the number of letters that appear in the string
- the last character in the string which is not whitespace
- the substring that contains all characters up to, but not including, the first whitespace character in the string
- the length of the string
- a list containing all the characters in the string
- the string with all letters converted to uppercase
def upToFirstWhitespace():
str = raw_input("Enter a string:")
i = 0
sub = ""
while not(str[i].isspace()) and (i < str.length):
sub += str[i]
print sub
6. Prompt the user to enter 2 strings, and then print:
- the characters that are in either or both strings
- the characters that are only in one or the other string
- the characters that are in the first string but not the second
- the characters that are in both strings
7. Write a graphics program that creates 10 white nested rectangles in a window with an orange background.
8. Read 20 strings from the user, and print the length of these strings to a file lenOutput.txt, one value per line.
9. Write a function makeList(s) that takes a string s, and returns a
list containing the letters and digits stored in s. For example:
makeList("hello, hey!") --> [h, e, l, l, o, h, e, y]
makeList("\nTalk $ \t1-800\n") --> [T, a, l, k, 1, 8, 0, 0]
10. Write a Python function getDigits(st) that takes a string st, and
returns a list that contains all the digits in st.
11. Create a list that contains 10 strings that you read from the user.
Sort this list, and print the strings to the screen both before and
after you sort.
12. Explain in detail how remove() works when it is called on a list.
What happens if you attempt to remove a value that is not in the list?
How do remove() and pop() work differently?
13. Which sequence types are mutable, and which are immutable?
14. Describe an application in which sets would be useful.
15. Write python code that displays the following output. You may only print one character at a time.
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
16. Write python code that displays the following output, printing only one character at a time.
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
import sys
for i in range(1, 10):
# print row i: 16-2(i-1) blanks, digits 1 to i, digits i-1 to 1
for j in range(16-2*(i-1)):
sys.stdout.write(" ")
for j in range(1, i+1):
sys.stdout.write(j)
for j in range(i-1, 0, -1):
sys.stdout.write(j)
print # go to next line
17. What is the value of x after the code is executed?
a)
s = 'kelp'
for y in s:
x = y
b)
x = 44%3 + 44/3
c) s = [3, 4, 11, 98, 222]
x = s[-3]
d) s = [3, 4, 11, 98, 222]
x = s[:2]
e) x = string.find("elvis lives", 'i')
f) l = [2, 3, 5, 77]
l.pop(2)
x = l
18. Write a function convert(num) that takes an integer num and returns a list that contains the digits of num.