Feb-17-2019, 10:19 AM
It looks like the recursive approach is wrong:
# the next integer whose digits are increasing. def increasing(n): asastring = str(n) length = len(asastring) if asastring == "9"*length: return "1"*(length+1) if length == 1: return int(n)+1 if length >= 2: firstcharacter = asastring[0] secondcharacter = asastring[1] if int(firstcharacter) > int(secondcharacter): return int(str(firstcharacter)*length) if firstcharacter == secondcharacter: return firstcharacter+str(increasing(int(asastring[1:]))) if int(firstcharacter) < int(secondcharacter): if secondcharacter == "9": return str(int(firstcharacter)+1) * len(str(n)) return firstcharacter+str(increasing(int(asastring[1:]))) def decreasing(n): asastring = str(n) length = len(asastring) # First the case where we need to add a digit. if asastring == "9"*length: return "1"+"0"*length # Now we know that the next integer has the same number of digits as the original number. if length == 1: return int(n)+1 if length >= 2: firstcharacter = asastring[0] secondcharacter = asastring[1] if int(firstcharacter) > int(secondcharacter): return str(firstcharacter) + str(decreasing(asastring[1:])) if int(firstcharacter) == int(secondcharacter): return decreasing(firstcharacter+str(decreasing(asastring[1:]))) if int(firstcharacter) < int(secondcharacter): return str(int(firstcharacter)+1)+'0'*(length-1) i = 1 found = False for k in range(4000): i = min(int(increasing(i)), int(decreasing(i))) print(i)outputs RecursionError: maximum recursion depth exceeded while getting the str of an object