Study Guide for CS 303E Test 3 (16 Aug 2013)
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)
* Define a simple class and create objects from that class.