As always, feel free to solve this problem on your own. However, I
would like you to think about the problem recursively. That may not be
the most natural way for you to think about it, but it's good practice.
One of the keys to this problem is to turn low and high
into strings and work on the strings. Here's how you can do it.
- Turn low and high
into the strings l and h.
- If the two strings differ in size, you can return instantly. Think about it.
- Otherwise, let's define a function NE(l, h), where l and
h are strings that are the same size. We'll define NE as follows:
- If l and h are empty strings, then NE(l, h) equals zero.
- Otherwise, let l be
l0 + lend, where l0 is the
first character of l and lend is the rest of the characters.
Similarly, let
h be
h0 + hend. If l0 does not
equal h0, then NE(l, h) equals zero.
- Otherwise, we know that
l0 equals
h0. If l0 equals '8', then
NE(l, h) equals
(1 + NE(lend, hend)).
- Otherwise, NE(l, h) equals
NE(lend, hend).
Go ahead and turn this definition into a recursive solution.
I know you can do this with one for loop after you've created l
and h. However, I'd like you to structure it with recursion. It's good
practice.