From 7e69f81f74ed288282bd81de4f4b0c2f804549fb Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 23 Oct 2021 11:49:01 +0000 Subject: [PATCH 1/5] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 950d8e2c0c4b..ebe353fb5bcd 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -317,6 +317,7 @@ * [Breadth First Search Shortest Path](https://github.com/TheAlgorithms/Python/blob/master/graphs/breadth_first_search_shortest_path.py) * [Check Bipartite Graph Bfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_bfs.py) * [Check Bipartite Graph Dfs](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_bipartite_graph_dfs.py) + * [Check Cycle](https://github.com/TheAlgorithms/Python/blob/master/graphs/check_cycle.py) * [Connected Components](https://github.com/TheAlgorithms/Python/blob/master/graphs/connected_components.py) * [Depth First Search](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search.py) * [Depth First Search 2](https://github.com/TheAlgorithms/Python/blob/master/graphs/depth_first_search_2.py) @@ -979,6 +980,7 @@ * [Instagram Crawler](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_crawler.py) * [Instagram Pic](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_pic.py) * [Instagram Video](https://github.com/TheAlgorithms/Python/blob/master/web_programming/instagram_video.py) + * [Nasa Data](https://github.com/TheAlgorithms/Python/blob/master/web_programming/nasa_data.py) * [Random Anime Character](https://github.com/TheAlgorithms/Python/blob/master/web_programming/random_anime_character.py) * [Recaptcha Verification](https://github.com/TheAlgorithms/Python/blob/master/web_programming/recaptcha_verification.py) * [Slack Message](https://github.com/TheAlgorithms/Python/blob/master/web_programming/slack_message.py) From 430e65981f2922bcf570da79dc7062ec071a7be2 Mon Sep 17 00:00:00 2001 From: kugiyasan Date: Sat, 23 Oct 2021 07:57:44 -0400 Subject: [PATCH 2/5] Project Euler 68 Solution --- project_euler/problem_068/__init__.py | 0 project_euler/problem_068/sol1.py | 128 ++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 project_euler/problem_068/__init__.py create mode 100644 project_euler/problem_068/sol1.py diff --git a/project_euler/problem_068/__init__.py b/project_euler/problem_068/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_068/sol1.py b/project_euler/problem_068/sol1.py new file mode 100644 index 000000000000..c334eb274db5 --- /dev/null +++ b/project_euler/problem_068/sol1.py @@ -0,0 +1,128 @@ +""" +Project Euler Problem 68: https://projecteuler.net/problem=68 + +Magic 5-gon ring + +Problem Statement: +Consider the following "magic" 3-gon ring, +filled with the numbers 1 to 6, and each line adding to nine. + + 4 + \ + 3 + / \ + 1 - 2 - 6 + / + 5 + +Working clockwise, and starting from the group of three +with the numerically lowest external node (4,3,2 in this example), +each solution can be described uniquely. +For example, the above solution can be described by the set: 4,3,2; 6,2,1; 5,1,3. + +It is possible to complete the ring with four different totals: 9, 10, 11, and 12. +There are eight solutions in total. +Total Solution Set +9 4,2,3; 5,3,1; 6,1,2 +9 4,3,2; 6,2,1; 5,1,3 +10 2,3,5; 4,5,1; 6,1,3 +10 2,5,3; 6,3,1; 4,1,5 +11 1,4,6; 3,6,2; 5,2,4 +11 1,6,4; 5,4,2; 3,2,6 +12 1,5,6; 2,6,4; 3,4,5 +12 1,6,5; 3,5,4; 2,4,6 + +By concatenating each group it is possible to form 9-digit strings; +the maximum string for a 3-gon ring is 432621513. + +Using the numbers 1 to 10, and depending on arrangements, +it is possible to form 16- and 17-digit strings. +What is the maximum 16-digit string for a "magic" 5-gon ring? +""" + +from itertools import permutations + + +def solution(gon_side: int = 5) -> int: + """ + Find the maximum number for a "magic" gon_side-gon ring + + The gon_side parameter should be in the range [3, 5], + other side numbers aren't tested + + >>> solution(3) + 432621513 + >>> solution(4) + 426561813732 + >>> solution() + 6531031914842725 + """ + if gon_side < 3 or gon_side > 5: + raise ValueError("gon_side must be in the range [3, 5]") + + # Since it's 16, we know 10 is on the outer ring + # Put the big numbers at the end so that they are never the first number + small_numbers = list(range(gon_side + 1, 0, -1)) + big_numbers = list(range(gon_side + 2, gon_side * 2 + 1)) + + for perm in permutations(small_numbers + big_numbers): + numbers = generate_gon_ring(gon_side, list(perm)) + if is_magic_gon(numbers): + return int("".join(str(n) for n in numbers)) + + raise ValueError(f"Magic {gon_side}-gon ring is impossible") + + +def generate_gon_ring(gon_side: int, perm: list[int]) -> list[int]: + """ + Generate a gon_side-gon ring from a permutation state + The permutation state is the ring, but every duplicate is removed + + >>> generate_gon_ring(3, [4, 2, 3, 5, 1, 6]) + [4, 2, 3, 5, 3, 1, 6, 1, 2] + >>> generate_gon_ring(5, [6, 5, 4, 3, 2, 1, 7, 8, 9, 10]) + [6, 5, 4, 3, 4, 2, 1, 2, 7, 8, 7, 9, 10, 9, 5] + """ + result = [0] * (gon_side * 3) + result[0:3] = perm[0:3] + perm.append(perm[1]) + + # idk why, but it works + magic_number = 1 if gon_side < 5 else 2 + + for i in range(1, len(perm) // 3 + magic_number): + result[3 * i] = perm[2 * i + 1] + result[3 * i + 1] = result[3 * i - 1] + result[3 * i + 2] = perm[2 * i + 2] + + return result + + +def is_magic_gon(numbers: list[int]) -> bool: + """ + Check if the solution set is a magic n-gon ring + Check that the first number is the smallest number on the outer ring + Take a list, and check if the sum of each 3 numbers chunk is equal to the same total + + >>> is_magic_gon([4, 2, 3, 5, 3, 1, 6, 1, 2]) + True + >>> is_magic_gon([4, 3, 2, 6, 2, 1, 5, 1, 3]) + True + >>> is_magic_gon([2, 3, 5, 4, 5, 1, 6, 1, 3]) + True + >>> is_magic_gon([1, 2, 3, 4, 5, 6, 7, 8, 9]) + False + """ + if len(numbers) % 3 != 0: + raise ValueError("a gon ring should have a length that is a multiple of 3") + + if min(numbers[::3]) != numbers[0]: + return False + + total = sum(numbers[:3]) + + return all(sum(numbers[i : i + 3]) == total for i in range(3, len(numbers), 3)) + + +if __name__ == "__main__": + print(solution(4)) From 49415d0c8bc2b585f6f2dbb6d0376312b8eeea75 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 23 Oct 2021 11:58:06 +0000 Subject: [PATCH 3/5] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index ebe353fb5bcd..8933410a14f1 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -756,6 +756,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_065/sol1.py) * Problem 067 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_067/sol1.py) + * Problem 068 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_068/sol1.py) * Problem 069 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_069/sol1.py) * Problem 070 From 0d75b78c85614b7a64cda04187c64c2dd86c2f48 Mon Sep 17 00:00:00 2001 From: kugiyasan Date: Sat, 23 Oct 2021 14:41:28 -0400 Subject: [PATCH 4/5] Project Euler 68 Fixed doctests, now at 93% coverage --- project_euler/problem_068/sol1.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_068/sol1.py b/project_euler/problem_068/sol1.py index c334eb274db5..9a96a1cfc747 100644 --- a/project_euler/problem_068/sol1.py +++ b/project_euler/problem_068/sol1.py @@ -52,10 +52,13 @@ def solution(gon_side: int = 5) -> int: >>> solution(3) 432621513 - >>> solution(4) + >>> solution(4) 426561813732 >>> solution() 6531031914842725 + >>> solution(6) + Traceback (most recent call last): + ValueError: gon_side must be in the range [3, 5] """ if gon_side < 3 or gon_side > 5: raise ValueError("gon_side must be in the range [3, 5]") @@ -112,6 +115,9 @@ def is_magic_gon(numbers: list[int]) -> bool: True >>> is_magic_gon([1, 2, 3, 4, 5, 6, 7, 8, 9]) False + >>> is_magic_gon([1]) + Traceback (most recent call last): + ValueError: a gon ring should have a length that is a multiple of 3 """ if len(numbers) % 3 != 0: raise ValueError("a gon ring should have a length that is a multiple of 3") @@ -125,4 +131,4 @@ def is_magic_gon(numbers: list[int]) -> bool: if __name__ == "__main__": - print(solution(4)) + print(solution()) From 46332924f2ee03af3d555c64d2bdf6c52bc940b4 Mon Sep 17 00:00:00 2001 From: John Law Date: Fri, 27 May 2022 03:17:57 +0800 Subject: [PATCH 5/5] Update sol1.py --- project_euler/problem_068/sol1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_068/sol1.py b/project_euler/problem_068/sol1.py index 9a96a1cfc747..772be359f630 100644 --- a/project_euler/problem_068/sol1.py +++ b/project_euler/problem_068/sol1.py @@ -90,7 +90,6 @@ def generate_gon_ring(gon_side: int, perm: list[int]) -> list[int]: result[0:3] = perm[0:3] perm.append(perm[1]) - # idk why, but it works magic_number = 1 if gon_side < 5 else 2 for i in range(1, len(perm) // 3 + magic_number):