0
$\begingroup$
moorse_string[] = {".-",      //  0 = A ('A' = ASCII code 65; 'a' = ASCII code 97)
                       "-...",    //  1 = B
                       "-.-.",    //  2 = C
                       "-..",     //  3 = D
                       ".",       //  4 = E
                       "..-.",    //  5 = F
                       "--.",     //  6 = G
                       "....",    //  7 = H
                       "..",      //  8 = I
                       ".---",    //  9 = J
                       "-.-",     // 10 = K
                       ".-..",    // 11 = L
                       "--",      // 12 = M
                       "-.",      // 13 = N
                       "---",     // 14 = O
                       ".--.",    // 15 = P
                       "--.-",    // 16 = Q
                       ".-.",     // 17 = R
                       "...",     // 18 = S
                       "-",       // 19 = T
                       "..-",     // 20 = U
                       "...-",    // 21 = V
                       ".--",     // 22 = W
                       "-..-",    // 23 = X
                       "-.--",    // 24 = Y
                       "--..",    // 25 = Z
                       "-----",   // 26 = 0 (ASCII code 48)
                       ".----",   // 27 = 1
                       "..---",   // 28 = 2
                       "...--",   // 29 = 3
                       "....-",   // 30 = 4
                       ".....",   // 31 = 5
                       "-....",   // 32 = 6
                       "--...",   // 33 = 7
                       "---..",   // 33 = 8
                       "----.",   // 34 = 9
                       "--..--",  // 36 = , (ASCII code 44)
                       ".-.-.-"   // 37 = . (ASCII code 46)
                      }

Only thing I can think of is to match each string of code with the array of strings above until a match is found. I want to think of a better way to decode this without brute force. For example, if I have "..---". This is located at 28 index of the array. and My code now looks from 0 - 28 and finds a match and breaks. Please suggest better ideas thanks.

I want to handle following list of chars only: enter image description here

and Morse encodings within a word are separated by a single space. Spaces between words and new lines are separated by three spaces.

Answer does not have to be code, but codes are accepted. Answer can be any valid suggestions that explain the logic wholly in words(atleast). I just want to explore different way of thinking. Any answer that is different from what I suggested will be accepted even if it is a worse algorithm. But if there is 2 answers then the more elegant one will be accepted. I am not asking for the solution but ideas. How would you approach the problem? and why? What is the logic that drives the algorithm? If you consider you way better than what I suggested then why?

$\endgroup$
5
  • $\begingroup$ Welcome to CS.SE! I think we need to understand what criteria you have for the algorithm. We need something clearer than "I do not like this" and "better" and "good", requirements or criteria that can be objectively evaluated, so that answerers can know in advance what you will find satisfying and voters can know what criteria to use for voting. To me, the algorithm you rejected looks perfectly good. $\endgroup$
    – D.W.
    Oct 6, 2022 at 17:09
  • $\begingroup$ I do not have any criteria. I just want to learn. I am looking for some thing more elegant or just different from what I suggested. I guess I just want new some ideas. $\endgroup$
    – Pragmatic
    Oct 6, 2022 at 17:19
  • $\begingroup$ what I suggested cannot be the only way of doing it. So, any new ideas are really appreciated. $\endgroup$
    – Pragmatic
    Oct 6, 2022 at 17:35
  • $\begingroup$ Have a look at tries. $\endgroup$
    – greybeard
    Oct 6, 2022 at 17:44
  • $\begingroup$ Thanks - tries is an interesting suggestion. $\endgroup$
    – Pragmatic
    Oct 6, 2022 at 17:50

2 Answers 2

0
$\begingroup$

You could build a tree where each node has a name and two children ("Dot" and "Dash").

For --., you'd:

  • Look at the Dash child of the tree's root node.
  • Go to its Dash child.
  • Then go to that node's Dot child.
  • Then take the name of the node "G".

For --.-, you'd do the same thing, but instead of taking the "G", you go to "Dash" and then find that it's "Q".

This is basically the way that people process the code and anticipate the target letter while they are listening to it; they get the --., and wait for the next sound to determine whether it is a "G", or perhaps a "Q", "Z", "7", or ",".

$\endgroup$
6
  • $\begingroup$ I believe this is the same as a trie. $\endgroup$
    – D.W.
    Oct 7, 2022 at 4:21
  • $\begingroup$ @greybeard, yes, that was an editing mistake. Thanks for pointing it out. (Originally, the terminal and non-terminal nodes were different, then I realized that it wasn't necessary.) $\endgroup$ Oct 7, 2022 at 13:36
  • $\begingroup$ @RayButterworth that is great suggestion. Also the algo is clear in words for anyone to read in the future. Thanks $\endgroup$
    – Pragmatic
    Oct 7, 2022 at 14:11
  • $\begingroup$ Another thing I found is hash-tables $\endgroup$
    – Pragmatic
    Oct 7, 2022 at 14:42
  • $\begingroup$ @Pragmatic, yes, but for something this small hash-tables probably aren't worth it. ¶ Whatever you do, write your program so that it doesn't know what the underlying algorithm is. E.g. it should use something like character = MorseToRoman("...-") and know that it will work regardless of the algorithm. That allows the algorithm to be completely changed without having to make any changes to anything that uses it. $\endgroup$ Oct 7, 2022 at 15:04
2
$\begingroup$

What you suggest is already a good method. It is what I would recommend. It is simple and easy to implement. That is a good thing. Generally, those qualities mean that it takes less time for the programmer to implement, and is less likely to have subtle bugs. Normally, as a first starting point, we want to optimize for reducing programmer time, even if it takes the computer a little longer, because the programmer's time is far more expensive than the computer's. Only if the resulting code is too slow do we begin to examine more sophisticated algorithms and data structures.

Here are two pithy quotes I like that capture these sentiments well:

That said, if you want a fancier data structure, a trie would be appropriate. It might yield better performance (or it might not, depending on memory hierarchy and cache effects).

$\endgroup$
1
  • $\begingroup$ Agreed (+1). Almost always, any processing that is linearly proportional to the size of the input or output doesn't need optimizing. Obvious and easy to change code should be the main priority. $\endgroup$ Oct 7, 2022 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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