Discussion 2: Basic Python and Arrays

← 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.


Problem 1

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.


Problem 1.1

What does type(prices[0]) evaluate to?

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.


Problem 1.2

What does type(calories[0]) evaluate to?

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.


Problem 1.3

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).


Problem 1.4

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).



Problem 2

Consider the following four assignment statements.

bass = "5"
tuna = 2
sword = ["4.0", 5, 12.5, -10, "2023"]
gold = [4, "6", "CSE", "doc"]


Problem 2.1

What is the value of the expression bass * tuna?

Answer: "55"


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 48%.


Problem 2.2

Which of the following expressions results in an error?

Answer: int(sword[0])


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 51%.


Problem 2.3

Which of the following expressions evaluates to "DSC10"?

Answer: gold[3].replace("o", "s").upper() + str(gold[0] + int(gold[1]))


Difficulty: ⭐️

The average score on this problem was 92%.



Problem 3

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.


Problem 4

Consider the following assignment statement.

puffin = np.array([5, 9, 13, 17, 21])


Problem 4.1

Provide arguments to call np.arange with so that the array penguin is identical to the array puffin.

penguin = np.arange(____)

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.


Difficulty: ⭐️

The average score on this problem was 90%.


Problem 4.2

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.

parrot = __(x)__ * np.arange(0, __(y)__, 2) + __(z)__

Answer:

  • x: 2
  • y: anything in (8, 10]
  • z: 5

Difficulty: ⭐️⭐️⭐️

The average score on this problem was 74%.



Problem 5

Suppose x and y are both ints 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.


Problem 5.1

Using array methods, write an expression that evaluates to the spread of peach.

Answer: peach.max() - peach.min()


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 62%.


Problem 5.2

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]


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 36%.


Problem 5.3

Choose the correct way to fill in the blank in this sentence:

The spread of peach is ______ the value of y - x.

Answer: always less than


Difficulty: ⭐️⭐️⭐️⭐️

The average score on this problem was 48%.



Problem 6

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


Problem 6.1

What is the value of poppy after this code is executed?

Answer: 21.0


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 54%.


Problem 6.2

What is the result of the expression daisy + lily?

Answer: Option D


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 68%.


Problem 6.3

What is the result of the expression daisy + lily[0]?

Answer: Option B


Difficulty: ⭐️⭐️

The average score on this problem was 79%.



Problem 7

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()


Difficulty: ⭐️⭐️⭐️

The average score on this problem was 59%.


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