Danielle Rutter

Greenhorn

Posts: 16

posted 1 year 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 non-negative 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 non-negative 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 1 year 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 1 year 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 1 year ago

I mis-typed 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 mis-typed 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