From 42b233e9335bfb392129501655d4f8026f73fbaf Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 1 Oct 2020 00:05:54 +0530 Subject: [PATCH 01/12] Add files via upload --- project_euler/problem_112.py | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 project_euler/problem_112.py diff --git a/project_euler/problem_112.py b/project_euler/problem_112.py new file mode 100644 index 000000000000..ea8bdb57c235 --- /dev/null +++ b/project_euler/problem_112.py @@ -0,0 +1,62 @@ +""" +Working from left-to-right 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 one-hundred, but just over half of +the numbers below one-thousand (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%. +""" + + +def check_bouncy(n: int) -> bool: + """ + Returns True if number is bouncy, False otherwise + >>> check_bouncy(6789) + False + >>> check_bouncy(-12345) + False + >>> check_bouncy(6.74) + False + >>> check_bouncy(132475) + True + >>> check_bouncy(-6548) + True + """ + if not isinstance(n, int): + return False + return "".join(sorted(str(n))) != str(n) and "".join(sorted(str(n)))[::-1] != str(n) + + +def compute_num(percent: int) -> int: + """ + Returns the least number for which the proportion of bouncy numbers is + exactly 'percent' + >>> compute_num(50) + 538 + >>> compute_num(90) + 21780 + >>> compute_num(80) + 4770 + """ + 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"{compute_num(99)}") From 8e8087634321f3152a0d5901f4a1a6c84be5e8c3 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 1 Oct 2020 00:10:06 +0530 Subject: [PATCH 02/12] Create __init__.py --- project_euler/problem_112/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 project_euler/problem_112/__init__.py diff --git a/project_euler/problem_112/__init__.py b/project_euler/problem_112/__init__.py new file mode 100644 index 000000000000..792d6005489e --- /dev/null +++ b/project_euler/problem_112/__init__.py @@ -0,0 +1 @@ +# From cd8780ca12ce9b73914580367fd00b611bd98e43 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 1 Oct 2020 00:11:42 +0530 Subject: [PATCH 03/12] Update and rename project_euler/problem_112.py to project_euler/problem_112/sol1.py --- project_euler/{problem_112.py => problem_112/sol1.py} | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) rename project_euler/{problem_112.py => problem_112/sol1.py} (89%) diff --git a/project_euler/problem_112.py b/project_euler/problem_112/sol1.py similarity index 89% rename from project_euler/problem_112.py rename to project_euler/problem_112/sol1.py index ea8bdb57c235..6daf99400ea1 100644 --- a/project_euler/problem_112.py +++ b/project_euler/problem_112/sol1.py @@ -34,15 +34,15 @@ def check_bouncy(n: int) -> bool: return "".join(sorted(str(n))) != str(n) and "".join(sorted(str(n)))[::-1] != str(n) -def compute_num(percent: int) -> int: +def solution(percent: int = 99) -> int: """ Returns the least number for which the proportion of bouncy numbers is exactly 'percent' - >>> compute_num(50) + >>> solution(50) 538 - >>> compute_num(90) + >>> solution(90) 21780 - >>> compute_num(80) + >>> solution(80) 4770 """ bouncy_num = 0 @@ -59,4 +59,4 @@ def compute_num(percent: int) -> int: from doctest import testmod testmod() - print(f"{compute_num(99)}") + print(f"{solution(99)}") From 6b5bec1fae6a9077cfa321d41485423d00011ec3 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 1 Oct 2020 13:01:45 +0530 Subject: [PATCH 04/12] Update project_euler/problem_112/sol1.py Co-authored-by: Dhruv --- project_euler/problem_112/sol1.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/project_euler/problem_112/sol1.py b/project_euler/problem_112/sol1.py index 6daf99400ea1..c68945526381 100644 --- a/project_euler/problem_112/sol1.py +++ b/project_euler/problem_112/sol1.py @@ -1,5 +1,8 @@ """ +Problem 112: https://projecteuler.net/problem=112 + Working from left-to-right 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. From c1b8ccd5df01a092dcbcd23a9350a1a9ca258ff8 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 1 Oct 2020 13:05:08 +0530 Subject: [PATCH 05/12] Update sol1.py --- project_euler/problem_112/sol1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_112/sol1.py b/project_euler/problem_112/sol1.py index c68945526381..732e44fd639b 100644 --- a/project_euler/problem_112/sol1.py +++ b/project_euler/problem_112/sol1.py @@ -2,7 +2,6 @@ Problem 112: https://projecteuler.net/problem=112 Working from left-to-right 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. From 1635a7fee0c041d7252f0d9d5c467528e4ca61b7 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 1 Oct 2020 13:36:57 +0530 Subject: [PATCH 06/12] Update sol1.py --- project_euler/problem_112/sol1.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_112/sol1.py b/project_euler/problem_112/sol1.py index 732e44fd639b..9800d49486fb 100644 --- a/project_euler/problem_112/sol1.py +++ b/project_euler/problem_112/sol1.py @@ -24,19 +24,33 @@ def check_bouncy(n: int) -> bool: False >>> check_bouncy(-12345) False - >>> check_bouncy(6.74) + >>> 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 """ if not isinstance(n, int): - return False + raise ValueError("check_bouncy() accepts only integer arguments") return "".join(sorted(str(n))) != str(n) and "".join(sorted(str(n)))[::-1] != str(n) -def solution(percent: int = 99) -> int: +def solution(percent: float = 99) -> int: """ Returns the least number for which the proportion of bouncy numbers is exactly 'percent' @@ -46,9 +60,20 @@ def solution(percent: int = 99) -> int: 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 """ + if 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 From 00923f61b0334cb6011c93c7c68e83c0d01e913a Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 1 Oct 2020 14:30:01 +0530 Subject: [PATCH 07/12] Update project_euler/problem_112/sol1.py Co-authored-by: Du Yuanchao --- project_euler/problem_112/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_112/sol1.py b/project_euler/problem_112/sol1.py index 9800d49486fb..d8cb334c9508 100644 --- a/project_euler/problem_112/sol1.py +++ b/project_euler/problem_112/sol1.py @@ -69,7 +69,7 @@ def solution(percent: float = 99) -> int: ... ValueError: solution() only accepts values from 0 to 100 """ - if percent >= 100: + if not 0 < percent < 100: raise ValueError("solution() only accepts values from 0 to 100") bouncy_num = 0 num = 1 From 7323ce700175b95fb4685064ef0c70ff4a6cad6b Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 1 Oct 2020 19:21:49 +0530 Subject: [PATCH 08/12] Update project_euler/problem_112/__init__.py Co-authored-by: Du Yuanchao --- project_euler/problem_112/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_112/__init__.py b/project_euler/problem_112/__init__.py index 792d6005489e..8b137891791f 100644 --- a/project_euler/problem_112/__init__.py +++ b/project_euler/problem_112/__init__.py @@ -1 +1 @@ -# + From 8fcdcc89aa3b6c92b691892199183e91e74957a9 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Thu, 1 Oct 2020 19:24:11 +0530 Subject: [PATCH 09/12] Update __init__.py --- project_euler/problem_112/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_112/__init__.py b/project_euler/problem_112/__init__.py index 8b137891791f..792d6005489e 100644 --- a/project_euler/problem_112/__init__.py +++ b/project_euler/problem_112/__init__.py @@ -1 +1 @@ - +# From dde4b3f0afbc4621c231e8bf8d8cfaae4b57c60c Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Fri, 2 Oct 2020 12:05:50 +0530 Subject: [PATCH 10/12] Update __init__.py --- project_euler/problem_112/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_112/__init__.py b/project_euler/problem_112/__init__.py index 792d6005489e..8b137891791f 100644 --- a/project_euler/problem_112/__init__.py +++ b/project_euler/problem_112/__init__.py @@ -1 +1 @@ -# + From bc220c771ff6824bb2ed0eb2c205cbc7b425807e Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Fri, 2 Oct 2020 15:41:59 +0530 Subject: [PATCH 11/12] Update __init__.py --- project_euler/problem_112/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_112/__init__.py b/project_euler/problem_112/__init__.py index 8b137891791f..792d6005489e 100644 --- a/project_euler/problem_112/__init__.py +++ b/project_euler/problem_112/__init__.py @@ -1 +1 @@ - +# From b4f2f0e9e8dd630e5b07a039bba9d622a6371e7c Mon Sep 17 00:00:00 2001 From: shellhub Date: Sat, 3 Oct 2020 16:31:39 +0800 Subject: [PATCH 12/12] delete __init__.py content --- project_euler/problem_112/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_112/__init__.py b/project_euler/problem_112/__init__.py index 792d6005489e..e69de29bb2d1 100644 --- a/project_euler/problem_112/__init__.py +++ b/project_euler/problem_112/__init__.py @@ -1 +0,0 @@ -#