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 8.
In addition to all concepts from Quiz 3, you should understand the following:
Python concepts
-
accumulating lists using
+andappend -
lists and strings as objects
-
string methods (e.g.,
split,strip,isdigit) -
passing lists to functions
-
stack diagrams for functions that take lists as parameters
-
-
using the
inoperator -
file I/O (e.g.,
open, reading the contents of a file line-by-line) -
top-down design and function stubs
Practice problems
-
Write a function called
is_vowel(letter)that has one parameter,letter. This function should returnTrueif the letter is a vowel (upper or lowercase),Falseif not. For example, callingis_vowel("i")should returnTrue, andis_vowel("Q")should returnFalse. (answer) -
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 countstatement.def main(): words = ['roses', 'are', 'red'] print(words) line = 'violets are blue' result = update(words, line) print(result) print(words) ################ def update(word_lst, phrase): count = 0 data = phrase.split() # splits on spaces with no parameters for item in data: if not (item in word_lst): word_lst.append(item) count = count + 1 # draw stack here return count main() -
Write a function called
get_zipcode()that ask the user to enter their zip code. Be sure that the user only enters 5-digit numbers. Any other input should force the user to enter the value again. Once the user has entered a valid zipcode, return the value as a string. -
You are trying to implement the game tic-tac-toe. You will store the board as a list of 9 strings. When the game starts, each element of the board will be an empty string. Once a user plays in a spot on the board, the element will change to an
Xor anO. For example, after four turns, theboardvariable might be equal to this:['', 'X', '', 'O', '', 'O', 'X', '', '']Assume that you have these functions already written for you:
-
get_space(board)asks the user to type which space they want to play in on the board and returns an integer representing the index in the list they want to play in. Theget_spacefunction only accepts valid, open spaces on the board. For example, given the board shown above, theget_spacefunction would only allow the user to enter the values 0, 2, 4, 7, or 8. -
winner(board)returns'X'ifXwon the game,'O'ifOwon the game,'T'if there are no spaces left and nobody won, or''if there are spaces left and nobody won. -
display(board)prints the board to the screen. For example, given the board above,display(board)would print this:| X | ----------- O | | O ----------- X | |-
Write the stub for the
winner(board)function. -
Write the implementation for the
get_space(board)function. -
Extra practice if you have time: Write a
main()function that uses the above functions to play the game of tic-tac-toe. Assume thatXalways goes first.
-
-
-
Given the assignments shown below, what is the value and type of each expression?
letters = "abacab" words = ["only two", "more weeks", "until", "Thanksgiving"] pt = Point(100, 200) circ = Circle(pt, 5) VALUE | TYPE ---------------------------------------------------------- len(letters) | ---------------------------------------------------------- len(words) | ---------------------------------------------------------- len(words[2]) | ---------------------------------------------------------- "a" in words | ---------------------------------------------------------- "a" in words[3] | ---------------------------------------------------------- "a" in letters | ---------------------------------------------------------- words[1][1] | ---------------------------------------------------------- words[0].split(' ') | ---------------------------------------------------------- pt.getX() | ---------------------------------------------------------- pt.getY() == 200 | ---------------------------------------------------------- circ.getRadius() | ---------------------------------------------------------- circ.getCenter() | -
Given an input file named "numbers.txt" that consists of some number of lines, each with a single value. For example, the first few lines of the file might look like:
18 2 -63 94 15 57Complete the program below which should:
-
read in the values from the file and append them to a list
-
use the
convertfunction (that you will write) to convert a list of strings into a new list of integers and returns that new list. -
use the
find_minfunction (that you will write) to find the smallest number in the list of integers.def main(): infile = open("numbers.txt", "r") str_list = [] # a. Read all of the numbers in the file into the list str_list # Each number should be stored as a string in the list. num_list = convert(str_list) small = find_min(num_list) print("Min value in list of %d values is %d" % (len(num_list), small)) infile.close() # b. Implement the convert function, which should take in a list of strings # and convert it to a list of ints. For now, you can assume all of the strings # can be converted to integers. # c. Implement the find_min function, which should look through a list of # integers and return the minimum value # d. Can you rewrite the convert function so that any string that is not made # up of only digits is not converted to an int and then not stored in the new # list? in this example, the number -63 would not be in the new list returned # by convert since "-" is not a digit. main()Running the program above without step (d) on the list of numbers shown should produce the output:
Min value in list of 6 values is -63Once you implement step (d), the output should become:
Min value in list of 5 values is 2
-