problem 112 https projecteuler netproblem112 working from lefttoright if no digit is exceeded by the digit to its left it is called an increasing number for example 134468 similarly if no digit is exceeded by the digit to its right it is called a decreasing number for example 66420 we shall call a positive integer that is neither increasing nor decreasing a bouncy number for example 155349 clearly there cannot be any bouncy numbers below onehundred but just over half of the numbers below onethousand 525 are bouncy in fact the least number for which the proportion of bouncy numbers first reaches 50 is 538 surprisingly bouncy numbers become more and more common and by the time we reach 21780 the proportion of bouncy numbers is equal to 90 find the least number for which the proportion of bouncy numbers is exactly 99 returns true if number is bouncy false otherwise checkbouncy6789 false checkbouncy12345 false checkbouncy0 false checkbouncy6 74 traceback most recent call last valueerror checkbouncy accepts only integer arguments checkbouncy132475 true checkbouncy34 false checkbouncy341 true checkbouncy47 false checkbouncy12 54 traceback most recent call last valueerror checkbouncy accepts only integer arguments checkbouncy6548 true returns the least number for which the proportion of bouncy numbers is exactly percent solution50 538 solution90 21780 solution80 4770 solution105 traceback most recent call last valueerror solution only accepts values from 0 to 100 solution100 011 traceback most recent call last valueerror solution only accepts values from 0 to 100 returns true if number is bouncy false otherwise check_bouncy 6789 false check_bouncy 12345 false check_bouncy 0 false check_bouncy 6 74 traceback most recent call last valueerror check_bouncy accepts only integer arguments check_bouncy 132475 true check_bouncy 34 false check_bouncy 341 true check_bouncy 47 false check_bouncy 12 54 traceback most recent call last valueerror check_bouncy accepts only integer arguments check_bouncy 6548 true returns the least number for which the proportion of bouncy numbers is exactly percent solution 50 538 solution 90 21780 solution 80 4770 solution 105 traceback most recent call last valueerror solution only accepts values from 0 to 100 solution 100 011 traceback most recent call last valueerror solution only accepts values from 0 to 100
def check_bouncy(n: int) -> bool:
    
    if not isinstance(n, int):
        raise ValueError("check_bouncy() accepts only integer arguments")
    str_n = str(n)
    sorted_str_n = "".join(sorted(str_n))
    return str_n not in {sorted_str_n, sorted_str_n[::-1]}


def solution(percent: float = 99) -> int:
    
    if not 0 < percent < 100:
        raise ValueError("solution() only accepts values from 0 to 100")
    bouncy_num = 0
    num = 1

    while True:
        if check_bouncy(num):
            bouncy_num += 1
        if (bouncy_num / num) * 100 >= percent:
            return num
        num += 1


if __name__ == "__main__":
    from doctest import testmod

    testmod()
    print(f"{solution(99)}")