All right. Starting off with some bunnies. I hope you enjoyed a little bit of bunny fun. So in case you didn't just immediately come to this video from our last perusal activity, we have these bunnies standing in a line numbered one, two whatever. This sounds crazy. Yeah, this is a very contrived example, but it's fun, and by the way this is one of the questions on coding birds recursion problems. So bunnyEars, I think it's called. We are recursively calculating how many bunnyEars there are. Before we jump into the recursive solution, I'll just admit right away, this is an okay thing to do with recursion, but I think fairly clearly the process of counting bunnyEars is a much more of an iterative solution. Would be a lot simpler, right? Probably, a loop with an if statement inside. The answer here is D. The question is, does the code work as intended? It does, I didn't make any changes there, but by the way that's always a fun thing if you're, "I really want to ask about one thing that I don't want to just have two options." You can always throw "Does the code work as intended?" That gets them looking at the entire program. Then what are the parameters to the first four calls after bunnyEars of five? So assuming we got a main program we call bunnyEars 5, after that's already called, I'm going to say, "What are the parameters to the next four calls?" So just an image that you can feel free to get screenshot and take. I thought we cannot possibly not have bunnies with three ears, so there you are. If we called bunnyEars 2, with the number five, then we go, is five equal to zero? No, we haven't gotten to our base case yet. Is bunny's modulo two equal one? So basically we're checking for an odd bunny, because the odd bunnies have the regular number of ears, and even bunnies, two and four, have three ears or two ears and a leg, whatever. Anyway, so that's why we have our two different returns there. If it's odd, we return two plus, and then keep going down the line, otherwise we're going to turn three plus. So this first one, five is odd. So we're going to call it return two plus bunnies minus one. Five minus one is four. So the first parameter that we pass is four. Now I'm not going to actually write the parameter up in the top each time, that is a good code tracing thing and we'll hopefully see some examples of that. But then the next time when we call it on four, four is even, so we'd skip the if statement and we call the bottom-line, return three plus bunnies of four minus one, which is three, and then we go back up and do one for the odds, and the ones for the evens. So basically every time we call this, we're going to be bouncing back and forth between those return statements, switching off each time until we get down to zero bunnies. What is printed when this code is called? I asked this question because students really have difficulty understanding or remembering that when we come to this recursive call, we hit pause. We do not continue executing the code in this. It's not like we're returning, we're not jumping away. We're hitting pause until we finish this recursive call, and then we'll eventually come back to it. So that's why the 4, 3, 2, 1 before printing out "Num" is there, and a really good thing you could even do it. Let me see, do I have that? No. I didn't put that in as example. One thing you can do to clarify this for students is not just print out "Num". You could put before "Plus num". Then after calling "Num", but we're getting that first print. That's the 4, 3, 2, 1. The 1, 2, 3, 4 building up underneath, that's coming from the second print as we get pop those things off the stack. So think of your cups again, we called it with four, and then we put the three cup, the two cup, the one cup, and then when we take it off, we print one and take it off. There we go. Before we talk about this question, let me provide a small interlude. Little do you know, when I was in the middle of recording this video, and I talked about this question, and then I started talking about the next question, and as I was doing it, I had this bad feeling. I don't think I tested this code. I think it's wrong. Yeah, and it was. I just sat down for 15 minutes and fixed the next few slides. Here's what I want you to take away from this lesson learned. If you are worried or unwilling to be caught out in class, you must always compile your code. You just must, because you're not just thinking about writing code, you're thinking about what code do I need to write so that they can find a particular problem and things like this. It's really difficult, and your brain is overloaded. So it's better if you figure out what you want to show, then stop thinking about that, just write the program and test it using a compiler before you go back to working on the question again. Now it's time to talk about this question, and in a minute I'm going to tell you that one of the reasons I have this question having two parts is not just for the particular things I'm going to bring out, but I want to give students exposure to the type of method header that we use to search arrays. Not in a binary search, but for a linear search. So this says, this was supposed to implement a recursive search of an array. I say that because this is one way to get kids to start to look at it, but not have to understand it in detail. Because I tell them what it does, and I just say, "In which order is the array searched?" So again, this looks scary and recursion can be hard to make your head want to even look at, but by just saying in which order is the array search, hopefully kids think of this, "Well, you know that base case, that base case tells me where I stop." So that could probably tell me, that's not where I started and I weren't down there. So since it says return zero, we're going to be pretty sure that this is going to be working right to left, assuming it works as it is intended. Right to left, because left is zero. Length is negative one, that's left of zero, which is our smallest index we'd have. Now there's two other places kids could look to find out this is also the case, and that is to notice that every recursive call is reducing length by one. So it might be great to see if you can get all three of those places noticed or two, if you want to think about they're two different cases, by strings. Point both of them out if they don't. Now we'll do a part two, and it's nice again because things have already invested in this problem, and so it's not like you're acting to look at a whole new problem. Sometimes when you have more challenging things, it can be good to try to get two problems out of one. This code does not work as intended. How many lines of code do you need to modify for it to work? Another teacher tip here. If you don't want to spend the time thinking what options should I give that would possibly give it away? Don't. Just take the easy way out, like I did here. How many lines of code need to be modified? Then when the kids are talking about it, even if one student came up with two, the other student came up to two. They still have to say, well, which two. Then talk about which ones they picked. It's very sneaky, I like it. So the problem here, probably this isn't terribly difficult, it's just a good thing to do. They got the parameters in the wrong order. It's array length "a", and if you look up at the method header, it's "a" array, and then length. However, again, one of the things that I would summarize all of this work with is this is a pretty common thing to do if we want to use recursion to walk over a 1D array. We provide the length, and it gets smaller every time or larger. You can start it at zero and go the other way, or/and then we provide the array, and the array doesn't change every time. It's just constantly passed in as a parameter. One last one. In fact we're using that same one again. So our brain is still working on that. What changes need to be made to Lines 1 and 2 so that this code returns a Boolean value to indicate when "a" is found, or when "a" is not found. This is this. Did you find my other one? What was I saying? Let's talk about this. There's one more error because when I sat there and told you just a minute ago, [inaudible] I unfixed this one. Again, Boolean. The key thing here is if we're not counting how many things we found in the array, we're just saying true or false that we do, then when we find the item when arrays of length equal equal "a", we want to stop recursing. We don't want to look any further. We don't have to look any further. So that's why at that point we would just return true. So there will end up being two base cases. Return true for when array length equal "a". What should the first base case be? Yeah, return false. Maybe you guys figured that out already in the comments with perusal. I hope so, and maybe that made you have a little good time because you found one that I missed. Very good. Also then the key thing for the second line is no change you need to make. I either found it, I return true, or I keep looking in my smaller subset of my array.