What word is missing at the center of the rightmost triangle?
The word we were looking for is FIREPLACE.
In each triangle, all words at the vertices are the same length. The word at the center of the triangle is the same length as the words at the vertices. It is found by starting at a vertex (the vertex at the top of the triangle with a wider border in both cases), traversing the vertices in a certain direction (clockwise in both cases) and always taking the letter at the next position in each consecutive word (the first letter of the first word, the second letter of the second word, and so on). We have marked the letters that spell the center word in red.
However, the puzzles are not only restricted to triangles: polygons with more than three vertices are possible as well. Moreover, we do not necessarily have to start at the top vertex. For example, if we start at the bottom vertex of the square below and run clockwise through the vertices, we get the word CHRISTMAS. We may also have to run counterclockwise through the words at the vertices. For example, if we start at the bottom right vertex of the pentagon below and run counterclockwise through the vertices, the word SANTACLAUS is spelled.
A polygon is a puzzle in which a sequence of $$m$$-letter words are placed at the $$n$$ vertices of a polygon. The puzzle is represented as a sequence (list or tuple)
that contains at least three elements ($$n \geq 3$$)
whose elements are strings (str) that represent the words at the vertices (listed in clockwise order)
whose elements only contain uppercase letters
whose elements are the same length
A candidate solution of a puzzle is an $$m$$-letter word (str) that is found by starting at a vertex, traversing the vertices in a certain direction (clockwise or counterclockwise) and always taking the letter at the next position in each consecutive word. Your task:
Write a function ispolygon that takes a single argument $$p$$. The function must return a Boolean value (bool) that indicates if $$p$$ represents a valid polygon.
Write a function solution that takes a single argument $$p$$. If $$p$$ does not represent a valid polygon, an AssertionError must be raised with the message invalid polygon. Otherwise, the function must return a candidate solution of polygon $$p$$, taking into account two optional parameters of the function:
start (int; default value: 0): index of the start vertex of polygon $$p$$
clockwise (bool; default value: True): direction in which the words of polygon $$p$$ must be iterated: clockwise (True) or counterclockwise (False)
Write a function solutions that takes a single argument $$p$$. If $$p$$ does not represent a valid polygon, an AssertionError must be raised with the message invalid polygon. Otherwise, the function must return a set containing all candidate solutions of polygon $$p$$. These are the candidate solutions where each vertex can act as a starting point and were vertices can be traversed both clockwise and counterclockwise.
The function also has an optional parameter clockwise that may take a Boolean value (bool). If the value True is explicitly passed to the parameter clockwise, only the candidate solutions that are found by traversing the vertices clockwise must be returned. If the value False is explicitly passed to the parameter clockwise, only the candidate solutions that are found by traversing the vertices counterclockwise must be returned.
>>> ispolygon(['DISEASES', 'RENUMBER', 'SOCIABLE'])
True
>>> ispolygon('FREELOADS, TIMEPIECE, NORMALIZE')
False
>>> ispolygon({'CONSULTANT', 'CAPITALIZE', 'KETTLEDRUM', 'SUBSECTION', 'LOOKALIKES'})
False
>>> ispolygon(('JAWBREAKERS', 2.833, 'CONSEQUENCE', 'FRIGHTFULLY', 'UNWILLINGLY'))
False
>>> solution(['DISEASES', 'RENUMBER', 'SOCIABLE'])
'DECEMBER'
>>> solution(('FREELOADS', 'TIMEPIECE', 'NORMALIZE'))
'FIREPLACE'
>>> solution(['PERFORMER', 'ANTIVIRAL', 'CROSSBOWS', 'PHENOTYPE'], start=2)
'CHRISTMAS'
>>> solution(('KETTLEDRUM', 'CONSULTANT', 'CAPITALIZE', 'SUBSECTION', 'LOOKALIKES'), clockwise=False, start=3)
'SANTACLAUS'
>>> solutions(['DISEASES', 'RENUMBER', 'SOCIABLE'])
{'DECEMBER', 'DONEABEE', 'RICUABES', 'ROSUASEE', 'SESIMSLR', 'SINIABLS'}
>>> solutions(['FREELOADS', 'TIMEPIECE', 'NORMALIZE'], clockwise=True)
{'FIREPLACE', 'NRMMLIIDE', 'TOEEAOEZS'}
>>> solutions(['PERFORMER', 'ANTIVIRAL', 'CROSSBOWS', 'PHENOTYPE'], clockwise=True)
{'AREFVBYEL', 'CHRISTMAS', 'PETSORRWE', 'PNONOIOPR'}
>>> solutions(['KETTLEDRUM', 'CONSULTANT', 'CAPITALIZE', 'SUBSECTION', 'LOOKALIKES'], clockwise=False)
{'CEOSTLDKOE', 'COTKEATREN', 'KOBIUEIIZT', 'LUPSLLTINM', 'SANTACLAUS'}
A hexagonal puzzle with an extra tip.