### Lecture 28 + HW:

posted Mar 13, 2013, 6:06 AM by Samuel Konstantinovich   [ updated Mar 18, 2013, 1:44 PM ]
 You should play with lists to see what you can do: Here are some examples of how you can use commands to see what they do.List methods and operationsCONCATENATION MAKES A NEW COPY OF THE LIST:>>> [1] + [2]  #add lists works like adding strings, it concatenates them[1, 2]>>> [1] + 2  #you cannot concatenate a list and a non-listTypeError: can only concatenate list (not "int") to list>>> x = [1,2]>>> y = [3,4]>>> print x+y  #concatenation or slicing always make new copies[1, 2, 3, 4]>>> print x  #so you should notice x and y don't change when you write x+y[1, 2]>>> print y[3, 4]>>> z = x + y #you can store changes by assigning them to any variable (including x and y)>>> print z[1, 2, 3, 4]SLICING MAKES A NEW COPY OF THE LIST:>>>print x[2:][3, 4]>>>print x  #x isn't changed[1, 2, 3, 4]List methods can modify a list:>>> x.append(6)    #append puts a value into the list at the end of the list >>> print x   [1, 2, 6]>>> print y[3, 4]>>> x.extend(y)  #extend is similar to concatenating, but it changes the list>>> print x  #notice that i didn't say x=something, and x changed.[1, 2, 6, 3, 4]>>>  Good Coding Practices:Some alternate ways to solve each of the problems below:1:  find(part,whole)   How do we search for a string in another string?2a: Fib(n)     How can we do this recursively?2b: FibList(n) How can we do this using fib, and better yet, how can we do this without using a separate function?CRITICAL QUESTION: If we write FibList(n) without fib(n) then how can we use FibList(n) to solve fib(n)?Homework for you:BRING A PRINTED PAPER COPY WITH HEADER!3. Write a function moveNegativeToEnd(L) that takes a list as a parameter. It moves all negative values to the end of the list in order. Precondition is that all values in L are real numbers. You can use any list methods you find, as well as slices. I strongly urge you to look at the methods!x=[ 1, 3, -5, 10, -2, 0, -6]moveNegativeToEnd(x)  changes x to [ 1, 3, 10, 0, -5, -2, -6]y=[0, 1 , -3, 4]moveNegativeToEnd(y)  changes y to  [ 0, 1, 4, -3]note that this does not return a copy! It changes the list. #Solutions to moveNegativeToEnd:def moveNegativeToEnd(L):    t=[]    i=0    while i