def is_increasing(n_str): if len(n_str) < 2: return True if n_str[0] <= n_str[1]: return is_increasing(n_str[1:]) return False def is_decreasing(n_str): if len(n_str) < 2: return True if n_str[0] >= n_str[1]: return is_decreasing(n_str[1:]) return False def is_bouncy(n_str): return not (is_increasing(n_str) or is_decreasing(n_str)) def euler_112(): n, n_bouncy = 0, 0 while True: n += 1 if is_bouncy(str(n)): n_bouncy += 1 ratio = n_bouncy / n if ratio >= 0.99: return n if __name__ == "__main__": solution = euler_112() print("e112.py: " + str(solution)) assert(solution == 1587000)