Transcript Lecture 7

CIT 590
More recursion
Filling in some missing pieces
What can we not test?
• Input/output functions are hard to test
• Design your program better for testability
Gather all input from user
Do all the processing (business logic) with several small
functions
Print/render the outputs
Always separate the logic from the input/output portion.
More unit testing resources
• http://www.cis.upenn.edu/~matuszek/cit590-
2013/Pages/unit-testing-in-python.html
• Understand the setUp method. This will also make a lot
more sense once we cover Python classes
• Become familiar with making different types of assertions
• assertTrue
• assertFalse
• The only tests are the functions that begin with the word test.
So you can actually have helper functions as well
Sets
• No repeats allowed
• Conversion between sets and lists is easy
• For loops work ‘out of the box’
• Extremely useful for set intersection/union etc
• Set difference ?
• All those elements in set A which are not in set B
• These DO NOT work by side effect
Questions
• List1 = [1,2,3,4] and list2 = [5,6,7,4]
list3 = list(set(list1).difference(set(list2)))
What happens when we print list3?
a) set([1,2,3])
b) [1,2,3]
c) None
d) [1,2,3,5,6,7]
Questions
• list1 = [1,2,3,4,5]
What is list1[1:][-2:-1][1:]
a) [4,5]
b) [4]
c) [5]
d) []
Style ‘rules’
• Two useful links from Dr Dave
• http://www.cis.upenn.edu/~matuszek/cit590-
2013/Pages/style-rules.html
• http://www.cis.upenn.edu/~matuszek/cit5902013/Pages/programming-hints.html
• Before submitting your assignments, go through a code
review process with your partner.
• Check and see if the style rules are being followed
• Ideally, look at the same large screen
• Skype/Facetime/Hangouts
• Collabedit (collabedit.com)
Recursion revisited
• Fibonacci sequence
0,1,1,2,3,5,8,13,21,34,…
fib(n) = fib(n-1) + fib(n-2)
• The natural recursive formulation becomes really slow!
• Why?
• Rate of growth of the function
Couple of solutions to Fibonacci being
slow
• Iterative formulation
• Dynamic programming
• Store the values that you have already computed in a dictionary
• Reuse them whenever they are asked for
• dynamicFib.py
List recursion examples
• Sum of elements of a list
• Finding max of a list
• Replicate - I want to initialize a list with repeats of a
certain number
All in recursionOnLists.py
Divide and Conquer
• Divide the problem into smaller problem with (usually)
similar structure
• Solve the smaller problems
• Now aggregate/rollup the smaller solutions to get the
solution to the original problem
Classic divide and conquer = mergesort
• Sort a list
• If I divide the list into 2 halves and sort each half does that
help?
• 2 sorted lists can be merged
mergesort
1.#sorting example
2.def mergeSort(a):
3.
#base case/ simple case
4.
if len(a)== 0 or len(a) == 1:
5.
return a
6.
else:
7.
#divide
8.
firstHalf =mergeSort(a[:len(a)/2])
9.
secondHalf=mergeSort(a[len(a)/2:])
10.
#and conquer!
11.
return merge(firstHalf,secondHalf)
Merge
• Making a larger sorted array from 2 smaller sorted ones
• Look at mergeSort.py