Here's a variation that works for inputs of any size. We can use mathematical induction to form logical structure of our program -
def int_check(whole, part):
if whole < part:
return False # base case: w < p
elif whole % e(part) == part:
return True # induction: w >= p
else:
return int_check(whole // 10, part) # induction: w >= p, w % e(p) != p
def e(n, m = 10):
if n < 10:
return m # base case: n < 10
else:
return e(n // 10, m * 10) # induction: n >= 10
Here's a variation that does not need to compute a separate e (seen above). This has some similarity to @Morinator's technique but is careful to avoid bugs found in their answer -
def int_check(whole, part):
def loop (w, q):
if q == 0:
return True # base case: q == 0
elif w < q:
return False # induction: q > 0
elif w % 10 == q % 10:
return loop(w // 10, q // 10) # induction: q > 0, w >= q
else:
return loop(w // 10, part) # induction: q > 0, w >= q, w % 10 != q % 10
return loop(whole, part)
Each implementation of int_check has the same output -
print(int_check(1234567, 23)) # True
print(int_check(1234567, 456)) # True
print(int_check(1234567, 3)) # True
print(int_check(1234567, 123)) # True
print(int_check(1234567, 3456)) # True
print(int_check(1234567, 56)) # True
print(int_check(1234567, 1234567)) # True
print(int_check(1234567, 58)) # False
print(int_check(1234567, 125)) # False
print(int_check(1234567, 2347)) # False
print(int_check(1234567, 45679)) # False
print(int_check(1234567, 99)) # False
12in132OK)? For the record, recursion is a horrible solution to this problem. Your teacher is telling you to hammer a nail and giving you a feather, a hairpin and a pipe cleaner to do it with. You can just usestr(x) in str(y), so don't let your teacher get in the way of your education.(y, x), just like in the code, so in my examples, it just says that, for example 1,23is in1234, while in example 2,44is not in76384. And yeah, I figured that using recursion in this problem is hard, but we'll get a zero grade if we don't use recursion so we have no choice :(str(x)compare withstr(y)as @ggorlen suggested. Because number can only be compared with ordinal values.