← return to practice.dsc10.com
Below are practice problems tagged for Lecture 3 (rendered directly from the original exam/quiz sources).
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%.
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%.