Sum2004 Midterm 2 Review
Below are questions like those we plan to ask on Midterm #2. The exam will consist of 4 or 5 questions like these.
Please do try these questions! Post your answers, questions, comments, concerns, and criticisms on the pages for each question. Those comments, questions, etc. can also be about each others' answers! If someone posts an answer that you don't understand, ask about it! If you see a question here that you know the answer to, don't keep it to yourself – help your fellow students!
We will be reading your answers. Here are the ground rules for the interaction.
- If you don't post, neither will we. We will not be posting official solutions to these problems at all! If one of you gets it right, terrific!
- We will try to always point out if an answer is wrong. We won't always point out when the answer is right.
- We are glad to work with you toward the right answer. We will give hints, and we're glad to respond to partial guesses.
What do they do?
(a)
def functionA(a,b,c):
if c<a:
print b
value=a+10
for z in range(1,c):
print "Howdy"
value=value+2
print value
print a
When executing functionA(10,8,5), what will print?
(b)
def functionB(a,b,c):
print a
number=1
list=range(1,c,b)
for x in list:
print b
number=number-1
print number
When calling the function by typing functionB(whoa,3,18), what will print?
Questions, comments, and answers for Sum2004 Midterm 2 Review - What will they do?
Short Essay
- What does the function getSamplingRate tell you and what does it take as input?
- What's the differerence between an array, a matrix, and a tree? Give an example where we have used each to represent some data of interest to us.
- What are some differences between top-down and bottom-up processing
- Why is red a bad color to use for chromakey?
- What's the difference between a function and a method?
- Why is including a doctype in a webpage important?
Questions, comments, and answers for Sum2004 Midterm 2 Review - Short Essay
Count 'em up
Write a function called countNumbers that will take a string of numbers as input (e.g. "15309132" or "7704973987"). Count the number of 1's and 2's in the input, and print the count.
Remember that strings are sequences.
>>> for i in "Hello":
... print i
...
H
e
l
l
o
Questions, comments, and answers for Sum2004 Midterm 2 Review - Count 'em up
Re-splicing the splice
Write a program called replaceWord that takes the sound thisisatest.wav and replaces the word "test" with the word "this". The following are the end points for each of the words in the sound:
| Recorded word | Index where it stops in the sound |
| This | 7865 |
| is | 27170 |
| a | 40326 |
| test. | 55770 |
Here is an example program done in class that copies the word "Test" from the end of the sound to the front.
def spliceTest():
file = r"C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\thisisatest.wav.wav"
source = makeSound(file)
target = makeSound(file) # This will be the newly spliced sound
targetIndex=1 # targetIndex starts at the beginning
for sourceIndex in range( 40327, 55770): # Where the word "Test" is in the sound
setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex))
targetIndex = targetIndex + 1
play(target) #Let's hear and return the result
return target
Assume that the sound file location is the same place as above. Make sure that none of the word "Test" remains – put zeroes in the samples after the word "This" to the end of the sound.
Questions, comments, and answers for Sum2004 Midterm 2 Review - Re-splicing the splice
Re-mixing the recipe
We've learned over the last few weeks that we can recombine recipes–in some ways, like how one can recombine physical, cooking recipes. A nice unit of recombining is a loop and its body.
Take a look at the program below, then answer the questions:
def spliceWeird():
file = r"C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\thisisatest.wav"
source = makeSound(file)
target = makeSound(file) # This will be the newly spliced sound
targetIndex=1 # targetIndex starts at the beginning
# Loop A
for sourceIndex in range( 40327, 55770): # Where the word "Test" is in the sound
setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex))
targetIndex = targetIndex + 1
# Loop B
for sourceIndex in range( 40327, 55770,2): # Where the word "Test" is in the sound
setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex))
targetIndex = targetIndex + 1
# Loop C
for sourceIndex in range( 55770,40327,-2): # Where the word "Test" is in the sound
setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex))
targetIndex = targetIndex + 1
# Loop D
for sourceIndex in range( 40327, 55770,2): # Where the word "Test" is in the sound
setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex))
targetIndex = targetIndex + 1
# Loop E
for sourceIndex in range( 55770,40327,-2): # Where the word "Test" is in the sound
setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex))
targetIndex = targetIndex + 1
# Loop F
for sourceIndex in range( 55770,40327,-1): # Where the word "Test" is in the sound
setSampleValueAt(target, targetIndex, getSampleValueAt( source, sourceIndex))
targetIndex = targetIndex + 1
play(target) #Let's hear and return the result
return target
- What do each of the loops in the above program do? What does the final sound sound like?
- Is there anything left in the sound when Loop F finishes? What is the value of targetIndex when the function ends?
Questions, comments, and answers for Sum2004 Midterm 2 Review - Re-mixing the recipe
Address book functions
Let's imagine that you have an address book file, named "address.txt", conveniently located in your JES folder (hint: You don't need a path to the file!) The format of the file looks like this:
Charlie Brown:1919 Peanuts Lane:Atlanta, GA:404-992-9292
Peppermint Patty:2020 Cashew Street:Atlanta, GA:404-299-2929
Calvin:101 Tiger Lane:Decatur, GA:770-899-8989
You are to write two functions:
- The first is called lookup that will accept a string as input that will be some part of a name to look up. You should print out the complete line of name, address, and phone-number for the matching person–if the person is found. If the person is not found, you should print "Not found."
- The second is called phone that will take the same input, but will print out JUST THE PHONE NUMBER and "Not found" if the input name is not found.
You might want to reference these two programs that we discussed in class, to look up phone book information, and to look up the temperature in a Web page.
def phonebook():
return """
Mary:893-0234:Realtor:
Fred:897-2033:Boulder crusher:
Barney:234-2342:Professional bowler:"""
def phones():
phones = phonebook()
phonelist = phones.split('\n')
newphonelist = []
for list in phonelist:
newphonelist = newphonelist + [list.split(":")]
return newphonelist
def findPhone(person):
for people in phones():
if people[0] == person:
print "Phone number for",person,"is",people[1]
def findTemperature():
weatherFile = getMediaPath("AtlantaWeather1.html")
file = open(weatherFile,"rt")
weather = file.read()
file.close()
# Find the Temperature
humloc = weather.find("Humidity")
if humloc != -1:
# Now, find the "," where the temp starts
temploc = weather.rfind(",",0,humloc)
endline = weather.find("<",temploc)
print "Current temperature:",weather[temploc+1:endline]
if humloc == -1:
print "They must have changed the page format -- can't find the temp"
Questions, comments, and answers for Sum2004 Midterm 2 Review - Address book functions
Gendered random sentences
Remember the random sentence generator that we did in class?
import random
def sentence():
nouns = ["Mark", "Adam", "Angela", "Larry", "Jose", "Matt", "Jim"]
verbs = ["runs", "skips", "sings", "leaps", "jumps", "climbs", "argues", "giggles"]
phrases = ["in a tree", "over a log", "very loudly", "around the bush", "while reading the Technique"]
phrases = phrases + ["very badly", "while skipping","instead of grading", "while typing on the CoWeb."]
print random.choice(nouns), random.choice(verbs), random.choice(phrases)
Create a new sentence function that takes as input a single letter, "M" for Male or "F" for Female. If the input is "M", print a random sentence with a noun as the name of a known male. If the input is "F", print a random sentence with a noun as the name of a known female.
Questions, comments, and answers for Sum2004 Midterm 2 Review - Gendered random sentences
Duplicate a list
Write a function duplicateList to input a list and then duplicate each element of the list. If the input is [1,2,3] return [1,1,2,2,3,3]
Recall that a list is manipulated like this:
>>> mylist = ["This","is","a", 12]
>>> print mylist
['This', 'is', 'a', 12]
>>> print mylist[0]
This
>>> for i in mylist:
... print i
...
This
is
a
12
>>> print mylist + ["Really!"]
['This', 'is', 'a', 12, 'Really!']
Questions, comments, and answers for Sum2004 Midterm 2 Review - Duplicate a List
Reverse a file
Write a function reverseFile that inputs the name of a file, opens it, and then writes it out to a file in the JES directory with the lines reversed.
If the original file "original.txt" (assume, for now, in the directory JES) contains the lines:
This is
a perfectly
ordinary
file, okay?
Then calling reverseFile("original.txt") should create a file in the JES directory named reversed with the contents:
file, okay?
ordinary
a pefectly
This is
You may want to reference this example from class lecture:
>>> file=open(program,"rt")
>>> lines=file.readlines()
>>> print lines
['def littlepicture():\n', ' canvas=makePicture(getMediaPath("640x480.jpg"))\n',
' addText(canvas,10,50,"This is not a picture")\n', ' addLine(canvas,10,20,300,50)\n',
' addRectFilled(canvas,0,200,300,500,yellow)\n', ' addRect(canvas,10,210,290,490)\n',
' return canvas']
>>> file.close()
(You'll also want the list functions described earlier.)
Questions, comments, and answers for Sum2004 Midterm 2 Review - Reverse a file
Adding a return
Your TA keeps complaining because nobody is including the return statement in their homeworks. So you decide to write a program that will go through and open up all the students' files in the TA's folder and then insert the return statement. Let's assume that each function should return the parameter (the input) that the function has.
For example, this function should include return(canvas)
at the end.
def littlepicture(canvas)
addText(canvas,10,50,"Here is an example")
addRect(canvas,10,210,290,490)
Questions, comments, and answers for Sum2004 Midterm 2 Review - Adding a return
Link to this Page
- Hotspots #3 last edited on 5 May 2008 at 10:40 am by c-76-17-124-0.hsd1.ga.comcast.net