Visually Symmetric Words
Published on September 16, 2023 words symmetryI read
a post about visually symmetric words a while back and decided
to write a program to automatically find them.
The code can be found on on github at mark-veres/vis-sym.
I began by finding all visually symmetrical letters and ended up with this list:
symmetric = ['i', 'l', 'm', 'o', 'u', 'v', 'w', 'x', 'y']
I created a list that contained pairs of mirrored letters.
mirrors = [('p', 'q'), ('d', 'b'), ('q', 'p'), ('b', 'd')]
for s in symmetric:
mirrors.append((s, s))
The is_symmetric
function takes a word, splits it
in two parts (and optionally the character in the middle of the
word) and checks if the letters of the words are mirrors or
visually symmetric.
For a word to be visually symmetric:
middle
must be symmetric- every letter in the first half must be equal to or a mirror of his opposite
def is_symmetric(word):
if len(word)%2 == 0:
first_half = word[:int(len(word)/2)]
second_half = word[int(len(word)/2):] [::-1]
else:
first_half = word[:int((len(word)-1)/2)]
second_half = word[int((len(word)+1)/2):] [::-1]
middle = word[int((len(word)-1)/2)]
if middle not in symmetric:
return False
for i, char in enumerate(first_half):
opposite = second_half[i]
if (char, opposite) not in mirrors:
return False
return True
The [::-1]
expression is used to reverse the
second_half
string, to make the comparing of
characters easier.
The final just takes words from a dictionary and checks if
they’re visually symmetric with the is_symmetric
function.