Old version
This is the CS 111 site as it appeared on December 20, 2022.
If you don’t see your question here, post it on Piazza or come to office hours! See the links in the navigation bar for both of those options.
I get an error message that mentions a TypeError
when I add
something to a list. What should I do?
If you get something like the following error.
TypeError: can only concatenate list (not "int") to list
you are likely trying to add an integer to a list.
Suppose you have two lists called x
and y
.
x = [0, 1, 2] y = [3, 4, 5]
And you would like to make a list that contains [0, 1, 2, 3]
.
You might try doing the following:
x + y[0]
Unfortunately, this is not allowed in Python, because addition
between a list and an integer is not defined. If you think about
it, this makes sense: how would you add a list to an integer?
Instead, recall that the +
operator can be used to concatenate
two lists. Knowing this, we can put y[0]
inside its own list
and concatenate that with the list x
.
x + [y[0]]
Do our functions need to have the exact names that you have specified in the assignment? What about the variables for the inputs?
Your functions must have the exact names that we have
specified, or we won’t be able to test them. Note in particular
that the case of the letters matters, and that some of the
function names have one or more underscore characters (e.g.,
larger_gap
).
For the variables specified for the inputs, you could in theory use different names, as long as they are still as descriptive as the original ones. However, when there are multiple inputs, you must keep the inputs in the same order that we specified in the assignment, or we won’t be able to test your function.
I’ve read about or used some Python functions that we have not discussed in lecture. Can I use them in the homework?
No. You should solve the problems in the homework using only constructs that we have discussed in lecture or that you have seen in the textbook. In addition, some of the problems have specific instructions regarding which functions you should or should not use. There are, of course, many different ways to accomplish the homework assignments, but we construct the assignments so that you can solve them using the tools that we have learned about in class.
As a reminder, you are prohibited from consulting with external sites such as Stack Overflow about the homework assignments.
Do we need to use recursion on this assignment?
No. None of the functions that we’re asking you to write for this assignment require recursion. If you wrote one or more of them using recursion, that’s okay, too, but doing so is not required.
I think I’m done with the assignment. Are there any things I should check before I submit?
I’m not sure exactly what I need to do when tracing functions. Do you have any suggestions?
We covered a similar tracing problem in lecture. That steps taken to solve that problem can also be seen in the pre-lecture video called Tracing Function Calls that can be found in the Blackboard folder for September 21’s lecture.
What should begins_with
return if the specified prefix
is
a substring of the specified word
, but isn’t actually
at the beginning of the word?
It should return False
. The function should only return True
if
the specified word
begins with the specified prefix
. For
example:
>>> begins_with('compute', 'put') result: False >>> begins_with('compute', 'com') result: True
Does begins_with
need to handle the case in which prefix
is
longer than word
?
Yes, and we’ve added an example for this case to the problem description:
>>> begins_with('begin', 'bigger') result: False
I’m having trouble figuring out how to write
the begins_with()
function. Do you have any hints?
We encourage you to start with some concrete cases. For example, we’ve given you the following test case:
>>> begins_with('computer', 'come') result: False
In this test case, the function needs to determine if the string
'come'
appears at the very beginning of the string 'computer'
.
In order to do so, it needs to extract the appropriate slice/substring
of 'computer'
and compare it to the string 'come'
.
s
(which in this case is the string
'computer'
)? prefix
(which in this case is the string 'come'
)?
(Note: There’s a hint in the problem description that may
help here!)By asking questions like these for this test case and other concrete cases, you should be able to come up with the general logic that you can use to determine the necessary return value.
I’m having trouble figuring out how to write
the replace_start()
function. Do you have any hints?
Here again, we encourage you to start with some concrete cases. For example, we give you the following test case:
>>> replace_start([1, 2, 3, 4, 5], [10, 11]) result: [10, 11, 3, 4, 5]
In this test case, the function needs to take the lists [1, 2, 3,
4, 5]
and [10, 11]
and create the list [10, 11, 3, 4, 5]
. In
order to do so, it needs to extract a slice of the first list
and concatenate it to the second list.
values
(which in this case is the list
[1, 2, 3, 4, 5]
)? new_start_vals
(which in this case is [10, 11]
)? By asking questions like these for this test case and other concrete cases, you should be able to come up with the general logic that you can use to determine the necessary return value. Also, don’t forget to handle the special case mentioned in the problem, which we recommend that you test for and handle first.
How can I pad a string in Python?
Remember that you can concatenate strings with the +
operator just
like you can concatenate lists. Therefore, if you have a variable
called pad
that represents the necessary padding, you can just
concatenate it to the beginning of the string.
To create the necessary padding, recall that you can repeat a string
by using the *
operator.
For example, if I wanted a string consisting of five a
‘s, I could
do the following:
'a' * 5
Last updated on January 6, 2023.