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

Function generate_gon_ring

project_euler/problem_068/sol1.py:80–101  ·  view source on GitHub ↗

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

(gon_side: int, perm: list[int])

Source from the content-addressed store, hash-verified

78
79
80def generate_gon_ring(gon_side: int, perm: list[int]) -> list[int]:
81 """
82 Generate a gon_side-gon ring from a permutation state
83 The permutation state is the ring, but every duplicate is removed
84
85 >>> generate_gon_ring(3, [4, 2, 3, 5, 1, 6])
86 [4, 2, 3, 5, 3, 1, 6, 1, 2]
87 >>> generate_gon_ring(5, [6, 5, 4, 3, 2, 1, 7, 8, 9, 10])
88 [6, 5, 4, 3, 4, 2, 1, 2, 7, 8, 7, 9, 10, 9, 5]
89 """
90 result = [0] * (gon_side * 3)
91 result[0:3] = perm[0:3]
92 perm.append(perm[1])
93
94 magic_number = 1 if gon_side < 5 else 2
95
96 for i in range(1, len(perm) // 3 + magic_number):
97 result[3 * i] = perm[2 * i + 1]
98 result[3 * i + 1] = result[3 * i - 1]
99 result[3 * i + 2] = perm[2 * i + 2]
100
101 return result
102
103
104def is_magic_gon(numbers: list[int]) -> bool:

Callers 1

solutionFunction · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected