Quiz 4 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 EdSTEM, 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 9.
In addition to all concepts from Quiz 3, you should understand the following:
Python concepts
-
lists and strings as objects
-
list methods (e.g.,
append
) -
string methods (e.g.,
split
,join
,strip
,isdigit
,isalpha
) -
top-down design
-
file I/O (e.g.,
open
,readlines
) -
Searching: linear vs binary search
-
Analysis of algorithms, and run times:
-
\(\log N\) — logarithmic (e.g. binary search)
-
\(N\) — linear (e.g. linear search)
-
Practice problems
-
NOTE: There are additional search-specific practice questions in the
worksheet.txt
file in your week 9 class notes directory.-
How many steps would it take to do binary search on a list of size 1 million, when the item you are searching for is NOT in the list?
-
Binary search is much faster than linear search, so why don’t we use it for every search problem?
-
For each iteration of the loop in
binarySearch(x, L)
, show the index values forlow
,high
, andmid
, and the value stored at locationmid
. What will be returned by this function call?x = 67 L = [10, 12, 14, 21, 37, 44, 45, 46, 58, 67, 99] index 0 1 2 3 4 5 6 7 8 9 10 low | high | mid | L[mid] - - - - - - - - - - - - - | | | | | | | | | | | |
-
For each iteration of the loop in
binarySearch(y, L)
, show the index values for low, high, and mid, and the value stored at location mid. What will be returned by this function call?y = 4 L = [10, 12, 14, 21, 37, 44, 45, 46, 58, 67, 99] 0 1 2 3 4 5 6 7 8 9 10 low | high | mid | L[mid] - - - - - - - - - - - - - - | | | | | | | | | | | |
-
True/False questions:
-
Linear search requires a number of steps proportional to the size of the list being searched (T/F)
-
The python
in
operator performs a binary search (T/F) -
Binary search is an O(N) algorithm (T/F)
-
The number of times N can be divided by 2 before reaching 1 is the log (base 2) of N (T/F)
-
-
Write a function called
isVowel(letter)
that has one parameter,letter
. This function should returnTrue
if the letter is a vowel (upper or lowercase),False
if not. For example, callingisVowel("i")
should returnTrue
, andisVowel("Q")
should returnFalse
. -
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.def main(): words = ['roses','are','red'] print(words) line = 'violets are blue' result = update(words,line) print(result) print(words) ################ def update(L, S): count = 0 data = S.split() for item in data: if item not in L: L.append(item) count = count + 1 # draw stack here return count main()
-
The following
get_numbers(n)
function doesn’t work. Find and fix the bug in the code below.def get_numbers(n): """ Purpose: Read n numbers from the user and return them as a list Parameters: n -- the number of values to read from the user (integer) Return: a list of the numbers entered by the user """ for i in range(n): value_list = [] value = int(input("Enter a number: ")) value_list.append(value) return value_list
-
Assume you have these two functions already written:
-
getPick()
asks the user for "r,p,s?" and returns:-
"rock" if they enter r
-
"paper" if they enter p
-
"scissors" if they enter s
-
-
winner(user,comp)
returns:-
"user" if user won the game (e.g., user="rock", comp="scissors")
-
"comp" if comp won the game (e.g., user="rock", comp="paper")
-
"tie" if it’s a tie game (e.g., user="rock", comp="rock")
-
Write a main() function that uses the above functions to play one round of rock-paper-scissors. Here are a few sample runs of the program:
$ python3 rps.py r,p,s?: r Computer chose rock tie... $ python3 rps.py r,p,s?: r Computer chose paper Computer wins! $ python3 rps.py r,p,s?: r Computer chose scissors You win!
-
-
Write the stub for the
winner(user, comp)
function. -
Write the implementation for the
getPick()
function. YourgetPick()
function should only accept r, p, or s from the user. If they enter anything else, print an error message and ask again.r,p,s?: w please enter r, p, or s!!! r,p,s?: zebra please enter r, p, or s!!! r,p,s?: r
-
Given the assignments for
S
,L
, andN
, what is the value and type of each expression?S = "CS21 Rocks" L = ["May", "the Force", "be", "with you."] N = "".join(L) VALUE TYPE L len(L) len(S) "a" in L[2] "cs" in S L[1] len(L[1]) s.split() N
-
Given an input file named "numbers.txt" that consits of some number of lines, each with a single float value. For example, the first few lines of the file might look like:
4.5 -0.5 32.1 -8 54.1 12.3
Complete the program below to implement the functions
fix_list
andfind_max
:def main(): infile = open("numbers.txt", "r") text = infile.read() num_list = text.splitlines() """ # The above is equivalent to this code below. # Either method you prefer is fine. num_list = [] for line in infile: line = line.strip() num_list.append(line) """ fix_list(num_list) max = find_max(num_list) print("Max value in list of %d values is %f" % (len(num_list), max)) infile.close() ######################## # implement the fix_list function, which should take in a list of strings # and convert it to a list of ints # implement the find_max function, which should look through a list # and return the maximum value ######################## main()
-