-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add Project Euler problem 205 solution 1 #5781
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
poyea
merged 7 commits into
TheAlgorithms:master
from
MaximSmolskiy:add-project-euler-problem-205-solution-1
Nov 9, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6fc67e8
updating DIRECTORY.md
ff0319f
Merge branch 'TheAlgorithms:master' into master
MaximSmolskiy 7737e28
Add solution
MaximSmolskiy e69a98d
updating DIRECTORY.md
ef04311
Fix
MaximSmolskiy 206e736
Merge branch 'add-project-euler-problem-205-solution-1' of https://gi…
MaximSmolskiy a272e27
Fix
MaximSmolskiy 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
Empty file.
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,75 @@ | ||
""" | ||
Project Euler Problem 205: https://projecteuler.net/problem=205 | ||
|
||
Peter has nine four-sided (pyramidal) dice, each with faces numbered 1, 2, 3, 4. | ||
Colin has six six-sided (cubic) dice, each with faces numbered 1, 2, 3, 4, 5, 6. | ||
|
||
Peter and Colin roll their dice and compare totals: the highest total wins. | ||
The result is a draw if the totals are equal. | ||
|
||
What is the probability that Pyramidal Peter beats Cubic Colin? | ||
Give your answer rounded to seven decimal places in the form 0.abcdefg | ||
""" | ||
|
||
from itertools import product | ||
|
||
|
||
def total_frequency_distribution(sides_number: int, dice_number: int) -> list[int]: | ||
""" | ||
Returns frequency distribution of total | ||
|
||
>>> total_frequency_distribution(sides_number=6, dice_number=1) | ||
[0, 1, 1, 1, 1, 1, 1] | ||
|
||
>>> total_frequency_distribution(sides_number=4, dice_number=2) | ||
[0, 0, 1, 2, 3, 4, 3, 2, 1] | ||
""" | ||
|
||
max_face_number = sides_number | ||
max_total = max_face_number * dice_number | ||
totals_frequencies = [0] * (max_total + 1) | ||
|
||
min_face_number = 1 | ||
faces_numbers = range(min_face_number, max_face_number + 1) | ||
for dice_numbers in product(faces_numbers, repeat=dice_number): | ||
total = sum(dice_numbers) | ||
totals_frequencies[total] += 1 | ||
|
||
return totals_frequencies | ||
|
||
|
||
def solution() -> float: | ||
""" | ||
Returns probability that Pyramidal Peter beats Cubic Colin | ||
rounded to seven decimal places in the form 0.abcdefg | ||
|
||
>>> solution() | ||
0.5731441 | ||
""" | ||
|
||
peter_totals_frequencies = total_frequency_distribution( | ||
sides_number=4, dice_number=9 | ||
) | ||
colin_totals_frequencies = total_frequency_distribution( | ||
sides_number=6, dice_number=6 | ||
) | ||
|
||
peter_wins_count = 0 | ||
min_peter_total = 9 | ||
max_peter_total = 4 * 9 | ||
min_colin_total = 6 | ||
for peter_total in range(min_peter_total, max_peter_total + 1): | ||
peter_wins_count += peter_totals_frequencies[peter_total] * sum( | ||
colin_totals_frequencies[min_colin_total:peter_total] | ||
) | ||
|
||
total_games_number = (4 ** 9) * (6 ** 6) | ||
peter_win_probability = peter_wins_count / total_games_number | ||
|
||
rounded_peter_win_probability = round(peter_win_probability, ndigits=7) | ||
|
||
return rounded_peter_win_probability | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"{solution() = }") |
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.