Quiz 5a#
Question 1#
What is the type of the parameter for a mouseclick handler?
Refer to the CodeSkulptor documentation .
Boolean
Tuple
Number
String
List
There is no parameter. \
Tuple \
Question 2#
Which of the following expressions mutate, i.e., change, list
my_list
? If you’ve forgotten what the
operations do, you can look in the CodeSkulptor
documentation .
my_list.reverse()
my_list + [10, 20]
my_list.extend([10, 20])
my_list.append(10)
another_list.extend(my_list)
\
my_list.reverse()
my_list.extend([10, 20])
my_list.append(10)
\
Question 3#
We want to remove the element at the front of a list. For example, we
want the following code to print "apple"
and
["pear", "blueberry"]
, respectively. What
function or method call should replace the question marks?
fruits = ["apple", "pear", "blueberry"]
fruit = # ???
print fruit, fruits
fruits.pop()
fruits.remove("apple")
fruits[0]
fruits.pop(0)
fruits.remove(0)
fruits[1:]
\
fruits.pop(0)
\
Question 4#
Which of the following uses of range()
will
generate the list [2, 5, 8, 11, 14]
?
First, think about what each of these returns, but also try each in CodeSkulptor .
range(14, 1, -3)
range(2, 17, 3)
range(2, 14, 3)
\
range(2, 17, 3)
\
Question 5#
To correctly compute the product of a list
numbers
of numbers, what statement should
replace the question marks?
numbers = # …
# ???
for n in numbers:
product *= n
product = numbers[0]
product = numbers[1]
product = 1
product = 0
product = []
\
product = 1
\
Question 6#
We can loop over strings, too!
The following incomplete function is a simple, but inefficient, way to reverse a string. What line of code needs to replace the questions marks for the code to work correctly?
def reverse_string(s):
"""Returns the reversal of the given string."""
# ???
for char in s:
result = char + result
return result
print reverse_string("hello")
result = ""
result = " "
result = []
result = 0
\
result = ""
\
Question 7#
Imagine a game on a map. At the beginning, we might want to randomly assign each player a starting point. Which of the following expressions may we use in place of the question marks to correctly implement this functionality?
import random
def random_point():
"""Returns a random point on a 100x100 grid."""
return (random.randrange(100), random.randrange(100))
def starting_points(players):
"""Returns a list of random points, one for each player."""
points = []
for player in players:
point = random_point()
# ???
return points
points.append(point)
points + point
point.append(points)
point.extend(points)
points.extend(point)
points += point
\
points.append(point)
\
Note that even it may look intuitive, points += point
won’t work, because operators operate typically with objects of the same type. In our case points
is a list so if we add another list (or tuple), then points
will add this list to itself - in other words extend itself.
Question 8#
The following function is supposed to check whether the given list of
numbers is in ascending order. For example, we want
is_ascending([2, 6, 9, 12, 400])
to return
True
while
is_ascending([4, 8, 2, 13])
should return
False
.
def is_ascending(numbers):
"""Returns whether the given list of numbers is in ascending order."""
for i in range(len(numbers)):
if numbers[i+1] < numbers[i]:
return False
return True
However, the function doesn’t quite work. Try it on the suggested tests to verify this for yourself. The easiest fix is to make a small change to the highlighted code. What should it be replaced with?
range(len(numbers) - 1)
range(1, len(numbers))
range(len(numbers - 1))
range(len(numbers)) - 1
\
range(len(numbers) - 1)
\
Question 9#
Turn the following English description into code:
Create a list with two numbers, 0 and 1, respectively.
For 40 times, add to the end of the list the sum of the last two numbers.
What is the last number in the list?
To test your code, if you repeat 10 times, rather than 40, your answer should be 89.
Enter answer here:\
x = [0, 1]
for _ in range(10):
x.append(x[-1] + x[-2])
print(x[-1])
x = [0, 1]
for _ in range(40):
x.append(x[-1] + x[-2])
print(x[-1])
165580141