Spring 2006 Midterm 2 Review
Note: The idea of this review is not to tell you exactly what is going to be on the test but to demonstrate concepts that may appear on the upcoming exam. Although we try to comprise all topics, there is a possibility that other topics covered in class and not on this review will be included on the exam
Please try these questions! Post your answers, questions, comments, and concerns 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 can give hints, and we're glad to respond to partial guesses.
- 80% of the TA's are not robots so it may take a while to get a response. Please be patient. Especially if you post in the wee hours of the morning.
- All questions highlighted in blue are a bit trickier. They aren't all that necessary for the test, but if you can solve them, then you know you're doing well.
- These review questions in general are harder than the test.
Whoooo! Look at that text go!
What is the tickertape doing?
Which way does the text move?
How many frames are created?
What do the frame names look like?
def tickertape(directory, string):
for frame in range(100):
canvas = makePicture(getMediaPath("640x480.jpg"))
x = frame * getWidth(canvas) / 100
y = abs(50 - frame) * getHeight(canvas) / 100
addText(canvas, x, y, string)
num = str(frame)
if len(num) == 1:
num = '0' + num
writePictureTo(canvas, directory + '//frame' + num + '.jpg')
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: WhooooOOOooo
Movie Code Fragment
Remember this part of the movie generation code?
def writeFrame(num, directory, framepict):
framenum=str(num)
if num < 10:
writePictureTo(framepict, directory + "//frame00" + framenum + ".jpg")
if num >= 10 and num < 100:
writePictureTo(framepict, directory + "//frame0" + framenum + ".jpg")
if num >= 100:
writePictureTo(framepict, directory + "//frame" + framenum + ".jpg")
- What does it do?
- Why is it needed?
- Can you write something shorter?
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: Some Movie Code
What this do?
Consider the below program:
def pharaoh(collardGreens, friedOkra):
for i in range(1, friedOkra + 1):
keke = ''
for banana in range(i):
keke = keke + collardGreens
print keke
for i in range(1, friedOkra):
keke = ''
for banana in range(friedOkra - i):
keke = keke + collardGreens
print keke
What happens when you pass a string in for collardGreens and a positive integer for friedOkra?
Try to figure this out without using JES.
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: What this do?
Tracing the printing
def newFunction(a, b, c):
print a
list1 = range(1, len(c))
value = 0
for x in list1:
print b
value = value + 1
print c
print value
If you call the function above by typing:
>>> newFunction("I", "you", "walrus")
What will you see?
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: Tracing the printing 2
Compute the grade
Students in a hypothetical class have three exams. Exam 1 and Exam 2 are midterms that together are worth 40% of the final grade weighted equally. Exam 3 is the final exam that is worth 60% of the final grade.
Write a function called finalGrade that takes in the three exam scores as parameters and computes the final score. You should print the final score, and print the grade as follows:
| If the final grade is: | the computer should print: |
| >=90 | Congratulations, you got an A! |
| >=80 and <90 | You got a B. |
| >=70 and <80 | You got a C. |
| >=60 and <70 | You got a D. |
| <60 | Uh-oh. |
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: Compute the grade
String Fun
Write out what each of these programs will return.
def thingamabob():
source = "Georgia Institute of Technology"
vowels = "aeiou"
source = source.lower()
for i in vowels:
loc = source.find(i)
if loc <> -1:
source = source[:loc] + source[loc + 1:]
return source
def thingamajig():
source = "Georgia Institute of Technology"
vowels = "aeiou"
source=source.lower()
for i in vowels:
loc = source.find(i)
while loc <> -1:
source = source[:loc] + source[loc + 1:]
loc = source.find(i)
return source
If you don't know what a while loop is, it basically works the same way as an if statement. If the logical statement after the word while is true, then the code below it will run. The only difference is once it's done with the indented code, it'll go back to the while statement and check to see if it's true again. If it is, then it'll do it again. So whenever you see a while statement, think "while this statement is true, do this...". While Loops will not be on the midterm, but you may find other concepts from this question beneficial to your review. For more information about the wonderful word of while loops, see page 240 in the book.
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: String Fun
Short Novel
(a) Give one example of a task for which you would not write a program.
(b) Give another example of a task for which you would write a program.
(c) What is dot notation and when do you use it?
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: Short Novel
001110100010110100101001
For each of the below, see if you can figure out the representation in terms of bits and bytes.
(a) Internet addresses are four numbers, each between 0 and 255. How many bits are in an Internet address?
(b) In networking, computer port numbers range from 1 to 65,536. How many bits are needed to represent a port number?
(c) Each pixel's color has three components: red, green, and blue, each of which can be between 0 and 255. How many bits are needed to represent a pixel's color?
(d) A string in some systems can only be up to 1023 characters. How many bits are needed to represent the length of a string in such a system?
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: 001110100010110100101001
Encoding and Decoding
Remember the string method replace()?
>>> letter = "Mr. Mark Guzdial requests the pleasure of your company at..."
>>> print letter.replace('a','!')
Mr. M!rk Guzdi!l requests the ple!sure of your comp!ny !t...
>>> print letter.replace('a','!').replace("e","#")
Mr. M!rk Guzdi!l r#qu#sts th# pl#!sur# of your comp!ny !t...
(a) Write a function to encode an input string so that some key consonants are replaced with symbols, using this table:
(b) Write a second function that takes an encoded string as input, and returns the original unencoded string with the consonants restored.
(c) Write a function that replaces all the a's with b's and replaces all b's with a's
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: Encoding and Decoding
Count 'em up
(a) Write a function called countAs that will take a string as input. Count the number of a's in the input, and return (but not print) the count.
(b) Now, write a function to count the a's and e's, and print out both counts.
(c) Write a function that will tally up all letters and print out totals for each. It is possible to do this without 26 if statements.
Hint: Sometimes strings go through an identity crisis and act like lists. That means you can use them in for loops like this.
>>> for i in "Hello":
... print i
...
H
e
l
l
o
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: Count 'em up 2
The Cover-Up
You have just been caught by the FBI for downloading illegal music. During your questioning though, you are left with the laptop that contains all the incriminating evidence. There are hundreds of log files in a single directory with your initials in the files. You need to replace all instances of your initials with your TA's initials to throw the FBI off your case.
Remembering what you learned about listing jpeg files in a directory from hw4 (or atleast what you're about to remember):
import os
...
for file in os.listdir(directory):
if file.endswith(".txt")
... perform operations on the current text file
...and remembering how to open files for reading and writing:
myfile = open(fileNameAndLocation, "rt") #open file for reading
myfile = open(fileNameAndLocation, "wt") #open file for writing, clearing the contents of the file
...you must write a function coverUp that takes in a directory name and replaces all instances of your initials in any ".txt" files.
Be sure to close the files that you open!
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: The Cover-Up
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.
Feeling l33t? Try this...
- Write a function called add that takes in a name, address, city, and phone number and ADDS IT to the currently existing phone book file in the proper format.
- Write a function called remove that takes in a name and removes that name from the phonebook if it exists.
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: Address book functions
Poking at your hard drive
Consider the following code that was run in the command area of JES...
>>> import os
>>> folder = pickAFolder()
>>> files = os.listdir(folder)
>>> file = files[0]
>>> foo = file.rfind('.')
>>> bar = file[foo:]
Assuming that a valid folder was chosen with pickAFolder() and there was atleast one file in it, which of the 4 types (number, string, list, object) are the following:
- folder
- files
- file
- foo
- bar
What sort of information would you expect to learn if you were to run the following command next?
>>> print bar
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: Poking at your hard drive
Fun with HTML
- How does <title> differ from <h1>?
- What information goes between <html> and </html>?
- What information goes between <body> and </body>?
- What information goes between <head> and </head>?
- What would you find inside the <html> and </html> but not surrounded by <body> or <head> tags?
Questions, comments, and answers for Midterm Exam 2 Review Spring 2006: Fun with HTML
Links to this Page