|
| 1 | +""" |
| 2 | +Problem 112: https://projecteuler.net/problem=112 |
| 3 | +
|
| 4 | +Working from left-to-right if no digit is exceeded by the digit to its left it is |
| 5 | +called an increasing number; for example, 134468. |
| 6 | +Similarly if no digit is exceeded by the digit to its right it is called a decreasing |
| 7 | +number; for example, 66420. |
| 8 | +We shall call a positive integer that is neither increasing nor decreasing a "bouncy" |
| 9 | +number, for example, 155349. |
| 10 | +Clearly there cannot be any bouncy numbers below one-hundred, but just over half of |
| 11 | +the numbers below one-thousand (525) are bouncy. In fact, the least number for which |
| 12 | +the proportion of bouncy numbers first reaches 50% is 538. |
| 13 | +Surprisingly, bouncy numbers become more and more common and by the time we reach |
| 14 | +21780 the proportion of bouncy numbers is equal to 90%. |
| 15 | +
|
| 16 | +Find the least number for which the proportion of bouncy numbers is exactly 99%. |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +def check_bouncy(n: int) -> bool: |
| 21 | + """ |
| 22 | + Returns True if number is bouncy, False otherwise |
| 23 | + >>> check_bouncy(6789) |
| 24 | + False |
| 25 | + >>> check_bouncy(-12345) |
| 26 | + False |
| 27 | + >>> check_bouncy(0) |
| 28 | + False |
| 29 | + >>> check_bouncy(6.74) |
| 30 | + Traceback (most recent call last): |
| 31 | + ... |
| 32 | + ValueError: check_bouncy() accepts only integer arguments |
| 33 | + >>> check_bouncy(132475) |
| 34 | + True |
| 35 | + >>> check_bouncy(34) |
| 36 | + False |
| 37 | + >>> check_bouncy(341) |
| 38 | + True |
| 39 | + >>> check_bouncy(47) |
| 40 | + False |
| 41 | + >>> check_bouncy(-12.54) |
| 42 | + Traceback (most recent call last): |
| 43 | + ... |
| 44 | + ValueError: check_bouncy() accepts only integer arguments |
| 45 | + >>> check_bouncy(-6548) |
| 46 | + True |
| 47 | + """ |
| 48 | + if not isinstance(n, int): |
| 49 | + raise ValueError("check_bouncy() accepts only integer arguments") |
| 50 | + return "".join(sorted(str(n))) != str(n) and "".join(sorted(str(n)))[::-1] != str(n) |
| 51 | + |
| 52 | + |
| 53 | +def solution(percent: float = 99) -> int: |
| 54 | + """ |
| 55 | + Returns the least number for which the proportion of bouncy numbers is |
| 56 | + exactly 'percent' |
| 57 | + >>> solution(50) |
| 58 | + 538 |
| 59 | + >>> solution(90) |
| 60 | + 21780 |
| 61 | + >>> solution(80) |
| 62 | + 4770 |
| 63 | + >>> solution(105) |
| 64 | + Traceback (most recent call last): |
| 65 | + ... |
| 66 | + ValueError: solution() only accepts values from 0 to 100 |
| 67 | + >>> solution(100.011) |
| 68 | + Traceback (most recent call last): |
| 69 | + ... |
| 70 | + ValueError: solution() only accepts values from 0 to 100 |
| 71 | + """ |
| 72 | + if not 0 < percent < 100: |
| 73 | + raise ValueError("solution() only accepts values from 0 to 100") |
| 74 | + bouncy_num = 0 |
| 75 | + num = 1 |
| 76 | + |
| 77 | + while True: |
| 78 | + if check_bouncy(num): |
| 79 | + bouncy_num += 1 |
| 80 | + if (bouncy_num / num) * 100 >= percent: |
| 81 | + return num |
| 82 | + num += 1 |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + from doctest import testmod |
| 87 | + |
| 88 | + testmod() |
| 89 | + print(f"{solution(99)}") |
0 commit comments