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 operations

CONCATENATION 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-list
TypeError: 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<len(L):
        if L[i]<0:
            t.append(L.pop(i))
        else:
            i+=1
    L.extend(t)

def moveNegativeToEnd(L):
    moves=0
    i=0
    while i<len(L)-moves:
        if L[i]<0:
            L.append(L.pop(i))
            moves+=1
        else:
            i+=1

#example how to use:
X=[1,-2,-3,4,9,0,-5]
moveNegativeToEnd(X)
print X


CODE EXAMPLES FROM CLASS:

#1find a substring in a string
#1a.using a while loop
def find(part,whole):
        i=0
        while i< len(whole-len(part)):
            if whole[i:i+len(part)] == part:
                return i
            i+=1
        return -1 #default to -1 when the loop ends

#1b. with a python twist 
def find(part,whole):
    if not part in whole:
        return -1
    else:
        i=0
        while True: #this will stop when the return 
             if whole[i:i+len(part)] == part:
                return i
            i+=1

#1c. recursively (and VERY inefficiently)    
def find(part,whole):
    if not part in whole:
        return -1
    elif whole[:len(part)] == part:
        return 0
    else:
        return 1 + find(part,whole[1:])

#2a fib using recusion ( exponential growth!)
def fib(n):
    if n==0:
        return 0
    elif n==1:
        return 1
    else:
        return fib(n-1)+fib(n-2)


#2b fibList using the recursive fib function
def fibList(n):
    ans=[]
    i=0
    while i<=n:
        ans.append(fib(i))
        i+=1
    return ans

#2bII fibList using a list's last two elements
def fibList(n):
    if n==0:
        return [0]
    elif n==1:
        return [0,1]
    else:
        i=0
        ans = [0,1]
        while i<n-1:
            ans.append(  ans[-1]+ans[-2])
            i+=1
        return ans

#2aIIfib using the fibList function
def fib(n):
    return fibList(n)[-1]
Comments