MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / is_magic_gon

Function is_magic_gon

project_euler/problem_068/sol1.py:104–130  ·  view source on GitHub ↗

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,

(numbers: list[int])

Source from the content-addressed store, hash-verified

102
103
104def is_magic_gon(numbers: list[int]) -> bool:
105 """
106 Check if the solution set is a magic n-gon ring
107 Check that the first number is the smallest number on the outer ring
108 Take a list, and check if the sum of each 3 numbers chunk is equal to the same total
109
110 >>> is_magic_gon([4, 2, 3, 5, 3, 1, 6, 1, 2])
111 True
112 >>> is_magic_gon([4, 3, 2, 6, 2, 1, 5, 1, 3])
113 True
114 >>> is_magic_gon([2, 3, 5, 4, 5, 1, 6, 1, 3])
115 True
116 >>> is_magic_gon([1, 2, 3, 4, 5, 6, 7, 8, 9])
117 False
118 >>> is_magic_gon([1])
119 Traceback (most recent call last):
120 ValueError: a gon ring should have a length that is a multiple of 3
121 """
122 if len(numbers) % 3 != 0:
123 raise ValueError("a gon ring should have a length that is a multiple of 3")
124
125 if min(numbers[::3]) != numbers[0]:
126 return False
127
128 total = sum(numbers[:3])
129
130 return all(sum(numbers[i : i + 3]) == total for i in range(3, len(numbers), 3))
131
132
133if __name__ == "__main__":

Callers 1

solutionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected