Presentation is loading. Please wait.

Presentation is loading. Please wait.

Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what.

Similar presentations


Presentation on theme: "Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what."— Presentation transcript:

1 Let’s try it one more time!

2 Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what the procedure will do. What are its inputs and what are its outputs. In a sentence, what does it accomplish? 3.Restate the problem recursively. Look for instances of the problem you defined in step 2. If the procedure returns a value, recursive calls to the function must use the returned values and every path through the function must return a value. 4.Write the procedure using instances of itself. Only worry about a single call of the routine. This is similar to proof by induction. You only solve for one case, but all other cases take care of themselves. Do not be tempted to follow the recursion. Use the routines having the faith that they will work. If you try to follow the recursion while you are writing the procedure, you will become hopelessly lost. How can you follow the recursion when the recursive routine is not finished yet? 5.Make sure you take care of the base case stopping the recursion. I like to take care of this last as it is easier to pinpoint the ending conditions after the general case has been written. Some personalities may not be able to delay this decision, but you will find coding is faster if you are able to delay it. It is like delaying the condition on a loop. In all but the simplest of loops, it is easier to write the termination condition after the loop body is written. 6.Once the routine is written, go ahead and follow the recursion, if you wish.

3 Let’s try it This question involves a game with teddy bears. The game starts when I give you some bears. You can then give back some bears, but you must follow these rules (where ct is the number of bears that you have): If ct is even, then you may give back exactly ct/2 bears. If ct is divisible by 3 or 4, then you may multiply the last two digits of ct and give back this many bears. (By the way, the last digit of ct is ct%10, and the next-to-last digit is ((ct%100)/10). If ct is divisible by 5, then you may give back exactly 42 bears. The goal of the game is to end up with EXACTLY 42 bears. 1. Decide that the problem needs a recursive solution. This one cries for a recursive solution!

4 SO…. For example, suppose that you start with 250 bears. Then you could make these moves: --Start with 250 bears. --Since 250 is divisible by 5, you may return 42 of the bears, leaving you with 208 bears. --Since 208 is even, you may return half of the bears, leaving you with 104 bears. --Since 104 is even, you may return half of the bears, leaving you with 52 bears. --Since 52 is divisible by 4, you may multiply the last two digits (resulting in 10) and return these 10 bears. This leaves you with 42 bears. --You have reached the goal!

5 Try it! 2. Decide specifically what the procedure will do. What are its inputs and what are its outputs. In a sentence, what does it accomplish?

6 Boolean winBearGame(int ct) Returns true if you can win the Bear Game starting with ct bears.

7 Try it 3. Restate the problem recursively. Look for instances of the problem you defined in step 2. Be sure to use “winBearGame” in defining “winBearGame”. If you don’t, it isn’t recursive.

8 Return true if you have 42 bears or if you have double 42 bears.

9 Bad, no recursive call of winBearGames Second attempt: return true if you have 42 bears return true if ct is divisible by 5 AND winBearGame(ct-42) There are other cases, but this seems to be the right approach. Try to define the rest of it.

10 Try it 3. Restate the problem recursively. Look for instances of the problem you defined in step 2. Return true if one of these things happen: Ct = 42 Ct%2==0 and winBearGame(ct – ct/2) Ct%3 ==0|| ct%4 ==0 and winBearGame(ct-multOfDigits) Ct%5==0 and winBearGame(ct-42)

11 Try it! 4. Write the procedure using instances of itself. Only worry about a single call of the routine. This is similar to proof by induction. You only solve for one case, but all other cases take care of themselves.

12 Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0) winBearGame(ct – ct/2) if (ct%3==0|| ct%4==0){ dig1 = ct%10; dig2 = ((ct%100/10) winBearGame(ct-dig1*dig2) if (ct%5 ==0) winBearGame(ct-42) }

13 We didn’t use the return values! If you don’t use them, it is likely wrong. Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0) winBearGame(ct – ct/2) if (ct%3==0|| ct%4==0){ dig1 = ct%10; dig2 = ((ct%100/10) winBearGame(ct-dig1*dig2) if (ct%5 ==0) winBearGame(ct-42) }

14 But now we don’t always return a value Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0) return winBearGame(ct – ct/2) if (ct%3==0|| ct%4==0){ dig1 = ct%10; dig2 = ((ct%100/10) return winBearGame(ct-dig1*dig2) if (ct%5 ==0) return winBearGame(ct-42) }

15 How close are we? Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0) return winBearGame(ct – ct/2) if (ct%3==0|| ct%4==0){ dig1 = ct%10; dig2 = ((ct%100/10) return winBearGame(ct-dig1*dig2) if (ct%5 ==0) return winBearGame(ct-42) return false; }

16 Just because we CAN give back have the bears doesn’t me we WANT to. Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0) return winBearGame(ct – ct/2) if (ct%3==0|| ct%4==0){ dig1 = ct%10; dig2 = ((ct%100/10) return winBearGame(ct-dig1*dig2) if (ct%5 ==0) return winBearGame(ct-42) }

17 How about this… Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0 and winBearGame(ct – ct/2)) return true; dig1 = ct%10; dig2 = ((ct%100/10) if (ct%3==0|| ct%4==0) and winBearGame(ct-dig1*dig2) return true; if (ct%5 ==0) and winBearGame(ct-42) return true; return false; }

18 Try it! 5. Make sure you take care of the base case stopping the recursion. I like to take care of this last as it is easier to pinpoint the ending conditions after the general case has been written. What is the base case for our code? When do you stop executing?

19 What is the base case? We seem to have several. Do we have enough? Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0 and winBearGame(ct – ct/2)) return true; dig1 = ct%10; dig2 = ((ct%100/10) if (ct%3==0|| ct%4==0) and winBearGame(ct-dig1*dig2) return true; if (ct%5 ==0) and winBearGame(ct-42) return true; return false; }

20 Suppose ct became zero, what would happen? Boolean winBearGame( int ct) { if (ct==42) return true; if (ct%2 ==0 and winBearGame(ct – ct/2)) return true; dig1 = ct%10; dig2 = ((ct%100/10) if (ct%3==0|| ct%4==0) and winBearGame(ct-dig1*dig2) return true; if (ct%5 ==0) and winBearGame(ct-42) return true; return false; }

21 How about this? Boolean winBearGame( int ct) { if (ct==42) return true; if (ct < 42) return false; if (ct%2 ==0 and winBearGame(ct – ct/2)) return true; dig1 = ct%10; dig2 = ((ct%100/10) if (ct%3==0|| ct%4==0) and winBearGame(ct-dig1*dig2) return true; if (ct%5 ==0) and winBearGame(ct-42) return true; return false; }

22 Now go ahead and trace it, if you want. 6. Once the routine is written, go ahead and follow the recursion, if you wish. While I believe that you should know HOW to trace it, generally, the tracing will get you even more confused and you will think even correct code is wrong.


Download ppt "Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what."

Similar presentations


Ads by Google