← return to practice.dsc10.com
This quiz was administered in-person. It was closed-book and
closed-note; students were not allowed to use the DSC
10 Reference Sheet. Students had 20 minutes to work on
the quiz.
This quiz covered Lectures 1-4 of the Spring 2024 offering
of DSC 10.
⚠️ PDF version available here .
True or False: int(3.4) and round(3.4)
evaluate to the same value.
True
False
Answer: True
int drops everything after the decimal point, so
int(3.4) is 3. round goes to the
nearest integer. Since 3.4 is closer to 3 than to 4,
round(3.4) is also 3.
The average score on this problem was 89%.
True or False: For any float x, int(x) and
round(x) evaluate to the same value.
True
False
Answer: False
int always drops the decimal part, but
round can round up. For example, int(3.7) is
3, but round(3.7) is 4.
The average score on this problem was 95%.
Consider the following code.
iris = 3 / 1
poppy = 8 - 6
daisy = np.array([8, 1, 5])
lily = np.array([4, 2])
poppy = iris ** iris - iris * poppy
What is the value of poppy after this code is
executed?
Answer: 21.0
iris is 3.0, because / always
gives a float. poppy is 2.
The last line uses these values:
3.0 ** 3.0 - 3.0 * 2.
The exponent comes first: 27.0. Then the multiplication:
6.0. Then the subtraction:
27.0 - 6.0 = 21.0.
The answer is a float because iris is a
float.
The average score on this problem was 54%.
What is the result of the expression daisy + lily?
array([8, 1, 5, 4, 2])
array([12, 3, 5])
array([12, 3])
This expression errors.
Answer: Option D
Adding two arrays adds them position by position: first plus first,
second plus second, and so on. daisy has 3 elements and
lily has only 2, so the third element has nothing to pair
with, and Python raises an error. Option A is wrong because
+ never joins two arrays together.
The average score on this problem was 68%.
What is the result of the expression
daisy + lily[0]?
array([8, 1, 5, 4])
array([12, 5, 9])
array([10, 3, 7])
This expression errors.
Answer: Option B
lily[0] is 4, a single number. When you add
a number to an array, it is added to every element:
[8 + 4, 1 + 4, 5 + 4].
The average score on this problem was 79%.
You are tracking the growth of a flower stem over a seven-day period. The flower stem starts out at 24.5 cm and ends up at 29.7 cm.
Write one line of code that calculates the average daily growth, in
centimeters, and assigns the result to the variable
avg_growth. Do not round your answer.
Answer:
avg_growth = (29.7 - 24.5) / 7
The total growth is 29.7 - 24.5, which is 5.2 cm. To get
the growth per day, divide by the 7 days. The parentheses are needed
because division happens before subtraction. Without them, Python would
compute 29.7 - (24.5 / 7).
The average score on this problem was 84%.
Suppose flower_data is a DataFrame with information on
different species of flowers, where:
The "species" column contains the name of the
species of flower, as a string. Each value in this column is
unique.
The "petals" column contains the average number of
petals of flowers of this species, as an int.
The "length" column contains the average stem length
of flowers of this species in inches, as a float.
One of these three columns is a good choice to use as the index of
this DataFrame. Write a line of code that sets this column as the index
of flower_data, and assigns the resulting DataFrame to the
variable flowers.
Answer:
flowers = flower_data.set_index("species")
A good index has a different value in every row. The problem says
each "species" value is unique. Two species could have the
same number of petals or the same length, so those columns are poor
choices.
The average score on this problem was 79%.
Important: The following questions will use
flowers instead of flower_data.
Which of the following expressions evaluates to a DataFrame that is
sorted by "petals" in descending order?
flowers.sort_values(by = "petals", ascending = True)
flowers.sort_values(by = "petals", ascending = False)
flowers.get("petals").sort_values(ascending = True)
flowers.get("petals").sort_values(ascending = False)
Answer: Option B
sort_values(by="petals") sorts the whole DataFrame by
the "petals" column, and ascending=False puts
the largest values first. Option A sorts in ascending order. Options C
and D first use .get("petals"), so they give back a Series,
not a DataFrame.
The average score on this problem was 83%.
Suppose that the 4th row of flowers corresponds to a
rare species of flower named "fire lily". Fill in the
blanks below so that both of these expressions evaluate to the stem
length in inches of "fire lily".
i. flowers.get("length").loc[__(x)__]
ii. flowers.get("length").iloc[__(y)__]
Answer: (x): "fire lily", (y):
3
.loc looks up a row by its row label. Since the index is
now the species names, the row label is "fire lily".
.iloc looks up a row by its position, and positions start
at 0, so the 4th row is at position 3.
The average score on this problem was 83%.
Suppose that the 3rd row of flowers corresponds to the
species "stinking corpse lily". Using the
flowers DataFrame and the string method
.split(), write an expression that evaluates to
"corpse".
Answer:
flowers.index[2].split(" ")[1]
flowers.index[2] gives the 3rd row label, which is
"stinking corpse lily". .split(" ") breaks it
at the spaces into ["stinking", "corpse", "lily"]. Then
[1] picks the second element, "corpse".
The average score on this problem was 46%.