posted 2 years ago
Hey guys!! I just reached the recursion section of the coding bat website! Yay! I don't recall every encountering anything like this before so it's taking me a bit to really grasp it. Anyway ... in the screen shot below ... I actually looked up the answer. But when I go through it I don't understand why it works. I DO understand the basics of recursion but I'll explain my confusion.
So in this example ... if n were 717 like the first example I understand the first time it checks it picks up that end 7 and then n is 71. The next round through it just divides by 10 since the remainder isn't 7. Then the number is just 7. So my confusion is ... how does it get that final 7? 7/10 doesn't give a remainder of 7 so how does it know to count that? So obviously I am missing something here.
Thanks!!
Screen shot won't work so I am just copying and pasting the problem and solution.
Given a nonnegative int n, return the count of the occurrences of 7 as a digit, so for example 717 yields 2. (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
public int count7(int n) {
if (n==0) return 0;
if (n%10 == 7) return 1 + count7(n/10);
return count7(n/10);
}
So in this example ... if n were 717 like the first example I understand the first time it checks it picks up that end 7 and then n is 71. The next round through it just divides by 10 since the remainder isn't 7. Then the number is just 7. So my confusion is ... how does it get that final 7? 7/10 doesn't give a remainder of 7 so how does it know to count that? So obviously I am missing something here.
Thanks!!
Screen shot won't work so I am just copying and pasting the problem and solution.
Given a nonnegative int n, return the count of the occurrences of 7 as a digit, so for example 717 yields 2. (no loops). Note that mod (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the rightmost digit (126 / 10 is 12).
public int count7(int n) {
if (n==0) return 0;
if (n%10 == 7) return 1 + count7(n/10);
return count7(n/10);
}
posted 2 years ago
The "7/10" is not the test. It is the "7 % 10 == 7" that is the test. The test is passed, and the 7/10 equals zero which is sent to the next recursion call. This zero causes the recursion to stop, and the whole thing is unrolled.
Henry
Danielle Rutter wrote:how does it get that final 7? 7/10 doesn't give a remainder of 7 so how does it know to count that? So obviously I am missing something here.
The "7/10" is not the test. It is the "7 % 10 == 7" that is the test. The test is passed, and the 7/10 equals zero which is sent to the next recursion call. This zero causes the recursion to stop, and the whole thing is unrolled.
Henry
posted 2 years ago
That is preferred over screen shots.
Also, remember to UseCodeTags (that's a link).
Screen shot won't work so I am just copying and pasting the problem and solution.
That is preferred over screen shots.
Also, remember to UseCodeTags (that's a link).
All things are lawful, but not all things are profitable.
Danielle Rutter
Greenhorn
Posts: 16
posted 2 years ago
I mistyped this. 7%10==7 ... the remainder of 7/10 is 7. But it's not! So the test isn't passed! That's why I'm confused. Obviously something very obvious just isn't clicking. It's always the easiest stuff that I have the hardest time understanding. :P
Henry Wong wrote:
The "7/10" is not the test. It is the "7 % 10 == 7" that is the test. The test is passed, and the 7/10 equals zero which is sent to the next recursion call. This zero causes the recursion to stop, and the whole thing is unrolled.
I mistyped this. 7%10==7 ... the remainder of 7/10 is 7. But it's not! So the test isn't passed! That's why I'm confused. Obviously something very obvious just isn't clicking. It's always the easiest stuff that I have the hardest time understanding. :P
Danielle Rutter
Greenhorn
Posts: 16
Today you are you, that is turer than true. There is no one alive who is youer than you!  Seuss. Tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
