### 2014-03-24 + HW

posted Mar 24, 2014, 5:12 AM by Samuel Konstantinovich   [ updated Mar 24, 2014, 8:17 AM ]
 Do Now: OPEN YOUR HOMEWORK IN PYTHON (All of the Methods you worked on Friday-Sunday.)Discuss and write down:1. find a list in a bigger list vs find a string in a bigger string. How would they be different? How would they be the same?2. After you finish opening your homework: look for a different way of doing find(part,whole) ... look at your neighbors code and write down a DIFFERENT way on paper. Write down what the main difference is. DO NOT Write down the variables are different, i want to know why the algorithm is different!3. Test on Thursday vs Quiz Thursday Quiz Friday vote on piazza.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)?GOAL: List methods and operationsHere are some examples of how you can use commands to see what they do.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-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 existing list>>> print x  #notice that i didn't say x=something, and x changed.[1, 2, 6, 3, 4]>>>   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