A palindrome is a word or sentence that remains the same when its characters are reversed. For example, the word “radar” is a palindrome, as is the sentence “Madam, I’m Adam.”
I was thinking today about Morse code palindromes, sequences of Morse code that remain the same when reversed.
This post will look at what it means for a letter or a word to be a palindrome in Morse code, then look at palindrome sentences in Morse code, then finally look at a shell script to find Morse palindromes.
Letters and words
Some individual letters are palindromes in Morse code, such as I (
..) and P (
Some letters change into other letters when their Morse code representation is reversed. For example B (
-...) becomes V (
...-) and vice versa.
The letters C (
-.-.), J (
.---), and Z (
--..) when reversed are no longer part of the 26-letter Roman alphabet, though the reversed sequences are sometimes used for vowels with umlauts: Ä (
.-.-), Ö (
---.), and Ü (
The sequence SOS (
... --- ...) is a palindrome in English and in Morse code. But some words are palindromes in Morse code that are not palindromes in English, such as “gnaw,” which is
--. -. .- .--
in Morse code.
The longest word I’ve found which is a palindrome in Morse code is “footstool.”
..-. --- --- - ... - --- --- .-..
I wrote some code to search a dictionary and make a list of English words that remain English words when converted to Morse code, reversed, and turned back into text. There aren’t that many, around 240. Then I looked for ways to make sentences out of these words.
For example, “Trevor sees Robert” is a palindrome in Morse code:
- .-. . ...- --- .-. ... . . ... .-. --- -... . .-. -
If you’d like to try your hand at this, you might find a couple files useful. This file gives a list of words that remain the same when their Morse code is reversed, such as “outdo” (
--- ..- - -.. ---) and this file gives a list of transformation pairs, such as “sail” (
... .- .. .-..) and “fins” (
..-. .. -. ...).
Conceptually we want to write out words in Morse code, reverse the sequence of dots and dashes, and turn the result back into English text. But we can do this without actually working with Morse code.
We can reverse the letters in the input, then replace each letter with the letter corresponding to reversing its Morse code.
I don’t know of an easy way to reverse a string in a shell script, but I do know how to do it with a Perl one-liner.
perl -lne 'print scalar reverse'
Next we need to turn around the dots and dashes of individual letters. Most letters stay the same, but there are six pairs of letters to swap:
- (A, N)
- (B, V)
- (D, U)
- (F, L)
- (G, W)
- (Q, Y)
tr (“translate”) utility was made for this kind of task, replacing all characters in one string with their counterparts in another.
tr ABDFGQNVULWY NVULWYABDFGQ
tr effectively does all the translations at the same time. For example, it replaces A’s with N’s and N’s with A’s simultaneously. If it simply marched down the two strings, replacing A’s with N’s, then replacing B’s to V’s, etc., it would not do what we want. For example, AN would first become NN and then AA.
Putting these together, the following one-liner proves that “footstool” is a palindrome in Morse code
echo FOOTSTOOL | perl -lne 'print scalar reverse' | tr ABDFGQNVULWY NVULWYABDFGQ
because the output is “FOOTSTOOL”.
Perl has a
tr function very much like the shell utility, so we could do more of the work in Perl:
echo FOOTSTOOL | perl -lne "tr /ABDFGQNVULWY/NVULWYABDFGQ/; print scalar reverse"
Update: A comment from Alastair below let me know you can replace the bit of Perl in the first one-liner with a call to
echo FOOTSTOOL | tac -rs . | tr ABDFGQNVULWY NVULWYABDFGQ
tac lists the lines of a file in reverse order. The name comes from reversing “cat”, the name of the command that dumps a file (“concatenates” it to standard output). The extra arguments to
tac cause it to change the definition of a line separator to any character, as indicated by the regular expression consisting of a single period. This effectively tells
tac to treat every character as a line, so reversing the lines reverses the string.