{"record":{"id":"c93baf732d033048","repo":"TheAlgorithms/Python","slug":"a-gon-ring-should-have-a-length-that-is-a-multiple","errorCode":null,"errorMessage":"a gon ring should have a length that is a multiple of 3","messagePattern":"a gon ring should have a length that is a multiple of 3","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"project_euler/problem_068/sol1.py","lineNumber":123,"sourceCode":"    \"\"\"\n    Check if the solution set is a magic n-gon ring\n    Check that the first number is the smallest number on the outer ring\n    Take a list, and check if the sum of each 3 numbers chunk is equal to the same total\n\n    >>> is_magic_gon([4, 2, 3, 5, 3, 1, 6, 1, 2])\n    True\n    >>> is_magic_gon([4, 3, 2, 6, 2, 1, 5, 1, 3])\n    True\n    >>> is_magic_gon([2, 3, 5, 4, 5, 1, 6, 1, 3])\n    True\n    >>> is_magic_gon([1, 2, 3, 4, 5, 6, 7, 8, 9])\n    False\n    >>> is_magic_gon([1])\n    Traceback (most recent call last):\n    ValueError: a gon ring should have a length that is a multiple of 3\n    \"\"\"\n    if len(numbers) % 3 != 0:\n        raise ValueError(\"a gon ring should have a length that is a multiple of 3\")\n\n    if min(numbers[::3]) != numbers[0]:\n        return False\n\n    total = sum(numbers[:3])\n\n    return all(sum(numbers[i : i + 3]) == total for i in range(3, len(numbers), 3))\n\n\nif __name__ == \"__main__\":\n    print(solution())\n","sourceCodeStart":105,"sourceCodeEnd":135,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/project_euler/problem_068/sol1.py#L105-L135","documentation":"Raised by is_magic_gon() in project_euler/problem_068/sol1.py when the numbers list length is not a multiple of 3. The gon ring is represented as triplets (outer, inner_a, inner_b) per side, so the list must have exactly 3 * gon_side entries; any other length makes the slicing logic meaningless and is rejected.","triggerScenarios":"is_magic_gon([1]) (length 1), is_magic_gon([1,2,3,4]) (length 4), or building a 4-gon ring but passing a 9-element list (should be 12). Passing a flat permutation of 2*gon_side numbers instead of the expanded 3*gon_side ring also triggers it.","commonSituations":"Calling is_magic_gon directly with raw permutations instead of generate_gon_ring output; changing gon_side but reusing a hardcoded ring list; off-by-one when slicing concatenated triplets.","solutions":["Pass generate_gon_ring(gon_side, perm) output, which is always 3*gon_side long.","Check length before calling: assert len(numbers) == 3 * gon_side.","If building rings manually, append exactly three entries per side."],"exampleFix":"# before\nnumbers = list(perm)  # 2*gon_side flat permutation\nis_magic_gon(numbers)  # ValueError\n\n# after\nnumbers = generate_gon_ring(gon_side, list(perm))\nis_magic_gon(numbers)","handlingStrategy":"validation","validationCode":"ring = generate_gon_ring(gon_side, list(perm))\nassert len(ring) == 3 * gon_side\nok = is_magic_gon(ring)","typeGuard":"def is_gon_ring_shaped(numbers, gon_side) -> bool:\n    return isinstance(numbers, list) and len(numbers) == 3 * gon_side","tryCatchPattern":"try:\n    ok = is_magic_gon(numbers)\nexcept ValueError:\n    logger.error(\"ring length %d not a multiple of 3\", len(numbers))\n    raise","preventionTips":["Only pass generate_gon_ring output to is_magic_gon.","Remember the ring encoding is 3 entries per side (outer, inner, inner).","Length-check before calling when constructing rings by hand."],"tags":["project-euler","validation","valueerror","data-format"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}