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
l_{0} + l_{end}, where l_{0} is the
first character of l and l_{end} is the rest of the characters.
Similarly, let
h be
h_{0} + h_{end}. If l_{0} does not
equal h_{0}, then NE(l, h) equals zero.
- Otherwise, we know that
l_{0} equals
h_{0}. If l_{0} equals '8', then
NE(l, h) equals
(1 + NE(l_{end}, h_{end})).
- Otherwise, NE(l, h) equals
NE(l_{end}, h_{end}).
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.