← return to practice.dsc10.com
Welcome! The problems shown below should be worked on on
paper, since the quizzes and exams you take in this course will
also be on paper.
We encourage you to complete this worksheet in
a live discussion section. Solutions will be made available after all
discussion sections have concluded. You don’t need to submit your
answers anywhere.
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)
.
Suppose we have imported the math
module using
import math
. Consider the nested expression below:
int(math.sqrt(math.pow(4 * 2 ** 4, min(9 % 4, 9 / 4, 9 - 4))))
How many function calls are there in this expression? How many arguments does each function have?
Answer: 4 function calls: one argument for
int()
, one for math.sqrt()
, two for
math.pow()
, three for min()
.
There are four function calls. One is a call to the type-conversion
function int()
, which takes one argument. Another is a call
to math.sqrt()
, which takes one argument. Another is a call
to math.pow()
, which takes two arguments. Finally is a call
to the built-in function min()
, which in this case takes
three arguments, but generally can take two or more arguments.
What does this expression evaluate to?
Answer: 8
For nested evaluation, it is helpful to work from the inside out.
Let’s evaluate some arithmetic expressions first. 9 % 4
evaluates to 1
because when we divide 9
by
4
, there is a remainder of 1
. Additionally,
9 / 4
evaluate to 2.25
, and 9 - 4
evaluates to 5
. Starting with the inner most function call,
we see min(9 % 4, 9 / 4, 9 - 4)
is equiavlent to
min(1, 2.25, 5)
which evaluates to 1
.
The next-most inner function call is the call to
math.pow()
which takes two arguments: a number for the
base, and a number for the exponent. We’ve already evaluated the
exponent, but we need to evaluate the base of 4 * 2 ** 4
.
Using the order of operations, we know we need to evaluate the exponent
first. So 4 * 2 ** 4
is equivalent to 4 * 16
or 64
.
Therefore,
math.pow(4 * 2 ** 4, min(9 % 4, 9 / 4, 9 - 4))
simplifies
to math.pow(64, 1)
, which Python evaluates to be
64.0
, a float
.
Next, math.sqrt(64.0)
evaluates to 8.0
.
Finally, the type conversion function int(8.0)
evaluates to
8
.