3
$\begingroup$

I wrote a code for converting a string into a Morse code.

convert[text_String] := 
  StringJoin[
    Map[
      Replace[
        {"a" -> ".- ", "b" -> "-... ", "c" -> "-.-. ", 
         "d" -> "-.. ", "e" -> ". ", "f" -> "..-. ", "g" -> "--. ", 
         "h" -> ".... ", "i" -> ".. ", "j" -> ".--- ", "k" -> "-.- ", 
         "l" -> ".-.. ", "m" -> "-- ", "n" -> "-. ", "o" -> "--- ", 
         "p" -> ".--. ", "q" -> "--.- ", "r" -> ".-. ", "s" -> "... ", 
         "t" -> "- ", "u" -> "..- ", "v" -> "...- ", "w" -> ".-- ", 
         "x" -> "-..- ", "y" -> "-.-- ", "z" -> "--.. ", " " -> "/ "}], 
      Characters[text]]]

Example

convert["a ladybug loves sleeping"]

gives

.- / .-.. .- -.. -.-- -... ..- --. / .-.. --- ...- . ... / ... .-.. . . .--. .. -. --.

I'm getting the correct output. However, when I coded my list of rules, I typed in every rule ("a"->".- ", "b"->"-... ", etc.) individually. I am wondering, how could I produce a set of rules in a more efficient way. Say I had a thousand of rules to create from two existing lists, making a list of rules from the two list by hand would be too tedious.

$\endgroup$

closed as unclear what you're asking by m_goldberg, garej, yohbs, MarcoB, Mr.Wizard Jun 7 '17 at 13:46

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ Thread[list1->list2]? $\endgroup$ – Kuba Jun 6 '17 at 18:40
  • $\begingroup$ I believe your question is something like: "If I had a really large alphabet how could I automate this list creating process?" Well, if I understood you correctly, the morse code is a convention, so no easy way for that. But if you accept using binary instead of morse things get interesting. $\endgroup$ – ivbc Jun 6 '17 at 19:58
  • 1
    $\begingroup$ Incidentally this should be written using StringReplace as it will be much faster, i.e. convert[text_String] := StringReplace[text, { (* rules *) }] $\endgroup$ – Mr.Wizard Jun 8 '17 at 7:00
5
$\begingroup$

I think this is what you are looking for:
Given that you already have your lists

morseAlphabet = {"·−","−···","−·−·","−··","·","··−·","−−·","····","··","·−−−","−·−","·−··","−−","−·","−−−","·−−·","−−·−","·−·","···","−","··−","···−","·−−","−··−","−·−−","−−··"}

and

alphabet = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};

You can use

Thread[alphabet -> morseAlphabet]

to create your replacement rules.

StringJoin[Characters["A LADYBUG LOVES SLEEPING"] /.Thread[alphabet -> morseAlphabet]]

Would then be equivalent to your function convert[text_String]

$\endgroup$
  • $\begingroup$ Yes, that was the function I was looking for! Thanks! $\endgroup$ – Nigi Tanka Jun 7 '17 at 21:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.