Summer 2005 - Exam 2 Review Questions
Below are questions like those we plan to ask on Midterm #2 (Friday, July 8).
Please 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 can give hints, and we're glad to respond to partial guesses.
What is the tickertape doing? Which way does the text move? How many frames are created? How do the frame names look?
def tickertape(directory, string):
for frame in range(0, 100):
canvas = makePicture(getMediaPath("640x480.jpg"))
addText(canvas, (600 - (frame * 10)) % 640, 100, string)
framestr = str(frame)
if frame < 10:
writePictureTo(canvas, directory + "//frame0" + framestr + ".jpg")
if frame >= 10:
writePictureTo(canvas, directory + "//frame" + framestr + ".jpg")
#tickertape(r"C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\tickertape",
#"Welcome to CS1315!")
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: Tickertape
Remember this part of the movie generation code? What does it do? Why is it needed?
def writeFrame(num, directory, framepict):
# Have to deal with single digit vs. double digit frame numbers differently
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")
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: Some Movie Code
What gets printed?
Consider the below program:
def testme(p, q, r):
if q > 50:
print r
value = 10
for i in range(1, p):
print "Everybody"
value = value - 1
print value
print r
If we execute testme(5, 51, "Homestar Rocks!"), what will print?
Hint: DON'T TRY IT IN JES! Try to figure it out by walking through the code. On the exam, you will NOT have access to JES. Checking your answer using JES afterwards makes sense.
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: What gets printed
Compute pay rates
Write a function called pay that takes in as input a number of hours worked and the hourly rate to be paid. Compute the gross pay as the hours * the rate. But then compute a tax.
| If the pay is... | Charge a tax of... |
| < 100 | .25 |
| >= 100 | .50 |
Compute the taxed amount as tax rate * gross pay.
Print the gross pay and the net pay (gross - tax).
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: Compute the pay rate
Tracing the printing
def newFunction(a, b, c):
print a
list1 = range(1, 5)
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 the computer print?
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: Tracing the printing
Compute the grade
Students in a class in a hypothetical class have three exams. Exam1 and Exam2 are midterms that together are worth 40% of the final grade. Exam3 is a final exam that is worth 60% of the final grade.
Write a function finalGrade that takes in the three exam scores as parameters (e.g., def finalGrade(exam1, exam2, exam3): )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 | Better luck next time. |
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: Compute the grade
Tracing the String Programs
Write out what each of these programs will return.
(a)
def string4():
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
b. (While Loops will not be on the midterm, but you may find other concepts from this question are beneficial to your review.)
def string5():
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
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: Tracing the String Programs
Short Essay
(a) Give me one example of a task for which you would not write a program, and give me another example of a task for which you would write a program.
(b) What is dot notation and when do you use it?
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: Short Essay
What's the underlying representation?
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 the programming language Basic, lines can be numbered, each one between 0 and 65535. How many bits are needed to represent a line 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 if we ignore the alpha channel?
(d) A string in some systems can only be up to 1024 characters. How many bits are needed to represent the length of a string?
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: What's the underlying representation?
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.
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: 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 o's, and print out both counts.
Remember that strings are sequences.
>>> for i in "Hello":
... print i
...
H
e
l
l
o
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: Count 'em up
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 some other initials to throw the FBI off your case.
Remembering what you learned about listing jpeg files in a directory from hw4:
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
And remembering the function replace(toFind, toReplaceWith) returns a modified string, you must write a function coverUp that takes in a directory name and replaces all instances of your initials in any ".txt" files with the initials "abc"!
Be sure to close the files that you open!
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: 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.
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 Midterm Exam 2 Review Summer 2005: 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 Summer 2005: 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 Summer 2005: Fun with HTML
Teh Intarweb!
Breifly discuss the following and how they interact:
- HTML
- HTTP
- FTP
- URL
- DNS
- IP
- TCP/IP
- Server
- Client
Questions, comments, and answers for Midterm Exam 2 Review Summer 2005: Teh Intarweb!
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