Warmup

Part A: String Editor

In stringedit.py, you will find a few functions that could be helpful if you wanted to do fun things with strings. Whoever wrote them even documented them well, with an example input and desired output! However, none of the functions are implemented correctly; each one has a bug that causes it to break. For each of the functions, do the following with your partner.

  1. Look at the code without running it, and try to guess what the code will actually do on the given test case.
  2. Run the code on the test case, and try to figure out what mistake the programmer made. Can you figure out what they were thinking?
  3. Figure out how to correctly implement the function.

For step 2, you should test your functions by adding code to the main function at the bottom of your stringedit.py (below the function definitions).

Part B: 2D List Editor

One way to think about a list of lists is as a table. In particular, if you have a list of m inner lists, each of length n, this is like a table with m rows and n columns. Each of the inner lists is a row. For example: [[1,2,3,4],[5,6,7,8],[9,10,11,12]] can be thought of like this:

1  2  3  4

5  6  7  8

9 10 11 12

In tableedit.py, you will find a few functions that manipulate 2D lists of numbers by editing their entries or gluing them together. Again, the functions are broken. Throughout the code in tableedit.py, assume that all 2D lists given as input have the same number of rows as columns. For each function, again do steps 1-3 from the previous part, with your partner. There is also a main function to test your code at the bottom of the file. See a pattern? Every file/module will probably have a main function from ths point on!

Append vs Extend

There are two common methods for adding elements to a list—append() and extend(). These methods are similar, but they are not identical. append() adds a single new entry to a list; whereas extend() adds all of the elements of one list onto the end of another list. That said, both methods directly modify the string they are called from. Read the descriptions below for more details.

.append():

Given a list mylist and a number n, mylist.append(n) adds n as a new entry to mylist. The append() method is said to modify mylist in-place. This means that append() does not return anything! Instead it directly changes mylist.

.extend():

Given two lists, mylist and yourlist, mylist.extend(yourlist) adds the elements of yourlist to the end of mylist. Again, extend() modifies mylist in-place.