dit – dah | morse code *

https://challenges.wolframcloud.com/challenge/write-in-morse-code

Write in Morse Code

Morse code is a way of transmitting text through a series of unique sounds or lights represented by dots and dashes, represented by "." and "-". Write a function that takes an English text and outputs the corresponding Morse code.

One famous piece of Morse code is "… — …", a distress signal for "SOS" that stands for "Save Our Souls".

What Your Function Should Do

Write a function ToMorseCode that takes a string of English text and outputs the Morse code translation. Use "/" to separate words.

In[1]:=

ToMorseCode["This is not a pipe"]

Out[1]=- .... .. ... / .. ... / -. --- - / .- / .--. .. .--. .

In[2]:=

ToMorseCode["Wolfram Challenges"]

Out[2]=.-- --- .-.. ..-. .-. .- -- / -.-. .... .- .-.. .-.. . -. --. . ...

To avoid ambiguity, separate each character with a single whitespace character:

In[3]:=

ToMorseCode["use spaces"]

Out[3]=..- ... . / ... .--. .- -.-. . ...

Here are all positions of whitespace characters in the previous example:

In[4]:=

![StringPosition[%, " "][[All, 1]]](https://www.wolframcloud.com/objects/wolframchallenges/public/nb/export/WriteInMorseCode/HTMLFiles/MoreDetails_7.gif)

Out[4]={4, 8, 10, 12, 16, 21, 24, 29, 31}

Treat uppercase and lowercase characters the same:

In[5]:=

ToMorseCode["SAME"]

Out[5]=... .- -- .

In[6]:=

ToMorseCode["same"]

Out[6]=... .- -- .

More Examples

A long piece of text:

In[7]:=

ToMorseCode["When in the Course of human events, it becomes necessary  for one people to dissolve the political bands which have connected  them with another, and to assume, among the Powers of the earth, the  separate and equal station to which the Laws of Nature and of  Nature's God entitle them, a decent respect to the opinions of  mankind requires that they should declare the causes which impel them  to the separation."]

Out[7]=.-- .... . -. / .. -. / - .... . / -.-. --- ..- .-. ... . / --- ..-. / .... ..- -- .- -. / . ...- . -. - ... ,/ .. - / -... . -.-. --- -- . ... / -. . -.-. . ... ... .- .-. -.-- / ..-. --- .-. / --- -. . / .--. . --- .--. .-.. . / - --- / -.. .. ... ... --- .-.. ...- . / - .... . / .--. --- .-.. .. - .. -.-. .- .-.. / -... .- -. -.. ... / .-- .... .. -.-. .... / .... .- ...- . / -.-. --- -. -. . -.-. - . -.. / - .... . -- / .-- .. - .... / .- -. --- - .... . .-. ,/ .- -. -.. / - --- / .- ... ... ..- -- . ,/ .- -- --- -. --. / - .... . / .--. --- .-- . .-. ... / --- ..-. / - .... . / . .- .-. - .... ,/ - .... . / ... . .--. .- .-. .- - . / .- -. -.. / . --.- ..- .- .-.. / ... - .- - .. --- -. / - --- / .-- .... .. -.-. .... / - .... . / .-.. .- .-- ... / --- ..-. / -. .- - ..- .-. . / .- -. -.. / --- ..-. / -. .- - ..- .-. . '... / --. --- -.. / . -. - .. - .-.. . / - .... . -- ,/ .- / -.. . -.-. . -. - / .-. . ... .--. . -.-. - / - --- / - .... . / --- .--. .. -. .. --- -. ... / --- ..-. / -- .- -. -.- .. -. -.. / .-. . --.- ..- .. .-. . ... / - .... .- - / - .... . -.-- / ... .... --- ..- .-.. -.. / -.. . -.-. .-.. .- .-. . / - .... . / -.-. .- ..- ... . ... / .-- .... .. -.-. .... / .. -- .--. . .-.. / - .... . -- / - --- / - .... . / ... . .--. .- .-. .- - .. --- -. .

Things You May Find Useful

The following list, morseAlphabet, is a list of the letters of the Morse code alphabet, in alphabetical order (a–z):

In[8]:=

morseAlphabet = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.",     "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.",     "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--",     "--.."};

DownloadOpen in Cloud

Categories: Start HereWords and Linguistics

Contributed by: Wolfram Research

 

 

 

image-20240102160849062

Scroll to Top