Solution: Factorials
- [Instructor] I hope you had fun figuring out those factorial functions. Welcome back. How'd you do? I'll show you my solution, and if you looked at the hints file, this solution probably shouldn't come as too much of a surprise. The first thing we want to do is make sure we're able to take the factorial of our input, so if our input is not an integer, we're going to return none. So this takes care of floats like 1.2 as well as the string "spam, spam, spam, spam, spam, spam" that you might have noticed was a test case. Of course, negative two is still a valid integer, so we need to make a special case for that as well, so we check to see if the input number is less than zero and then we return none, because you cannot have the factorial of a negative integer. Now we want to get down to the business with calculating the factorial. To do this, I'm going to make two variables, fact, short for factorial. This is the thing that we ultimately return, and then counter to keep track of how many loops we need to do. So while counter is less than the input num, that's basically the number of iterations, our factorial is equal to factorial, the existing factorial, times the counter, and then with each pass in the loop, we increment or add one to our counter. So on the first pass, you're going to be multiplying by one, one times one. On the second pass, you're going to be multiplying by two, on the third pass, times three, et cetera, up to the number that was passed in. So if you pass in five, you're going to end up returning one times two times three times four times five, which is exactly what we want. And remember, we have a special case where the input number is equal to zero, so mathematicians decided that the factorial of zero is one. I didn't decide that, that was mathematicians, but we as computer programmers must follow the rules of mathematicians, so does this work when the input number is zero? If num is zero, then we start the fact number at one. The while loop never gets entered because the input number zero is already greater than the counter, and so we just end up returning one, which is correct, so great. Now, there's also a shorter way to write this function, and that's through recursion, where a function calls itself. So I just want to show you the solution really quick. I'm actually going to keep the same checks here, and I'm going to add an additional case. If num equals zero, return one. Okay, finally, we want to return num times factorial num minus one, and if we test this, that also works. Awesome. So what's going on? Well, let's say we call this function factorial with the number three, and I'm just going to write a comment down here. Factorial three is how we're going to call it, and what this is equal to, this isn't Python code, this is just sort of a shorthand. What this is going to do is it's going to say, okay, great, all these checks pass for the number three. It's not equal to zero, so we go down here and we return three times factorial of three minus one, which is two, so we end up returning three times factorial two. Well, what's factorial two? So it gets called again with num being two, passes all of this, and it turns out that factorial two is equal to two times factorial one. All right, so what's factorial one? We call this again. It gets called here. Factorial one is equal to one times factorial zero, one times factorial zero. Well, what's factorial zero? Gets called again. If the number is zero, we return one, so factorial zero is one, and it turns out that factorial three is three times two times one times one, which is six, so our original function call, factorial three, is equal to six, and ultimately, return six. Now, recursion might seem like magic at first, but don't be intimidated. It's just a fancy kind of loop. It does take a while to get the hang of, but it's also a very handy skill to have in your programming repertoire.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。