-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Created problem_112.py in project_euler #2532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
42b233e
Add files via upload
Kush1101 8e80876
Create __init__.py
Kush1101 cd8780c
Update and rename project_euler/problem_112.py to project_euler/probl…
Kush1101 6b5bec1
Update project_euler/problem_112/sol1.py
Kush1101 c1b8ccd
Update sol1.py
Kush1101 1635a7f
Update sol1.py
Kush1101 00923f6
Update project_euler/problem_112/sol1.py
Kush1101 7323ce7
Update project_euler/problem_112/__init__.py
Kush1101 8fcdcc8
Update __init__.py
Kush1101 dde4b3f
Update __init__.py
Kush1101 bc220c7
Update __init__.py
Kush1101 b4f2f0e
delete __init__.py content
realDuYuanChao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
Working from left-to-right if no digit is exceeded by the digit to its left it is | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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): | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return False | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return "".join(sorted(str(n))) != str(n) and "".join(sorted(str(n)))[::-1] != str(n) | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def solution(percent: int = 99) -> int: | ||
""" | ||
Returns the least number for which the proportion of bouncy numbers is | ||
exactly 'percent' | ||
>>> solution(50) | ||
538 | ||
>>> solution(90) | ||
21780 | ||
>>> solution(80) | ||
4770 | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
bouncy_num = 0 | ||
num = 1 | ||
while True: | ||
if check_bouncy(num): | ||
bouncy_num += 1 | ||
if (bouncy_num / num) * 100 >= percent: | ||
Kush1101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return num | ||
num += 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() | ||
print(f"{solution(99)}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.