In addition to the concepts below, you should also know the
concepts from Quiz 1,
Quiz 2, and Quiz 3.
You should understand and/or be able to use the following terms and Python concepts:
- 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
- file I/O
- 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.
- Write a function called avgList that, given a list
of numbers, calculates and returns their average. For example,
avgList([5, 10, 5, 4]) should return 6. Write a
main program to test the function.
- Write a function called maxList that, given a list of
numbers, finds 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.
- Consider the following program.
test = "whee"
check = "e"
answer = helper(test, check)
print("the answer is %d" % (answer))
def helper(word, letter):
print("word: %s" % (word))
print("letter: %s" % (letter))
x = 0
for ch in word:
if ch == letter:
x = x+1
# draw stack here, just before return
- Draw the call stack as it would look just prior to returning from helper.
- What is the program's output (i.e. what does it print when the program is run)?
- What does the helper function compute?
- What would helper("moobaalalala", "a") return?
- What would helper("zoo", "a") return?
(Question 4 Answer Key)
- For the following program, show the full output when the program is
run to completion, and draw the stack as it would look just before the computer
executes the return count statement.
OUTPUT: STACK DIAGRAM:
def update(L, S):
count = 0
data = S.split()
for item in data:
if item not in L:
count = count + 1
# draw stack here
words = ['roses','are','red']
line = 'violets are blue'
result = update(words,line)
(Question 5 Answer Key)
- 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:
Your program will create and print out the string "TGIF"