A palindrome is a observe or sentence that stays the identical when its characters are reversed. Let’s allege, the observe “radar” is a palindrome, as is the sentence “Madam, I’m Adam.”
I modified into thinking as of late about Morse code palindromes, sequences of Morse code that dwell the identical when reversed.
This submit will gawk at what it procedure for a letter or a observe to be a palindrome in Morse code, then gawk at palindrome sentences in Morse code, then in some procedure gawk at a shell script to fetch Morse palindromes.
Letters and words
Some particular particular person letters are palindromes in Morse code, reminiscent of I (
..) and P (
Some letters alternate into barely barely just a few letters when their Morse code illustration is reversed. Let’s allege B (
-...) becomes V (
...-) and vice versa.
The letters C (
-.-.), J (
.---), and Z (
--..) when reversed are now not fragment of the 26-letter Roman alphabet, though the reversed sequences are in most cases ragged for vowels with umlauts: Ä (
.-.-), Ö (
---.), and Ü (
The sequence SOS (
... --- ...) is a palindrome in English and in Morse code. However some words are palindromes in Morse code that are now not palindromes in English, reminiscent of “gnaw,” which is
--. -. .- .--
in Morse code.
The longest observe I’ve found which is a palindrome in Morse code is “footstool.”
..-. --- --- - ... - --- --- .-.. .-..
I wrote some code to search a dictionary and assemble a listing of English words that dwell English words when converted to Morse code, reversed, and grew to turn into succor into textual announce. There aren’t that many, round 240. Then I hunted for ways to assemble sentences out of those words.
Let’s allege, “Trevor sees Robert” is a palindrome in Morse code:
- .-. . ...- --- .-. ... . . ... .-. --- -... . .-. -
Whenever you occur to’d cherish to take a look at out your hand at this, you might well additionally fetch a couple files precious. This file provides a listing of words that dwell the identical when their Morse code is reversed, reminiscent of “outdo” (
--- ..- - -.. ---) and this file provides a listing of transformation pairs, reminiscent of “waft” (
... .- .. .-..) and “fins” (
..-. .. -. ...).
Conceptually we are looking to write out words in Morse code, reverse the sequence of dots and dashes, and flip the result succor into English textual announce. However we are going to have the option to end this without with no doubt working with Morse code.
We can reverse the letters in the enter, then replace every letter with the letter a lot like reversing its Morse code.
I don’t know of an straightforward technique to reverse a string in a shell script, but I end understand how to complete it with a Perl one-liner.
perl -lne 'print scalar reverse'
Next we should flip round the dots and dashes of particular particular person letters. Most letters bear the identical, but there are six pairs of letters to swap:
- (A, N)
- (B, V)
- (D, U)
- (F, L)
- (G, W)
- (Q, Y)
tr (“translate”) utility modified into made for this manufacture of project, replacing all characters in one string with their counterparts in one more.
tr ABDFGQNVULWY NVULWYABDFGQ
tr effectively does the full translations at the identical time. Let’s allege, 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, and so forth., it would now not end what we desire. Let’s allege, AN would first turn into NN after which AA.
Striking these collectively, the next one-liner proves that “footstool” is a palindrome in Morse code
echo FOOTSTOOL | perl -lne 'print scalar reverse' | tr ABDFGQNVULWY NVULWYABDFGQ
for the explanation that output is “FOOTSTOOL”.
Perl has a
tr characteristic very mighty cherish the shell utility, so we would maybe additionally end more of the work in Perl:
echo FOOTSTOOL | perl -lne "tr /ABDFGQNVULWY/NVULWYABDFGQ/; print scalar reverse"