Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing recursive methods. We’ve seen a couple of examples Change maker Factorial Categories of problems suited to recursion – natural language processing.

Similar presentations


Presentation on theme: "Writing recursive methods. We’ve seen a couple of examples Change maker Factorial Categories of problems suited to recursion – natural language processing."— Presentation transcript:

1 Writing recursive methods

2 We’ve seen a couple of examples Change maker Factorial Categories of problems suited to recursion – natural language processing a noun phrase is a noun or an adjective followed by a noun phrase. – maze problems – Towers of hanoi – 8 queens – sudoku solver – any kind of “backtracking” problems

3 Analyze the problem Given base and n that are both 1 or more, compute recursively (no loops) the value of base to the n power, so powerN(3, 2) is 9 (3 squared). Rewrite the problem We know that n 1 = n and that n m = n 1 * n 2 * … * n m A recursive definition becomes: f(n, 1) = n f(n, m) = n * f(n, m-1) from codingBat.com

4 Now design the solution f(n, 1) = n// base or easy case f(n, m) = n * f(n, m-1) // refinement public int powerN(int base, int n) { int result; if(n == 1)// easy case result = base; else // recursive case (refinement) result = base * powerN(base, n - 1); return result; }

5 Now another problem Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,... Written as a function F(1) = 1 F(2) = 1 F(n) = F(n – 1) + F(n – 2) + +

6 public static int fibonacci(int num) { if (num == 1 || num == 2) return 1; else return fibonacci(num – 1) + fibonacci(num – 2); }

7 Your Turn CodingBat.com We have a number of bunnies and each bunny has two big floppy ears. We want to compute the total number of ears across all the bunnies recursively (without loops or multiplication). bunnyEars(0) → 0 bunnyEars(1) → 2 bunnyEars(2) → 4

8 Steps How will you know that you are done? This is the easy case. How will you progress toward easy case? This is the refinement case.

9 We have bunnies standing in a line, numbered 1, 2,... The odd bunnies (1, 3,..) have the normal 2 ears. The even bunnies (2, 4,..) we'll say have 3 ears, because they each have a raised foot. Recursively return the number of "ears" in the bunny line 1, 2,... n (without loops or multiplication). bunnyEars2(0) → 0 bunnyEars2(1) → 2 bunnyEars2(2) → 5

10 Steps How will you know that you are done? This is the easy case. How will you progress toward easy case? This is the refinement case.

11 Your turn examples from codingBat.com Given a string and a non-empty substring sub, compute recursively the number of times that sub appears in the string, without the sub strings overlapping. strCount("catcowcat", "cat") → 2 strCount("catcowcat", "cow") → 1 strCount("catcowcat", "dog") → 0

12 Steps How will you identify the substring in the String? How will you know that you are done? This is the easy case. How will you progress toward easy case? This is the refinement case.

13 Given a string, compute recursively a new string where all the 'x' chars have been removed. noX("xaxb") → "ab" noX("abc") → "abc" noX("xx") → “”


Download ppt "Writing recursive methods. We’ve seen a couple of examples Change maker Factorial Categories of problems suited to recursion – natural language processing."

Similar presentations


Ads by Google