CS 124
Fall 2023

Problem Set 2 FAQ

If you don’t see your question here, post it on Piazza or come to TA help hours! See the links in the navigation bar for both of those options.

General questions

  1. 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]]
    
  2. 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., convert_to_inches).

    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.

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

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

  5. I think I’m done with the assignment. Are there any things I should check before I submit?

    1. Be sure all of your functions return values instead of printing them.
    2. Include a docstring in all of your functions.
    3. Be sure to run your code after you add your docstrings to ensure that it still runs. If it doesn’t, you may have introduced problems involving the indentation of your docstrings.
    4. Ensure that there are no print statements in the global scope.

Problem 2

  1. 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 the video called Tracing Function Calls that can be found on Moodle under Class 2.

Problem 3

  1. I’m having trouble figuring out how to write the move_to_end() 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:

    >>> move_to_end('computer', 3)
    'putercom'
    

    In this test case, the function needs to take the string 'computer' and create the string 'putercom'. In order to do so, it needs to extract substrings of 'computer' and concatenate them together.

    • What are the substrings that the function needs to extract in this concrete case?
    • What Python expressions would extract those substrings from the input string s (which in this case is the string 'computer')?
    • How are those expressions related to the value of the second input n (which in this case is 3)?

    By asking like these for this test case and other concrete cases, you should be able to come up with the general expression that you to construct 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.

Problem 4

  1. How do I start implementing is_mirror?

    In the assignment, we hint that computing len(s)//2 may be helpful. What does this value represent? How can you use this value in conjunction with slicing to extract one or more components of the string s that could be useful in the context of this function?

    Our second hint mentions that you might also want to consider calling your previous mirror function. Doing so isn’t required, but if you don’t call it, you will need to replicate at least part of what that other function does in order to determine if s is a “mirrored” string.