← return to practice.dsc10.com
These problems are taken from past quizzes and exams. Work on them
on paper, since the quizzes and exams you take in this
course will also be on paper.
We encourage you to complete these
problems during discussion section. Solutions will be made available
after all discussion sections have concluded. You don’t need to submit
your answers anywhere.
Note: We do not plan to cover all of
these problems during the discussion section; the problems we don’t
cover can be used for extra practice.
prices
is an array of prices, in dollars, of different
products at the grocery store. Similarly, calories
is an
array of the calories in these same products, in the same order.
What does type(prices[0])
evaluate to?
int
float
str
The price of the first product.
Answer: float
prices[0]
represents the price in dollars of some
product at the grocery store. The data type should be a
float
because prices are numbers but not necessarily
integers.
What does type(calories[0])
evaluate to?
int
float
str
The calories in the first product.
Answer: int
Similarly, calories[0]
represents the calories in some
product at the grocery store. The data type should be int
because calories in foods are always reported as integers.
When we divide two arrays of the same length, their corresponding
elements get divided, and the result is a new array of the same length
as the two originals. In one sentence, interpret the meaning of
min(prices / calories)
.
Answer: This is the cost per calorie of the product which has the lowest cost per calorie, which you might say is the cheapest food to fuel up on (like instant ramen or pasta).
True or False: min(prices / calories)
is the same as
max(calories / price)
.
Answer: False
The former is measured in dollars per calories (a very small number), whereas the latter is measured in calories per dollar (a very big number).
However, there is a connection between these two values. The product
that has the lowest price per calorie is the same product with the most
calories per dollar. So these numbers refer to the same grocery store
product, and we can convert one value into the other by taking the
reciprocal, which swaps the numerator and denominator of a fraction.
Therefore, it’s true that min(prices / calories)
is the
same as 1 / max(calories / price)
.
Consider the following four assignment statements.
= "5"
bass = 2
tuna = ["4.0", 5, 12.5, -10, "2023"]
sword = [4, "6", "CSE", "doc"] gold
What is the value of the expression bass * tuna
?
Answer: "55"
The average score on this problem was 48%.
Which of the following expressions results in an error?
int(sword[0])
float(sword[1])
int(sword[2])
int(sword[3])
float(sword[4])
Answer: int(sword[0])
The average score on this problem was 51%.
Which of the following expressions evaluates to
"DSC10"
?
gold[3].replace("o", "s").title() + str(gold[0] + gold[1])
gold[3].replace("o", "s").upper() + str(gold[0] + int(gold[1]))
gold[3].replace("o", "s").upper() + str(gold[1] + int(gold[0]))
gold[3].replace("o", "s").title() + str(gold[0] + int(gold[1]))
Answer:
gold[3].replace("o", "s").upper() + str(gold[0] + int(gold[1]))
The average score on this problem was 92%.
Evaluate the expression
(np.arange(1, 7, 2.5) * np.arange(8, 2, -2))[2]
.
Answer: 24.0
This question although is daunting at first, is best solved by
breaking up the question into parts. First, let us think about the first
part, np.arange(1, 7, 2.5)
. In order to answer this, we
must figure out what np.arange()
does. What
np.arange()
does is it creates a numpy
array
that contains regularly spaces values between a start value and an end
value (start is inclusive, end is exclusive). So in this first case, our
starting value is 1, our end value is 7, and the regular interval or
step size is 2.5. So this call, np.arange(1, 7, 2.5)
, will
output the numpy
array
np.array([1.0, 3.5, 6.0])
because we start at 1, and
continue adding 2.5 stopping at the last value that’s less than 7. The
reason the resulting np.array([])
containts all
float
values is because one of the numbers is not an
int
, and all elements in the array have to have the same
data type. Now that we have evaluated the first half, let us now solve
for np.arange(8, 2, -2)
. Now this part may seem a little
tricky because of the negative regular interval (step size), but it is
the same logic as before. The output will simply be
np.array([8, 6, 4])
. In order to get that, we start at 8,
and continue to decrease our start value by 2 stopping before we reach
2. Now that we have evaluated both np.arange(1, 7, 2.5)
and
np.arange(8, 2, -2)
, it is now time to multiply.
Multiplication of two numpy
arrays is simply a pair wise
multiplication. So in our case, we will be multiplying
np.array([1.0, 3.5, 6.0]) * np.array([8, 6, 4])
, which
results to np.array([8.0, 21.0, 24.0])
. Again, paying
attention to the datatypes, the reason that
np.array([8.0, 21.0, 24.0])
contains float
values rather than int
values is because when you multiply
an int
by a float
, your answer will be a
float
. Now that we have evaluated
(np.arange(1, 7, 2.5) * np.arange(8, 2, -2))
to be
np.array([8.0, 21.0, 24.0])
, we now just need to access the
element in position 2, which is 24.0
.
Consider the following assignment statement.
= np.array([5, 9, 13, 17, 21]) puffin
Provide arguments to call np.arange
with so that the
array penguin
is identical to the array
puffin
.
= np.arange(____) penguin
Answer: We need to provide np.arange
with three arguments: 5, anything in (21,
25], 4. For instance, something line
penguin = np.arange(5, 25, 4)
would work.
The average score on this problem was 90%.
Fill in the blanks so that the array parrot
is also
identical to the array puffin
.
Hint: Start by choosing y
so that
parrot
has length 5.
= __(x)__ * np.arange(0, __(y)__, 2) + __(z)__ parrot
Answer:
x
: 2
y
: anything in (8,
10]z
: 5
The average score on this problem was 74%.
Suppose x
and y
are both int
s
that have been previously defined, with x < y
. Now,
define:
peach = np.arange(x, y, 2)
Say that the spread of peach
is the difference
between the largest and smallest values in peach
. The
spread should be a non-negative integer.
Using array methods, write an expression that
evaluates to the spread of peach
.
Answer: peach.max() - peach.min()
The average score on this problem was 62%.
Without using any methods or functions, write an
expression that evaluates to the spread of peach
.
Hint: Use [ ]
.
Answer:
peach[len(peach) - 1] - peach[0]
or
peach[-1] - peach[0]
The average score on this problem was 36%.
Choose the correct way to fill in the blank in this sentence:
The spread of peach
is ______ the
value of y - x
.
always less than
sometimes less than and sometimes equal to
always greater than
sometimes greater than and sometimes equal to
always equal to
Answer: always less than
The average score on this problem was 48%.
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
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
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
The average score on this problem was 79%.
Suppose that weights
is an array containing the weights,
in kilograms, of several leopard sharks living in La Jolla Cove. Several
leopard sharks represented in weights
weigh more than 10
kilograms.
Suppose that we have imported a module called sharkpy
by
running the code import sharkpy
. The sharkpy
module includes a function heavy
that takes as input an
array of shark weights and returns a smaller array containing only the
weights that are above 10 kilograms.
Using the heavy
function and an array method of
your choice, write an expression that evaluates to the weight,
in kilograms, of the lightest leopard shark in weights
that
weighs more than 10 kilograms.
Answer:
sharkpy.heavy(weights).min()
The average score on this problem was 59%.