Fall 2023 Quiz 2

← 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 5-10 of the Fall 2023 offering of DSC 10.

⚠️ PDF version available here .


The DataFrame items describes various items available to collect or purchase using bells, the currency used in the game Animal Crossing: New Horizons.

For each item, we have:

The first 6 rows of items are below, though items has more rows than are shown here.


Problem 1

Lecture 5

Fill in the blanks so that count_1 and count_2 both evaluate to the number of items in items with a "Cost" of 0.

count_1 = items.groupby(__(a)__).__(b)__().get("Item").loc[__(c)__]
count_2 = items[__(d)__].shape[0]

Answer:

  • a: "Cost"
  • b: count
  • c: 0
  • d: items.get("Cost") == 0

count_1:

items.groupby("Cost").count() creates one row per distinct cost, with the cost as the index. Each remaining column holds the number of items at that cost. We select the “Item” column with .get("Item"), and .loc[0] pulls out the entry whose index label is 0, which is the number of items costing 0 bells.

count_2:

We query the items DataFrame with the Boolean Series items.get("Cost") == 0, to keep only the rows with a “Cost” of 0 bells. Then, we do .shape[0] to count the number of rows.


Difficulty: ⭐️⭐️

The average score on this problem was 81%.


Problem 2

Lecture 9

The DataFrame keepers has 5 rows, each of which represents a different shopkeeper in the Animal Crossing: New Horizons universe.

keepers is shown below in its entirety.


How many rows are in the following DataFrame? Give your answer as an integer.

keepers.merge(items.take(np.arange(6)), 
              left_on="Store", 
              right_on="Location")

Answer: 10

This code merges keepers (left) with items.take(np.arange(6)) (right), which is the first 6 rows of items. A row from keepers and a row from items are combined whenever the "Store" value in keepers is the same as the "Location" value in items.

keepers has:

  • 1 row of “Able Sisters Shop”
  • 3 rows of “Nook’s Cranny”
  • 1 row of “Kicks Shoe Store”

The first 6 rows of items have:

  • 1 row of “Able Sisters Shop”
  • 3 rows of “Nook’s Cranny”
  • 1 row of “Crafting”
  • 1 row of “Birthday Gift”

For each value that appears in both DataFrames, we multiply the number of matching rows in each, then add up the products:

  • “Able Sisters Shop”: 1 \times 1 = 1 row
  • “Nook’s Cranny”: 3 \times 3 = 9 rows

“Kicks Shoe Store”, “Crafting” and “Birthday Gift” each appear in only one DataFrame, so they produce no rows.

1 + 9 = \boxed{10} \text{ rows}


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 54%.


Problem 3

Lecture 6

Which type of plot should we use to visualize the distribution of the "Location" column in the items DataFrame?

Answer: Bar chart

“Location” is a categorical variable, since each value is the name of a store or action. The distribution of a categorical variable is shown with a bar chart (one bar per category).


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 74%.


Problem 4

Lecture 7

Nintendo collected data on the heights of a sample of Animal Crossing: New Horizons players. A histogram of the heights in their sample is given below.

What percentage of players in Nintendo’s sample are at least 62.5 inches tall? Give your answer as an integer rounded to the nearest multiple of 5.

Answer: 80%

The total area of a density histogram is equal to 1. The area of each bar is the proportion of data within that bin. The percentage of players at least 62.5 inches tall is the total area of all bars at 62.5 inches and to the right.

For each bar,

\text{area} = \text{width} \times \text{height}.

One method is to find the area of all bars at 62.5 and to the right. However, we can use a shortcut: find the area of the bars to the left of 62.5 and subtract it from the total area of 1:

1 - (0.02 \times 2.5 + 0.06 \times 2.5) = 1 - 0.2 = 0.8 = 80\%.

Therefore, about 80% of players are at least 62.5 inches tall.


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 73%.


Problem 5

Lecture 10

Consider the function tom_nook, defined below. Recall that if x is an integer, x % 2 is 0 if x is even and 1 if x is odd.

def tom_nook(crossing):
    bells = 0
    for nook in np.arange(crossing):
        if nook % 2 == 0:
            bells = bells + 1
        else:
            bells = bells - 2
    return bells

What value does tom_nook(8) evaluate to?

Answer: -4

Calling tom_nook(8) passes 8 into crossing, so the loop runs over np.arange(8), which is 0, 1, 2, 3, 4, 5, 6, and 7. We initialize the variable bells as 0.

nook % 2 is the remainder when nook is divided by 2, so it equals 0 for even numbers and 1 for odd numbers. That means:

If nook is even, bells goes up by 1. There are 4 evens (0, 2, 4, and 6), for a total of +4.

If nook is odd, bells goes down by 2. There are 4 odds (1, 3, 5, and 7), for a total of 4 \times (-2) = -8.

0 + 4 - 8 = -4

Therefore, when we return bells at the end of the function, it outputs a value of \boxed{-4}.


Difficulty: ⭐️⭐️

The average score on this problem was 79%.


👋 Feedback: Find an error? Still confused? Have a suggestion? Let us know here.