You are on page 1of 10

CSCA08HF 2011 Midterm Test

Duration 100 minutes


Aids allowed: none
Student Number:
Last Name: First Name:
Lecture Section (circle one): L01 (afternoon) L02 (morning)
Do not turn this page until you have received the signal to start.
(Please ll out the identication section above, write your name on the back
of the test, and read the instructions below.)
Good Luck!
This midterm consists of 5 questions on 10 pages (including this one).
When you receive the signal to start, please make sure that your copy is
complete.
At the end of the test is a page to be used for rough work, and a page
containing functions and methods that may be of use to you. If you use any
space for rough work, indicate clearly what you want marked.
Comments are not required except where indicated, although they may help
us mark your answers. They may also get you part marks if you cant gure
out how to write the code.
# 1: / 6
# 2: / 5
# 3: / 7
# 4: / 6
# 5: / 8
TOTAL: /32
Total Pages = 10 Page 1 contd. . .
CSCA08 F Midterm Test Fall 2011
The next two questions involve the sound module that we used in the rst assignment. At the end of the
test, youll nd a list of the sound functions we learned about. Note that you may not need to use all of
them.
You may assume that the sound module has already been imported use the line import sound.
Question 1. [6 marks]
For each part of this question, you need to write a few lines of code. For all three parts, assume that the
variable snd has already been dened and refers to a Sound object.
Part (a) [2 marks] Calling functions
Write code to create a silent Sound of the same length as snd and assign it to the variable silent snd.
Part (b) [2 marks] Looping over a Sound
Write code that prints the value of the left channel of each Sample in snd, each value printed on its own
line.
Part (c) [2 marks] Conditionals in Loops
Write code that prints the index of each Sample in snd for which the left and right channels are equal,
each value printed on its own line.
Page 2 of 10 contd. . .
CSCA08 F Midterm Test Fall 2011
Question 2. [5 marks]
Take a look at the following function denition for triple. The triple function will run without error,
but it does not do what the docstring says.
def triple(snd)
Return a copy of Sound snd with the left and right channels tripled.
The original snd is unchanged.
for samp in snd:
left = sound.get_left(samp)
right = sound.get_right(samp)
sound.set_left(samp, left * 3)
sound.set_right(samp, right * 3)
return snd
Part (a) [1 mark] Explain, in English, what is wrong with the triple function as written.
Part (b) [2 marks] In the space above, edit triple so that it performs as the docstring describes. You
may need to add additional lines or code and/or edit existing ones.
Part (c) [2 marks] Write the lines of code that will open a le called song.wav, call triple (assuming
the errors have been xed), and then play the new song (which will be three times as loud as the original).
Page 3 of 10 contd. . .
CSCA08 F Midterm Test Fall 2011
Question 3. [7 marks]
Each of the parts of this question contain some lines of code containing print statements. You should write
the output from running these lines of code.
Part (a) [1 mark]
Code: Output:
print 3 / 4
Part (b) [1 mark]
Code: Output:
x = 10
y = x + 3
print x, y
Part (c) [1 mark]
Code: Output:
for char in toronto:
if char != o:
print char
Part (d) [1 mark]
Code: Output:
s = toronto
print len(s)
print s[1]
print s[0] + s[2:]
Page 4 of 10 contd. . .
CSCA08 F Midterm Test Fall 2011
Part (e) [1 mark]
Code: Output:
a = [10, 20, 30]
z = a[0]
z += 1
print a
print z
Part (f) [1 mark]
Code: Output:
w = 0
for i in range(3):
w = w + i
print w
Part (g) [1 mark]
Code: Output
r = 4
while r > 0:
print r
r = r - 2
Page 5 of 10 contd. . .
CSCA08 F Midterm Test Fall 2011
Question 4. [6 marks]
Dene the function add spaces, which takes a string, and returns a new string with a space added after
each character.
For example:
add spaces(cscA08) should return c s c A 0 8
Note that there is a space after the nal character in the original string.
Fill in the function denition below.
def add_spaces(S):
Return a new string which contains all the characters of string S in the same
order, but with a space after each character.
Page 6 of 10 contd. . .
CSCA08 F Midterm Test Fall 2011
Question 5. [8 marks]
Part (a) [2 marks] Alphabetic characters are all the characters in
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Consider the string method isalpha:
S.isalpha() -> bool
Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
Write a call to isalpha which returns True:
Write a call to isalpha which returns False:
Part (b) [6 marks] Dene the function isalpha list, which takes a list of strings, and returns True
if all the strings are alphabetic, False otherwise.
For example:
isalpha_list([Ron, Harry, Hermione]) # True
isalpha_list([every, string, contains, only, letters]) # True
isalpha_list([1, string, contains, numbers]) # False
isalpha_list([l33t]) # False
Fill in the function denition below.
def isalpha_list(L):
Return True iff all the strings in list L contain only alphabetic characters.
Page 7 of 10 contd. . .
CSCA08 F Midterm Test Fall 2011
[Use the space below for rough work. This page will not be marked, unless you clearly indicate the part of
your work that you want us to mark.]
Page 8 of 10 contd. . .
CSCA08 F Midterm Test Fall 2011
Short Python function/method descriptions:
__builtins__:
abs(num) -> number
Return the absolute value of the number num.
len(seq) -> int
Return the number of items in the sequence seq. Sounds, Strings and Lists are sequences.
max(a, b, c, ...) -> value
With two or more arguments, return the largest argument.
min(a, b, c, ...) -> value
With two or more arguments, return the smallest argument.
raw_input([prompt]) -> string
Read a string from standard input. The trailing newline is stripped. The str prompt,
if given, is printed without a trailing newline before reading.
float:
float(x) -> float
Convert x to a float, if possible.
int:
int(x) -> integer
Convert x to an integer, if possible.
media:
choose_file() -> str
Prompt user to pick a file. Return the path to that file.
sound:
copy(snd) -> Sound
Return a copy of the Sound snd.
create_sound(duration) -> Sound
Return a new, silent Sound that is duration samples long.
get_index(samp) -> int
Return the index the Sample samp.
get_left(samp) -> int
Return the right channel value of the Sample samp.
get_right(samp) -> int
Return the right channel value of the Sample samp.
get_sample(snd, i) -> Sample
Return the Sample at index i in Sound snd.
load_sound(filename) -> Sound
Return the Sound in str filename.
play(snd)
Play the Sound snd.
set_left(samp, val)
Set the Sample samps left channel value to the int val.
set_right(samp, val)
Set the Sample samps right channel value to the int val.
list:
L.append(object)
append object to end
L.insert(index, object)
insert object before index
L.pop() -> item
remove and return item at last index.
L.pop(index) -> item
remove and return item at index.
L.remove(value)
remove first occurrence of value.
Page 9 of 10 contd. . .
CSCA08 F Midterm Test Fall 2011
Last Name: First Name:
Lecture Section (circle one): L01 (afternoon) L02 (morning)
Page 10 of 10 End of Examination

You might also like