CSE 1310 -
Midterm 2 Practice Questions
Problem 1
What does this program print?
def fx(x):
d = x[:]
size = len(d)
for i in range(1, size):
d[i-1] = d[i-1] + d[i]
i += 1
return d
## main ##
d = [1, 2, 3, 4, 5]
a = fx(d)
for num in a:
print num
Problem 2
What does this program print?
def function1(num):
my_list = [2]
count = 1
for i in range(0, num-1):
my_list.append(count*my_list[count-1])
count += 1
return [my_list, max(my_list)]
## main ##
n = 4
(x, y) = function1(n) # Note multiple returns
print x, y
Problem 3
What does this program print?
def eye(n):
result = []
for r in range(0, n):
row = []
for c in range(0, n):
if r==c:
row.append(1)
else:
row.append(0)
result.append(row)
return result
## Main ##
num = 3
matrix = eye(num)
for row in matrix:
print row
Problem 4
What does this program print?
def f(z):
l = []
for i in range(1, z+1):
if z % i == 0:
l.append(i)
return l
## Main ##
i = 16
z = f(i)
for item in z:
print item
Problem 5
Write a function foo that satisfies the following specifications:
- foo takes in one argument, which is a string containing only lower-case characters, upper-case characters, and spaces.
- foo returns True if the string is a palindrome, and False otherwise.
- don't worry about checking for errors in the arguments. Just make sure that, when the argument satisfies the requirements, the output is the right one.
A palindrome is a word or phrase that reads the same forward as backward. For example, the string 'hannah' is a palindrome.
You can assume all characters are lower case letters, upper case letters, or spaces, and no other characters. Note that
you will need to remove the spaces from the phrase and make all the letters the same case in order for this to work properly.
For example, if the argument is the string 'go hang a salami im a lasagna hog', the function should return True.
Problem 6
Write a function that receives an integer as an argument and computes the factorial of the integer. The factorial of
an integer is the product of all the positive integers less than or equal to that integer. For example, the factorial of
5 (denoted by 5!) is 5*4*3*2*1 = 120. The function should verify the input is a positive integer and return None if it is not.
Problem 7
Write a program that takes a string as an argument and returns a new string that only keeps the vowels and spaces of
the input string and omits the consonants. You can assume that the argument only contains letters and spaces, you do not need to write code to check for that. For example, if you passed
the function 'hello there', it would return 'eo ee'.
Problem 8
Python provides a string method, string.title()
, that takes in a string as an argument and returns a new string in which
the first letter of each word in the input string is capitalized. For example, if you pass the string 'this is the title of
my book', it returns the string 'This Is The Title Of My Book'. Write your own function title(string)
that
does the same, obviously without using the built-in string.title()
method.
Problem 9
What does this program print?
x = []
count = 0
while count < 10:
t = count + 2
if t > 5:
x.append(count)
else:
x.append(t)
count = count + 1
for number in x[3:]:
print number
Problem 10
String processing: can you loop through strings and make decisions based on characters?
Write a function foo that satisfies the following specifications:
- foo takes a string as an argument. Do not worry about error handling, just make sure that foo does the right thing when the argument is indeed a string.
- foo returns the number of vowels in the argument string.
Problem 11
(Note: This problem really defines three possible midterm questions, we would not be including this entire problem as a single question. However, a midterm question at the level of difficulty of writing just foo, or just foo2, or just foo3, would be fair game).
Can you perform string slicing (also applicable to lists and tuples)?
Write a funtion foo that satisfies the following specifications:
- foo takes three arguments: word, low, high. word is a string, and low and high are two integers. Do not worry about cases where the arguments do not comply with this requirement.
- foo returns a new string that is a slice of the original, from the position specified by low up to and including the position specified by high.
Also, write a function foo2, that is like foo, except that it returns the slice in reverse order.
In addition, write a function foo3, that is like foo, except that it returns a string of all characters EXCEPT for the slice returned by foo.
Problem 12
(Note: the solution to this problem would be too long to be a good fit for an exam. However, it would be a fair game to include an exam question that only requires support of only three commands, e.g., just supporting insert, delete, and print).
Modifying lists
Write a program that starts with an empty list and presents the user a menu that allows them to append
elements, insert elements, delete elements, print the list, pop an element and print it, and find an element
and print its position
This is how the program should work:
- start with an empty list L.
- Enter an infinite loop, where, at each iteration, the system should:
- ask the user to enter a string among "append", "insert", "delete", "print", "pop", "find".
- if the user enters "append", the system should get from the user a value, and append it to the list L.
- if the user enters "insert", the system should get from the user a value and a position, and insert the value at the specified position of list L. If the specified position is not valid, the system should print "invalid position" and not crash.
- if the user enters "delete", the system should get from the user a value, and remove the first occurrence of that value from list L. If the value does not occur in L, the system should print "value not found" and not crash.
- if the user enters "print", the system should print the current contents of list L.
- if the user enters "pop", the system should print the value of the last element of the list L, and remove that last element from L. If the list is empty, the system should print "empty list" and not crash.
- if the user enters "find", the system should get from the user a value, and print the position of the first occurrence of that value in L. If the value does not occur in the list, the system should print "not found" and not crash.
- if the user enters "q", the program quits.
Problem 13
Nested lists - do you know how to access nested lists? What does the following program print?
L = [[1, 3, [5]], [44, 5], [8]]
print L[0]
print L[0][2]
print L[0][2][0]
print L[2]
print L[1][1]
Problem 14
Write a function foo that takes as argument a string S1 that contains only letters and spaces, and returns a string S2 that contains first the consonants of S1 (in the order in which they appear in S1), and then the vowels of S1 (again, in the order in which they appear in S1). For example: foo('Hello world') should return the string 'Hllwrldeoo'.
Problem 15
Write python code that creates a list L that satisfies the following specifications:
- L[0][2] is equal to 77
- L[1][0][0] is equal to 10
- L[1][0][2] is equal to 40
- L[2] = 1000
- L[3][1] is equal to "hello"
Note: there are multiple correct answers for this problem.
Problem 16
Write a function foo that satisfies the following specifications:
- foo takes in one argument, which is an integer N greater than or equal to 1.
- foo returns a list L of N elements. The element at position i is a string with i+1 characters, and all characters of that string are equal to the character 'a'.
- don't worry about checking for errors in the arguments. Just make sure that, when the argument satisfies the requirements, the output is the right one.
For example, foo(3) returns the list ['a', 'aa', 'aaa'].
Problem 17
What does this program print?
"""
assumptions:
- L is a list of numbers.
"""
def foo(L):
# L must have an odd number of elements, else return None.
if (len(L) % 2) == 0:
return None
half_length = len(L) / 2 #note: if foo has 5 elements, half_length is 2.
for i in L:
smaller = 0
larger = 0
for j in L:
if j <= i:
smaller = smaller + 1
if j >= i:
larger = larger + 1
if (smaller > half_length) and (larger > half_length):
return i
print foo([5, 10, 1, 20, 30])
Problem 18
We define that a string S is a well-formed name of a person if it follows the conventions:
- S follows the form last_name + ", " + first name, where last_name and first_name are substrings.
- last_name and first_name both start with a capital letter, and then only include lower-case letters
- Between last_name and first_name, S contains only a comma and then a space character.
- S should contain no additional characters beyond last_name, first_name, and the comma and space that separate last_name from first_name.
For example, "Smith, John" is a well-formed name. "Smith, John Edward" is NOT a well-formed name. "Smith-Johnson, Mary" is NOT a well-formed name.
Write a function check_name that takes in as argument a string S and returns True if S is a well-formed name, and False otherwise. Don't handle the case where the argument S is not a string, but do handle correctly all cases where S is indeed a string.
Problem 19
Write a function foo that translates word-by-word from language A to language B. In particular, foo should satisfy the following specifications:
- foo takes in two arguments. The first argument is called
dictionary
, and the second argument is called sentence
.
-
dictionary
is a list. Every element of dictionary
is a list containing two strings. The first string is a word in language A, and the second string is the translation of the first string in language B.
-
sentence
is a list of strings. Each element of sentence
is a word in language A, and appears as the first element of some element of the dictionary
argument.
- foo returns a list of strings. The i-th element of that list is the translation (according to the dictionary) of the i-th element of the
sentence
argument.
- don't worry about checking for errors in the arguments. Just make sure that, when the arguments satisfies the specifications above, the output is the right one.
For example, if we execute the following code:
dictionary = [['house', 'casa'], ['is', 'es'], ['sky', 'cielo'], ['today', 'hoy'], ['Monday', 'lunes']]
sentence = ['today', 'is', 'Monday']
print foo(dictionary, sentence)
we should get the output:
['hoy', 'es', 'lunes']