Quiz 3 Study Guide
Quiz Study Guides are provided as a courtesy. You may work with other students on the questions, and ask questions about the guide during evening ninja help sessions, on piazza, or during meetings with faculty and staff. We do not provide full solutions to the quiz guides.
You are responsible for all material covered through the end of Week 5.
In addition to all concepts from Quiz 2, you should understand the following:
Python concepts
-
stack diagrams
-
scope of a variable
-
function definition
-
function call
-
function parameters: defining and calling
-
definite
for
loop -
indefinite
while
loop -
mutable and immutable types
-
list manipulation (accumulators, indexing, etc)
Practice problems
-
Write a program which asks the user for 4 words and then prints them in reverse order.
Here’s an example run of the program:
word 1: hello word 2: zebra word 3: unicorn word 4: corgi corgi unicorn zebra hello
-
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 amain
function to test the function in a complete program. -
Consider the following program:
def main(): print("in main") test = "whee" check = "e" answer = helper(test, check) print("the answer is %d" % (answer)) def helper(word, letter): print("in helper") print("word: %s" % (word)) print("letter: %s" % (letter)) x = 0 for i in range(len(word)): if word[i] == letter: x = x+1 # draw stack here, just before return return x main()
-
Draw the function stack as it would look just prior to returning from helper. [answer]
-
Which variables are in scope at this same point in the code?
-
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?
-
-
Write a program that reads in student grades from the user, until the user enters a -1. After reading in the -1, the program should compute and display the average grade.
Please enter your grades below. Enter a -1 when you are all done... grade 1: 98 grade 2: 87 grade 3: 65 grade 4: 95 grade 5: 80 grade 6: -1 The average of those 5 grades is 85.000
-
Analyze the following code:
m = ["t","i","m","e"] s = "time" m[1] = "a" s[1] = "a"
What happens when this code is executed? What are the values of
m
ands
afterward? -
Consider the following program:
def main(): values = [2,3,4,10] print(values) result = mystery(values) print("result: %d" % result) print(values) def mystery(L): total = 0 for i in range(len(L)): if is_odd(L[i]): L[i] = L[i]*2 total = total + L[i] # Q2: what does the stack look like at this point? return total def is_odd(number): # Q1: draw stack to this point (first time function is called) if number % 2 == 0: return False else: return True main()
-
Draw the function stack as it would look the first time
is_odd()
is called. [answer] -
Draw the function stack as it would look just prior to returning from
mystery()
. -
What variables are in scope at these same points in the code?
-
What is the program’s output (i.e. what does it print when the program is run)?
-
What does the
mystery()
function do?
-
-
Write a function called
getInt
that has two parameters,lo
andhi
. The function should prompt the user to enter an integer betweenlo
andhi
(inclusive), and then return the value entered. Your function should not return until the user enters a valid integer in the rangelo
tohi
. You may assume bothlo
andhi
are positive integers.Now add a
main()
function that callsgetInt
to get an integer from 1-10. Yourmain()
should then print out "Boo!" that many times.Here’s an example run of the full program:
Please enter an integer from 1-10: 99 Please enter an integer from 1-10: -40 Please enter an integer from 1-10: zebra Please enter an integer from 1-10: 4 Boo! Boo! Boo! Boo!