← return to practice.dsc10.com
Below are practice problems tagged for Lecture 3 (rendered directly from the original exam/quiz sources).
Consider the following four assignment statements.
bass = "5"
tuna = 2
sword = ["4.0", 5, 12.5, -10, "2023"]
gold = [4, "6", "CSE", "doc"]What is the value of the expression bass * tuna?
Answer: "55"
The average score on this problem was 48%.
Which of the following expressions results in an error?
int(sword[0])
float(sword[1])
int(sword[2])
int(sword[3])
float(sword[4])
Answer: int(sword[0])
The average score on this problem was 51%.
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]))
Answer: gold[3].replace("o", "s").upper() + str(gold[0] + int(gold[1]))
The average score on this problem was 92%.
Consider the following assignment statement.
puffin = np.array([5, 9, 13, 17, 21])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 like penguin = np.arange(5, 25, 4) would work.
The average score on this problem was 90%.
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: 2y: anything in (8, 10]z: 5
The average score on this problem was 74%.
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()
The average score on this problem was 59%.
Define narwhal and jellyfish as follows.
narwhal = np.arange(6, 12, 3)
jellyfish = np.arange(12, 6, -3)
True or False: narwhal.sum() and jellyfish.sum() evaluate to the same value.
True
False
Answer: False
The average score on this problem was 86%.
True or False: (narwhal+jellyfish)[0] and (narwhal+jellyfish)[1] evaluate to the same value.
True
False
Answer: True
The average score on this problem was 92%.
Alongside the unveiling of your new game, you’ll want to announce a new fighter to build hype. The code below calculates the name of this new fighter and stores it in the variable new. In the box, write in the value of the new variable after the code executes. Write clearly!
lu = "luigi"
dk = "dk"
new = "crash"
dk.replace("k", "kong")
new = ((lu + "t") * len(dk)).title()
new = new
Answer: "Luigitluigit"
The average score on this problem was 44%.
Coleman purchased a property on Clougherty Drive, but the street number can only be determined by evaluating the following expression. What is the street number?
sum(2 ** np.arange(3, 7, 2)) * len(str(len("sushi" * 999)))Answer: 160
The average score on this problem was 48%.
Ella and Bianca, a duo of world renowned pizza-turned-sushi chefs, are interviewing jointly for a job at Coleman’s Catch. Help them ace their interview question by determining the output of the code block below. Write legibly!
food1 = "hamachi"
food2 = "sashimi"
food2.replace("sh", "iba").replace("s", "b") + "o".join(food1.split("a"))Answer: "baibaimihomochi"
The average score on this problem was 58%.
The management of the Solazzo apartment complex is changing the comple’s name to be the output of the following line of code. Write the new name of this complex as a string.
Note that the string method .capitalize() converts the first character of a string to uppercase.
("Solazzo".replace("z", "ala" * 2)
.split("aa")[-1]
.capitalize()
.replace("o", "Jo"))Answer: “LaJo”
Let’s trace the steps:
We start with the original string: “Solazzo”.
"Solazzo".replace("z", "ala" * 2)
Replace every instance of “z” with “alaala” since “ala” * 2 = “alaala”: “Solaalaalaalaalao”
"Solaalaalaalaalao".split("aa")
Split the string by “aa”: [“Sol”, “l”, “l”, “l”, “lao”]
["Sol", "l", "l", "l", "lao"][-1]
Get the last element of the list: “lao”
"lao".capitalize()
Uppercase the first character of the string: “Lao”
"Lao".replace("o", "Jo")
Replace every instance of “o” with “Jo”: “LaJo”
The average score on this problem was 76%.
The management fo the Renaissance apartment complex has decided to follow suit and rename their complex to be the output of the following line of code. Write the new name of this complex as a string.
(("Renaissance".split("n")[1] + "e") * 2).replace("a", "M")Answer: “MissMeMissMe”
Let’s trace the steps:
We start with the original string: “Renaissance”.
"Renaissance".split("n")
Split the string by “n”: [“Re”, “aissa”, “ce”]
["Re", "aissa", "ce"][1]
Get the element in the 1st index of the list (the second element in the list): “aissa”
"aissa" + e
Add an “e” to the end of the string: “aissae”
("aissae") * 2
Repeat the string twice: “aissaeaissae”
"aissaeaissae".replace("a", "M")
Replace every instance of “a” with “M”: “MissMeMissMe”
The average score on this problem was 70%.
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
What is the value of poppy after this code is executed?
Answer: 21.0
The average score on this problem was 54%.
What is the result of the expression daisy + lily?
array([8, 1, 5, 4, 2])
array([12, 3, 5])
array([12, 3])
This expression errors.
Answer: Option D
The average score on this problem was 68%.
What is the result of the expression daisy + lily[0]?
array([8, 1, 5, 4])
array([12, 5, 9])
array([10, 3, 7])
This expression errors.
Answer: Option B
The average score on this problem was 79%.
Below is the opening line from the legendary Minecraft parody song
Revenge:
line = "Creeper aww man"You want to create an echo effect to produce the following string:
"Creeper Creeper Creeper aww man"Which of the following code snippets produces this output? Select all that apply.
words = line.split(" ") (words[0] + " ") * 3 + words[1] + " " + words[2]
line * 3
line.replace("Creeper", "Creeper Creeper Creeper")
words = line.split(" ") " ".join(words[0] * 3 + words[1])
None of the above.
Answer:
Option 1 and Option 3
The average score on this problem was 93%.
One way to use np.arange to produce the sequence [2, 6, 10, 14] is np.arange(2, 15, 4). This gives three inputs to np.arange.
Fill in the blanks below to show a different way to produce the same sequence, this time using only one input to np.arange. Each blank below must be filled in with a single number only, and the final result, x*np.arange(y)+z, must produce the sequence [2, 6, 10, 14].
Using x*np.arange(y)+z fill in the missing values:
x = _
y = _
z = _
Answer:
x = 4, y = 4, z = 2
The question states that we are trying to create the sequence [2, 6, 10, 14] by filling in the blanks for the statement x*np.arange(y)+z. If we look at the sequence we are attempting to derive, we can see that each step in the sequence increments by 4. Similarly, we can see that the sequence begins at 2. We know that passing an argument by itself in np.arange will increment up to that number (for example np.arange(4) will produce the sequence [0,1,2,3]). Knowing this, we can create this sequence by setting y to 4. Attempting to reach the desired sequence of [2, 6, 10, 14] from [0, 1, 2, 3] we can multiply each number by 4 by setting x to 4 and instantiate the sequence at 2 by setting z as 2.
The average score on this problem was 96%.
Consider the assignment statement below.
pear = [6, 10.1, "pear", 13]
What does the expression np.array(pear) evaluate to?
array([6, 10.1, "pear", 13])
array([6, 10.1, pear, 13])
array(["6", "10.1", "pear", "13"])
array(["pear"])
array([pear])
This expression errors
Answer: array(["6", "10.1", "pear", "13"])
The average score on this problem was 50%.
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.
Using array methods, write an expression that evaluates to the spread of peach.
Answer: peach.max() - peach.min()
The average score on this problem was 62%.
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]
The average score on this problem was 36%.
Choose the correct way to fill in the blank in this sentence:
The spread of peach is ______ the value of y - x.
always less than
sometimes less than and sometimes equal to
always greater than
sometimes greater than and sometimes equal to
always equal to
Answer: always less than
The average score on this problem was 48%.
How many unique data types exist in the clubs DataFrame?
2
3
4
5
6
None of these.
Answer: 4
The average score on this problem was 61%.
Suppose we run the following code.
clubs_and_ratings = ["Cheese Club".upper(), str(int(4.6)), "Painting Club".title(), 3.7 - 1]
a = np.array(clubs_and_ratings)What do a[0], a[1], a[2], and a[3] evaluate to?
Answer:
a[0]: "CHEESE CLUB"
a[1]: "4"
a[2]: "Painting Club"
a[3]: "2.7"
The average score on this problem was 75%.
The Data Science Student Society has a $500 budget, which they will use only to buy food for their 4 meetings this quarter. Each time they have a meeting, they will use exactly half of their remaining budget on food. For example, the first meeting’s food cost will be $250 and the next meeting’s food cost will be $125. Use np.arange(), together with arithmetic operations, to assign food_costs to an array containing the food cost for each of the 4 meetings. Do not use np.array().
food_costs =
Answer: 500 * 0.5 ** np.arange(1,5) or 500/2 ** np.arange(1,5)
The average score on this problem was 11%.
For this problem, let weight and age be defined by:
weight = np.array(zoo.get('weight_lb'))
age = np.array(zoo.get('age'))(a) Avi wants to quickly estimate each animal’s weight in kilograms instead of pounds using the shortcut ( ).
Which of the following expressions output an array of the animal weights in kilograms using this shortcut? Select all that apply.
A. weight * 0.45
B. weight - 0.55
C. weight * 0.90 / 2
D. weight / 0.45
Answer: A and C
weight * 0.90 / 2 equals 0.45 * weight.(b) Michelle wants to find the range of age, i.e., the age of the oldest zoo animal minus the age of the youngest zoo animal.
Which of the following expressions incorrectly computes this value?
A. age.max() - age.min()
B. (age - age.max()).min()
C. (age.max() - age).max()
D. (age - age.min()).max()
Answer: B
age - age.max() is always non-positive, so .min() gives the negative of the range, not the range.
Pizza by Peter awards loyalty points: every third slice you purchase from them earns 2^i points, starting from the 1st slice. For example, the 1st, 4th, 7th and 10th slices that a customer purchases each earn 2^i loyalty points where i is 1, 4, 7 and 10 respectively.
Without using the + operator, write a one-line expression that evaluates to the total loyalty points from those four slices.
Answer: (2**(np.arange(1, 12, 3))).sum()
The 1st, 4th, 7th, and 10th slices earn 2^1, 2^4, 2^7, and 2^{10} points. Exponents 1, 4, 7, 10 are every third integer starting at 1: np.arange(1, 12, 3).
Explanation:
np.arange(1, 12, 3) gives [1, 4, 7, 10]; 2** raises 2 to each power; .sum() adds them (without using binary + in your source).
Alternatives: np.sum(np.array([2**1, 2**4, 2**7, 2**10])), or (2**(np.arange(0, 10, 3) + 1)).sum(), etc.
The average score on this problem was 58%.
Your supervisor asks you to create a welcome message for new students. The code below generates this message and stores it in the variable message. In the box, write in the value of the message variable after the code executes. Write clearly!
school = "ucsd"
year = "2026"
message = "hello"
message = school.upper() + " " + year
message = message.replace("SD", " San Diego")Answer: "UC San Diego 2026"
Trace through each line:
school = "ucsd", year = "2026", message = "hello".message = school.upper() + " " + year. school.upper() is "UCSD". Concatenating gives "UCSD" + " " + "2026" → "UCSD 2026".message.replace("SD", " San Diego") replaces the substring "SD" inside "UCSD 2026" with " San Diego" (note the leading space in the replacement string), yielding "UC San Diego 2026".
The average score on this problem was 86%.