SPIS Today More Recursion Todays goals Trace recursive

  • Slides: 22
Download presentation
SPIS Today: More Recursion

SPIS Today: More Recursion

Today’s goals • • • Trace recursive functions Identify errors in a function Identify

Today’s goals • • • Trace recursive functions Identify errors in a function Identify the base can and recursive step in a function Describe the high-level behavior of a recursive function Write simple recursive functions Write recursive algorithms and functions using the use-it-orlose-it strategy

Powerhouse concept: assignment vs. equality & Boolean values as data What does the following

Powerhouse concept: assignment vs. equality & Boolean values as data What does the following Python statement do, assuming that a has previously been assigned a Boolean value: a = (a == False) A. B. C. D. E. It always changes the value of a It always sets a to 0 (False) It always sets the value of a to 1 (True) It always causes an error Something else

More code tracing… Consider the following code: def mystery( x, y ): if x

More code tracing… Consider the following code: def mystery( x, y ): if x < 0 : 1 return false elif x == 0: 2 return true else: 3 mystery( x-y, y ) Which line(s) represents the base case(s)? A. 1 B. 2 C. 3 D. 1&2 (Also mark the recursive step) E. 2&3

More code tracing… Consider the following code: def mystery( x, y ): if x

More code tracing… Consider the following code: def mystery( x, y ): if x < 0 : return false elif x == 0: return true else: mystery( x-y, y ) >>> mystery( 9, 5 ) What is the value of calling mystery(9, 5)? A. 1 (i. e. , true) B. 0 (i. e, false) C. -1 D. None

More code tracing… Consider the following code: def mystery( x, y ): if x

More code tracing… Consider the following code: def mystery( x, y ): if x < 0 : return false elif x == 0: return true else: return mystery( x-y, y ) >>> mystery( 9, 5 ) NOW, what is the value of calling mystery(9, 5)? A. 1 (i. e. , true) B. 0 (i. e, false) C. -1 D. None

More code tracing… Consider the following code: def mystery( x, y ): if x

More code tracing… Consider the following code: def mystery( x, y ): if x < 0 : return false elif x == 0: return true else: return mystery( x-y, y ) >>> mystery( 9, 5 ) Describe as concisely as possible what the mystery function does, in general. First write down your answer, then discuss. Separately, describe how it does this.

Reasoning about recursive functions def mystery( x, y ): if x < 0: return

Reasoning about recursive functions def mystery( x, y ): if x < 0: return false elif x == 0: return true else: return mystery( x-y, y ) def mystery 2( x, y ): if x < 0: return false if x == 0: return true return mystery 2( x-y, y ) Do the two methods above do the same thing for all input? A. Yes B. No

Reasoning about recursive functions def mystery( x, y ): if x < 0: return

Reasoning about recursive functions def mystery( x, y ): if x < 0: return false elif x == 0: return true else: return mystery( x-y, y ) def mystery 2( x, y ): if x <= 0: return x == 0 else: return mystery 2( x-y, y ) Do the two methods above do the same thing for all input? A. Yes B. No

Writing recursive functions Write a method named int. Divide(x, y) that it returns the

Writing recursive functions Write a method named int. Divide(x, y) that it returns the result of x / y, without the remainder (you should assume x and y are ints). DO NOT USE / You will probably find mystery to be a helpful guide. def int. Divide( x, y ): if else: # base case # recursive step

Writing recursive functions Write a function named int. Mod(x, y) that returns the result

Writing recursive functions Write a function named int. Mod(x, y) that returns the result of x % y (assuming x and y are integers) – i. e. the remainder when x is divided by y. DO NOT USE % def int. Mod( x, y ):

Power Set! >>> powerset([1, 2]) [[], [2], [1, 2]] This really demonstrates the power

Power Set! >>> powerset([1, 2]) [[], [2], [1, 2]] This really demonstrates the power of functional programming! >>> powerset([1, 2, 3]) [[], [3], [2, 3], [1, 2, 3]] The order in which the subsets >>> powerset([1]) >>> powerset([]) are presented is unimportant but within each subset, the order should be consistent with the input set. We’ll leave this as a challenge for you in lab…

Lists: indexing and slicing Each element in a list as an index. Index locations

Lists: indexing and slicing Each element in a list as an index. Index locations always start at 0. [ 1, 2, 3, 4] 0 1 2 3 [ [1, 2], 3, 4] 0 1 2 Note that elements may be lists! To extract a single element from a list, use the index operator: >>> L = [1, 2, 3, 4] >>> L[0] 1 >>> L[3] 4 >>> L[4] ERROR!! >>> L 1 = [[1, 2], 3] >>> L 1[0] [1, 2] >>> L 1[2] ERROR!!

Lists: indexing and slicing Each element in a list as an index. Index locations

Lists: indexing and slicing Each element in a list as an index. Index locations always start at 0. [ 1, 2, 3, 4] 0 1 2 3 [ [1, 2], 3, 4] 0 1 2 Note that elements may be lists! To extract multiple elements from a list, as a list, use slicing: >>> L = [1, 2, 3, 4] >>> L[1: ] [2, 3, 4] >>> L[1: 3] [2, 3] >>> L[3: ] [4] # NOTE THE DIFFERENCE FROM L[3]!!

Lists: appending The + operator will append one list to another (i. e. smush

Lists: appending The + operator will append one list to another (i. e. smush them together) >>> L = [1, 2, 3, 4] >>> L + [5, 6] [1, 2, 3, 4, 5, 6] >>> L + 7 ERROR!! >>> L + [[42]] What is the value shown here? A. B. C. D. E. [1, 2, 3, 4, 5, 6, 42] [1, 2, 3, 4, 5, 6, [42]] [1, 2, 3, 4, 42] [1, 2, 3, 4, [42]] ERROR Lists are not our main focus right now, But you can read more in the book.

Use-It-Or-Lose-It

Use-It-Or-Lose-It

The Knapsack Problem… Kingdom of Shmorbodia Item Weight Value Spam Tofu Chocolate 2 3

The Knapsack Problem… Kingdom of Shmorbodia Item Weight Value Spam Tofu Chocolate 2 3 4 100 112 125 Knapsack Capacity: 5? 6? 7? >>> knapsack(7, [ [2, 100], [3, 112], [4, 125] ]) 237 Prof. I. Lai thinks that a “greedy solution” is the way to go! Prove why a greedy solution will not work.

The Knapsack Problem… Kingdom of Shmorbodia Item Weight Value Spam Tofu Chocolate 2 3

The Knapsack Problem… Kingdom of Shmorbodia Item Weight Value Spam Tofu Chocolate 2 3 4 100 112 125 Knapsack Capacity: 5? 6? 7? Use it or lose it: • Pick one element to be “it” (The first item in the knapsack) • Compute the solution using “it”, then recompute the solution without “it” • Choose whichever option is better (More value) • Base case(s)? ? Let’s try it!

The Knapsack Revisited… Kingdom of Shmorbodia Item Weight Value Spam Tofu Chocolate 2 3

The Knapsack Revisited… Kingdom of Shmorbodia Item Weight Value Spam Tofu Chocolate 2 3 4 100 112 125 Knapsack Capacity: 5? 6? 7? >>> knapsack(7, [ [2, 100], [3, 112], [4, 125] ]) [237, [ [3, 112], [4, 125] ] ] Modify the knapsack code so that it also returns the list of items chosen

Comparing DNA via Longest Common Subsequence (LCS) AGGACAT ATTACGAT >>> LCS(“AGGACAT”, “ATTACGAT”) 5 >>>

Comparing DNA via Longest Common Subsequence (LCS) AGGACAT ATTACGAT >>> LCS(“AGGACAT”, “ATTACGAT”) 5 >>> LCS(“spam”, “sam!”) I prefer spam 3 to an xsam! >>> LCS(“spam”, “xsam”) 3

Recursive Approach… def LCS(S 1, S 2): if BASE CASE else: LCS(“spam”, “sam!”) Try

Recursive Approach… def LCS(S 1, S 2): if BASE CASE else: LCS(“spam”, “sam!”) Try this in your notes!

Power Set! >>> powerset([1, 2]) [[], [2], [1, 2]] This really demonstrates the power

Power Set! >>> powerset([1, 2]) [[], [2], [1, 2]] This really demonstrates the power of functional programming! >>> powerset([1, 2, 3]) [[], [3], [2, 3], [1, 2, 3]] The order in which the subsets >>> powerset([1]) >>> powerset([]) are presented is unimportant but within each subset, the order should be consistent with the input set. We’ll leave this as a challenge for you in lab…