Print( "%s\n%s\n%s\n%s\n%s\n\n" % tuple(x))Ĭan you tell the complexity order of this code? If you don’t know what I’m taking about, you should definitively see my book Computer Science Distilled. Now all we need to do is to try to assemble a perfect square using the viable words: squares =
Rev = lambda w: "".join(reversed( list(w))) To filter words that don’t have a valid reverse, let’s put our words in a set, and check if every word’s reverse is in it. For instance, the word stop is a candidate, because the word pots is also a valid word in English, whereas the word coder isn’t, because redoc doesn’t exist in the English language. A magic square can only be assembled with words of 5 characters, so let’s filter our words: words = filter( lambda x: len( x) = 5, words)Ī word can only fit in the square if it’s backwards read is also in the square. Now words is a list with all words in the English language.
#Magic square generator algorithm free
That’s an easy task, using a free wordlist from the internet: import urllib. Here’s how it works.įirst, we need to get all the English words with five letters. So I wrote a little Python program to do it. With computers, finding one should be a trivial task. After discovering its existence, I kept asking myself if such a perfect 5×5 square of English words existed. It probably must have taken Romans either a strike of luck, or an incredible amount of work to discover such a square of latin words. Many believed the perfectly symmetric grid had the power to repel bad spirits and bring good luck. Ancient Romans were so amazed by this, that they often wrote this word-grid into houses, temples, monuments, drinking vessels. That’s the ultimate palindromic wordplay. What’s amazing about this square is that you can can read its words top-to-bottom, and also bottom-to-top, left-to-right and right-to-left: It’s the latin expression sator arepo tenet opera rotas. This one was very famous in ancient roman times: Ancient Sator Square inscription. Try reading these sentences backwards: Live not on evil.īut there is another word play that is even more impressive: a magic word square. You can read the letters in these sentences forwards of backwards, and they’ll still form the same words. You might have heard of palindromic sentences.