← 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 8-11 of the Winter 2024 offering
of DSC 10.

The DataFrame `flights`

contains information about recent
flights, with each row representing a specific flight and the following
columns:

`"flight num" (str)`

: The unique code of the flight, consisting of a two-character airline designator followed by 1 to 4 digits (e.g.,`"UA1989"`

).`"airline" (str)`

: The airline name (e.g.,`"United"`

).`"departure" (str)`

: The code for the airport from which the flight departs (e.g.,`"SAN"`

).`"arrival" (str)`

: The code for the airport at which the flight arrives (e.g.,`"LAX"`

).

Suppose the function `first_two`

takes as input a string
and returns a string with the first two characters only. For example
`first_two("panda")`

is `"pa"`

. Using this
function, write an expression that evaluates to a Series containing the
**two-character airline designator** for each flight in
`flights`

.

**Answer:**
`flights.get("flight_num").apply(first_two)`

The average score on this problem was 78%.

Fill in the blanks below so that `grouped`

is a DataFrame
showing how many flights on each airline departed from each airport.

`= flights.groupby(___(x)___).___(y)___.get(["flight_num"]) grouped `

**Answer (x):** `["departure", "airline"]`

or
`["airline", "departure"]`

The average score on this problem was 82%.

**Answer (y):** `count()`

The average score on this problem was 89%.

Suppose the expression
`grouped.shape[0] == flights.shape[0]`

evaluates to
`True`

. Select all true statements below.

No two flights in

`flights`

were on the same airline.No two flights in

`flights`

had the same departure and arrival airports.Among all flights in

`flights`

that were on the same airline, no two had the same departure airport.Among all flights in

`flights`

from the same departure airport, no two were on the same airline.

**Answer:** Option 3 and Option 4

The average score on this problem was 80%.

Suppose we have another DataFrame `more_flights`

which
contains the same columns as `flights`

, but different rows.
Define `merged`

as follows.

`= flights.merge(more_flights, on = "airline") merged `

Suppose that in `merged`

, there are 108 flights where the
airline is `"United"`

, and in `more_flights`

,
there are 12 flights where the airline is `"United"`

. If
`flights`

has 15 rows in total, how many of these rows are
**not** for `"United"`

flights? Give your answer
as an integer.

**Answer:** 6

The average score on this problem was 64%.

Fill in the blanks below so that `prob`

evaluates to the
probability that a randomly selected flight from the
`flights`

DataFrame arrives at `"SAN"`

given that
it departs from `"LAX"`

.

```
san = flights.get("arrival") == "SAN"
lax = flights.get("departure") == "LAX"
prob = flights[san___(x)___lax].shape[0] / ___(y)___
```

**Answer (x):** `&`

The average score on this problem was 66%.

**Answer (y):** `flights[lax].shape[0]`

The average score on this problem was 62%.

Write an expression that evaluates to the probability that a randomly
selected flight from the `flights`

DataFrame is
**not** on the airline `"Delta"`

.

**Answer:**
`1 - flights[flights.get("airline") == "Delta"].shape[0] / flights.shape[0]`

or
`flights[flights.get("airline") != "Delta"].shape[0] / flights.shape[0]`

or `(flights.get("airline") != "Delta").mean()`

Use the function defined below to answer the questions on the right.

```
def discount(duration, price):
if duration > 6:
price = price * 0.9
if duration > 4:
price = price * 0.95
elif duration > 3:
price = price * 0.98
else:
price = price * 0.99
price = price - 5
return price
```

What is the output of `discount(7, 100)`

?

`100 * 0.9 * 0.95 * 0.98 - 5`

`100 * 0.9 * 0.95 - 5`

`100 * 0.9 - 5`

`100 * 0.9`

**Answer:** Option 2

The average score on this problem was 72%.

Find `x`

and `y`

such that
`discount(x, y)`

evaluates to `97 * 0.98 - 5`

.

**Answer (x):** anything greater than `3`

and
less than or equal to `4`

The average score on this problem was 95%.

**Answer (y):** `97`

The average score on this problem was 95%.