Study Guide for CS 303E Test 3 (12 Aug 2011)
The last test is cummulative but there will be
greater emphasis on material that was not covered
in the earlier tests.
For each exam topic I have given some sample questions
for you to solve. The best way to tackle the material
would be to write solutions to these problems and run
them on the computer. I do not have answer keys for
these questions.
* Dictionary and its associated functions
Q. Create a function that accepts a string as an input
parameter and then prints out the frequency of all the
characters other than white space characters. Use a
dictionary and parse the input string character by
character. The character will be the key and the
frequency of its occurence the value. Print all the
key-value pairs at the end.
* Set and its associated functions
Q. Write a function that accepts two lists of strings as
input and returns a list that has only the strings common
to both the input list. Use a sets inside the function.
* List and its associated functions
Q. Create a 2-D list that has 3 rows and 5 columns and populate
that list with random numbers in the range 1 through 100.
Q. Write a function that takes as input two 2-D list and returns
True if they are the same and False otherwise.
Q. Write a function that takes as input two 2-D list of the same
size and returns a 2-D list that has the sum of the corresponding
elements of the two lists.
Q. Write a function that takes as input a 2-D list and returns
a 2-D list with each row in reverse order.
Q. Write a function that takes as input a 2-D list and returns a
2-D list with each column in reverse order.
Q. Write a function that takes as input two 1-D list of the same
size and returns a single number that is the sum of the corresponding
products of the elements of the two lists.
a = [1, 2, 3]
b = [4, 5, 6]
Your function should return 1*4 + 2*5 + 3*6 = 32
Q. Write a function that takes as input a 1-D list and returns True
if it is sorted in ascending order and False otherwise.
Q. Write a function that sums the rows, columns, and diagonals of a
2-D list.
Q. Given a 1-D list of 3 numbers sort the list in ascending order
without using the built-in sort function or loops.
Q. Given a 1-D list shuffle the contents of the list.
* Basic algorithms of sorting, searching, and merging
Q. Consider the binary search code shown below. What is the last
value that is printed for count and mid when
a = [ 2, 5, 8, 9, 11, 14, 16, 19, 22, 25, 35, 41, 45, 55, 62]
x = 23
def binarySearch (a, x):
lo = 0
hi = len(a) - 1
count = 0
while (lo <= hi):
count = count + 1
mid = (lo + hi) / 2
print count, mid
if (x < a[mid]):
hi = mid - 1
elif (x > a[mid]):
lo = mid + 1
else:
return mid
return -1
* String and its associated functions
Q. Write a function that takes as input two strings and returns True
if the two string are anagrams and False otherwise. The strings may
have spaces and punctuation marks and upper and lower characters. You
are only concerned that they have the same letters (case insensitive).
* File manipulations for reading, writing, and appending
Q. Write a function that takes as input two strings - username and
password and returns True if they match a username and password
combination in a password file and False otherwise. The password file
is called passwd.txt and the username and passwords are stored one pair
to a line and each pair of username and password is separated by a colon.
username1:password1
...
usernameN:passwordN
* Write recursive code from recurrence relations and write the output of
recursive functions given the input.
Q. Write a recursive function that implements this recurrence relation
f(n) = 2 * f(n - 1) + n where f(0) = 0
What is f(8)?
Q. Consider the recursive function shown below. What will it return
for recurse (45, 75)?
def recurse (m, n):
if (m == n):
return m
elif (m > n):
return recurse (m - n, n)
else:
return recurse (n - m, m)
* Permutation Problems
Q. A secretary writes letters to A, B, C, and D. She also prepares four
envelopes addressed to A, B, C, and D. She manages to put all the letters
in the wrong envelopes. Enumerate (list) all the ways she can do that.
Q. You have the following books that you would like to arrange on your
shelf.
- War and Peace by Leo Tolstoy
- Anna Karenina by Leo Tolstoy
- Magic Mountain by Thomas Mann
- Death in Venice by Thomas Mann
- Arms and the Man by Bernard Shaw
- Candida by Bernard Shaw
Enumerate the different ways you can arrange the books as long as you
keep the books by the same author together.
Q. A, B, C, D, and E go to a ball game. A and B want to sit next to each
other but C and D prefer not to. Enumerate the different ways that they
can sit on the same bench.
* Combination Problems
Q. Home owners A, B, C, D, E, and F have all agreed to serve on the Home
Owners' Association. But the Home Owners' Association just needs three
people. A is willing to serve only if B serves, though B has not made
that same condition. C and D both refuse to serve if the other is in the
association. Enumerate the different associations that you can form.
Q. A side show at Coney Island is described as follows: There were ten
little dummies which you were to knock over with baseballs. The man said:
"Take as many throws as you like at a cent apiece and stand as close as
you please. Add up the numbers on all the men that you knock down and
when the sum amounts to exactly fifty, neither more nor less you get a
genuine 25 cent Maggie Cline cigar with a gold band around it." The
numbers on the ten dummies were:
15, 9, 30, 21, 19, 3, 12, 6, 25, 27
What numbers in that series add up to 50?