CS21: Quiz 4 Study Guide

In addition to the concepts below, you should also know the concepts from Quiz 1, Quiz 2, and Quiz 3.

You should be able to define the following terms:

• formal parameter
• actual parameter (also known as an argument)
• function definition
• function call
• call stack
• scope of a variable
• top-down design
• stubbed-out function
• pseudocode

Practice problems:

1. Write a function called getLetter() that asks the user for a letter and returns the given letter. Your function should check to make sure the user enters just a single letter. If the user enters anything other than a single letter, the function should ask again, until it receives valid input.
2. Write a function called avgList that takes a list of numbers as input and returns their average. For example, avgList([5, 10, 5, 4]) should return 6. Write a main program to test the function.
3. Write a function called maxList that takes a list of numbers as input and returns the largest number in the list. For example, maxList([7,4,8,1]) should return 8. Write a main program to test the function.
4. Consider the following program.
```def main():
print "in main"
test = "whee"
check = "e"

def helper(word, letter):
print "in helper"
print "word:", word
print "letter:", letter
x = 0
for ch in word:
if ch == letter:
x = x+1
# draw stack here
return x

main()
```
• Draw the call stack as it would look just prior to returning from helper.
• What is the program's output (i.e. what is printed when the program is run)?
• What does the helper function compute?
• What would helper("moobaalalala", "a") return?
• What would helper("zoo", "a") return?
5. Trace through the following code, show the output from its entire execution, and draw the stack as it would look before the return statement in function foo:
```def main():                     OUTPUT                       STACK
print "in main"
b = foo(5)
print len(b)
print b[0]
print b[1][1]

def foo(n):
print "in foo"
biglist = []
for i in range (n):
sublist = ["h", i, i+10]
biglist.append(sublist)
# draw stack here
return biglist

main()
```
What is the type and value of each of the following expressions:
```                      value                    type
(1) biglist
(2) biglist[2]
(3) biglist[2][0]
(4) biglist[1][2]
(5) len(biglist)
(6) len(biglist[1])
```
6. Write a program that reads in the lines from a file called infile.txt and creates an acronym from the first letter of each line. For example, if the input file is:
```Thanks
Goodness
It's
Friday
```
Your program will create and print out the string "TGIF"