Coding questions · Sealth
Decode Ambiguous Morse Messages
This question evaluates string parsing, pattern matching, and combinatorial decoding skills using a Morse-to-letter mapping, covering both deterministic separator handling and enumeration of ambiguous letter partitions.
You are given a mapping from Morse patterns to lowercase English letters.
Implement two related functions:
-
Deterministic decoding with character and word separators
- Input: a Morse-encoded string, a
char_septhat separates letters inside a word, and aword_septhat separates words. - Output: the decoded sentence.
- Example: if
char_sepseparates letters andword_sepseparates words, decode the full message by splitting on words first and then on characters.
- Input: a Morse-encoded string, a
-
Enumerate all valid decodings when character separators are missing
- Input: a Morse-encoded string that contains only a
word_sepbetween encoded words, but no separator between letters inside each word. - Because each encoded word may be partitioned into letters in multiple valid ways, return all possible decoded sentences as a list or set.
- For each encoded word, generate every valid letter sequence whose Morse concatenation matches that word exactly, then combine the per-word results into full-sentence outputs.
- Input: a Morse-encoded string that contains only a
Assume the Morse-to-letter dictionary is provided as part of the problem.
Decode Morse With Separators
Decode a Morse message using explicit character and word separators.
Examples
Example 1
Input: ('.... . .-.. .-.. --- / .-- --- .-. .-.. -..', ' ', ' / ', {'....':'h','.':'e','.-..':'l','---':'o','.--':'w','.-.':'r','-..':'d'})
Output: 'hello world'
Explanation: Decode with explicit character and word separators.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.
Enumerate Morse Decodings Without Character Separators
Return all decoded sentences when letters inside each word are not separated; output is sorted for deterministic grading.
Examples
Example 1
Input: ('....../.-', '/', {'.':'e','-':'t','....':'h','..':'i','...':'s','.-':'a'})
Output: ['eeeeee a', 'eeeeee et', 'eeeei a', 'eeeei et', 'eeeie a', 'eeeie et', 'eees a', 'eees et', 'eeh a', 'eeh et', 'eeiee a', 'eeiee et', 'eeii a', 'eeii et', 'eese a', 'eese et', 'ehe a', 'ehe et', 'eieee a', 'eieee et', 'eiei a', 'eiei et', 'eiie a', 'eiie et', 'eis a', 'eis et', 'esee a', 'esee et', 'esi a', 'esi et', 'hee a', 'hee et', 'hi a', 'hi et', 'ieeee a', 'ieeee et', 'ieei a', 'ieei et', 'ieie a', 'ieie et', 'ies a', 'ies et', 'ih a', 'ih et', 'iiee a', 'iiee et', 'iii a', 'iii et', 'ise a', 'ise et', 'seee a', 'seee et', 'sei a', 'sei et', 'sie a', 'sie et', 'ss a', 'ss et']
Explanation: Enumerate all decodings for each separator-delimited word.
Example 2
Input: ('', '/', {'.':'e'})
Output: ['']
Explanation: Empty encoded input has one empty sentence.
Hints
- Clarify edge cases before coding.
- Keep the return value deterministic.