## Spring2005 Midterm 2 Review

Below are questions like those we plan to ask on Midterm REMOVED (Mar 16). The exam will consist of 4 to 6 questions like these.

1. 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!
2. We will try to always point out if an answer is wrong. We won't always point out when the answer is right.
3. We are glad to work with you toward the right answer. We will give hints, and I'm glad to respond to partial guesses.

### Lower or Louder

Consider these two programs:
(a)
```def sound1():
snd = makeEmptySound(3)
value = 0
for i in range(1,getLength(snd)):
posn = i % 100
if posn < 25:
value = value + 100
if 25 < posn < 75:
value = value - 100
if posn > 75:
value = value + 100
setSampleValueAt(snd,i,value)
play(snd)
return snd
```

b
```def sound2():
snd = makeEmptySound(3)
value = 0
for i in range(1,getLength(snd)):
posn = i % 200
if posn < 50:
value = value + 200
if 50 < posn < 150:
value = value - 200
if posn > 150:
value = value + 200
setSampleValueAt(snd,i,value)
play(snd)
return snd

```

(1) Which of these two produces a louder sound?
(2) Which produces a lower sound?
(3) What is the frequency of each sound? (The sampling rate of an empty sound is always 22050 Hz)
(4) What is the maximum amplitude of each sound?

Questions, comments, and answers for Midterm Exam 2 Review Spring 2005: Lower or Louder

### The Story of the Internet

Explain what happens when you ask a browser to view http://www.cnn.com. In your explanation, use the terms (in this order): URL, HTTP, protocol, domain name server, TCP/IP, IP address, web server, server directory, database, HTML, JPEG.

Questions, comments, and answers for Midterm Exam 2 Review Spring 2005: The Story of the Internet

### Which did which?

Consider these two pieces of code:
(a)
```def sound3():
pre = makeSound(getMediaPath("preamble10.wav"))
for i in range(1,getLength(pre)):
value = getSampleValueAt(pre,i)
if 500<value<2000:
setSampleValueAt(pre,i,0)
play(pre)
return pre
```

b.
```def sound4():
pre = makeSound(getMediaPath("preamble10.wav"))
for i in range(1,getLength(pre)):
value = getSampleValueAt(pre,i)
if 500<value<2000:
setSampleValueAt(pre,i,3000)
if -500>value>-2000:
setSampleValueAt(pre,i,-3000)
play(pre)
return pre
```

Which one of these generated which of these sounds, viewed in the MediaTools:

Questions, comments, and answers for Midterm Exam 2 Review Spring 2005: Which did which

### 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 Spring 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'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.

(c) What is dot notation and when do you use it?

(d) Why is red a bad color to use for chromakey?

(e) What's the difference between a function and a method?

(f) Why is a tree a better representation for files on a disk than an array? Why do you have many directories on your disk, and not just one gigantic one?

(g) What are some advantages that Vector-Based graphics have over Bitmap Graphical representations (like JPEG, BMP, GIF)?

Questions, comments, and answers for Midterm Exam 2 Review Spring 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?

(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 Spring 2005: What's the underlying representation?

### Sound Timing Questions

(a) How would you figure out the index of where the third second starts in a sound?

(b) Write a program that takes a sourceSound and a targetSound as input, then copy the sourceSound starting at 1 second and ending at 1.5 second into the targetSound starting 2 seconds into the targetSound.

(c) Write a program where you move one second of an input sound from the 4th second of the sound to the start of the 2nd second.

Questions, comments, and answers for Midterm Exam 2 Review Spring 2005: Sound Timing

### 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
```

1. What do each of the loops in the above program do? What does the final sound sound like?
2. 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 Midterm Exam 2 Review Spring 2005: Re-mixing the recipe

### Encoding and Decoding

Remember the string method replace().

```>>> letter="Mr. Mark Guzdial REMOVEDs the pleasure of your company at..."
>>> print letter.replace('a','!')
Mr. M!rk Guzdi!l REMOVEDs 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 vowels and key consonants are replaced with symbols, using this table:

 FROM TO e # i ! a @ o % u ^ r - s = t :

(b) Write a second function that takes an encoded string as input, and returns the original unencoded string.

Questions, comments, and answers for Midterm Exam 2 Review Spring 2005: Encoding and Decoding

### Count 'em up

(a) Write a function called countAs that will take a string as input. The string will contain only a's, b's, and c's, e.g., "abccbbaa" or "aaabbbccc". Count the number of a's in the input, and print the count.

(b) Now, write a function to count the a's, b's, and c's, and print out ALL THREE 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 Spring 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 inintials (like your professor's!) 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 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 of your professor!

Be sure to close close the files that you open!

Questions, comments, and answers for Midterm Exam 2 Review Spring 2005: The Cover-Up

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 REMOVED: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")
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"```