← return to practice.dsc10.com
The problems in this worksheet are taken from past exams. Work on
them on paper, since the 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.
Note: We do not plan to cover all
problems here in the live 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.
What does type(calories[0])
evaluate to?
int
float
str
The calories in the first product.
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)
.
True or False: min(prices / calories)
is the same as
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
?
Which of the following expressions results in an error?
int(sword[0])
float(sword[1])
int(sword[2])
int(sword[3])
float(sword[4])
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]))
Evaluate the expression
(np.arange(1, 7, 2.5) * np.arange(8, 2, -2))[2]
.
For the problems that follow, we will work with a dataset consisting
of various skyscrapers in the US, which we’ve loaded into a DataFrame
called sky
. The first few rows of sky
are
shown below (though the full DataFrame has more rows):
Each row of sky
corresponds to a single skyscraper. For
each skyscraper, we have:
its name, which is stored in the index of sky
(string)
the 'material'
it is made up of (string)
the 'city'
in the US where it is located
(string)
the number of 'floors'
(levels) it contains
(int)
its 'height'
in meters (float), and
the 'year'
in which it was opened (int)
Below, identify the data type of the result of each of the following expressions, or select “error” if you believe the expression results in an error.
'height') sky.sort_values(
int or float
Boolean
string
array
Series
DataFrame
error
'height').get('material').loc[0] sky.sort_values(
int or float
Boolean
string
array
Series
DataFrame
error
'height').get('material').iloc[0] sky.sort_values(
int or float
Boolean
string
array
Series
DataFrame
error
'floors').max() sky.get(
int or float
Boolean
string
array
Series
DataFrame
error
0] sky.index[
int or float
Boolean
string
array
Series
DataFrame
error
Write a single line of code that evaluates to the name of the tallest
skyscraper in the sky
DataFrame.
Write a single line of code that evaluates to the average number of floors across all skyscrapers in the DataFrame.
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
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
Suppose students
is a DataFrame of all students who took
DSC 10 last quarter. students
has one row per student,
where:
The index contains students’ PIDs as strings starting with
"A"
.
The "Overall"
column contains students’ overall
percentage grades as floats.
The "Animal"
column contains students’ favorite
animals as strings.
What type is students.get("Overall")
? If this expression
errors, select “this errors."
float
string
array
Series
this errors
What type is students.get("PID")
? If this expression
errors, select “this errors."
float
string
array
Series
this errors
Vanessa is one student who took DSC 10 last quarter. Her PID is A12345678, she earned the sixth-highest overall percentage grade in the class, and her favorite animal is the giraffe.
Supposing that students
is already sorted by
"Overall"
in descending order, fill in the
blanks so that animal_one
and animal_two
both evaluate to "giraffe"
.
= students.get(__(x)__).loc[__(y)__]
animal_one = students.get(__(x)__).iloc[__(z)__] animal_two
If students
wasn’t already sorted by
"Overall"
in descending order, which of your answers would
need to change?
Neither y
nor z
would need to change
Both y
and z
would need to change
y
only
z
only
You are given a DataFrame called sports
, indexed by
'Sport'
containing one column,
'PlayersPerTeam'
. The first few rows of the DataFrame are
shown below:
Sport | PlayersPerTeam |
---|---|
baseball | 9 |
basketball | 5 |
field hockey | 11 |
Which of the following evaluates to
'basketball'
?
sports.loc[1]
sports.iloc[1]
sports.index[1]
sports.get('Sport').iloc[1]
Suppose you are given a DataFrame of employees for a given company.
The DataFrame, called employees
, is indexed by
'employee_id'
(string) with a column called
'years'
(int) that contains the number of years each
employee has worked for the company.
Suppose that the code
='years', ascending=False).index[0] employees.sort_values(by
outputs '2476'
.
True or False: The number of years that employee 2476 has worked for the company is greater than the number of years that any other employee has worked for the company.
True
False
What will be the output of the following code?
=2021-employees.get('years'))
employees.assign(start='start').index.iloc[-1] employees.sort_values(by
the employee id of an employee who has worked there for the most years
the employee id of an employee who has worked there for the fewest years
an error message complaining about iloc[-1]
an error message complaining about something else