Skip to content

Instantly share code, notes, and snippets.

@BRonen
Created July 30, 2023 04:10
Show Gist options
  • Select an option

  • Save BRonen/12883945f33243ae53ca18378c01385e to your computer and use it in GitHub Desktop.

Select an option

Save BRonen/12883945f33243ae53ca18378c01385e to your computer and use it in GitHub Desktop.
A script that handles incomplete morse signals and gives the original source possibilities
/**
* Finds the number in an array with the most digits.
*
* @param {string} signals - The incomplete source.
* @returns {string[]} Suggestions of possible complete sources.
*/
const MORSE_DICTIONARY: Record<string, string> = {
".": "E",
"-": "T",
"..": "I",
".-": "A",
"-.": "N",
"--": "M",
"...": "S",
"..-": "U",
".-.": "R",
".--": "W",
"-..": "D",
"-.-": "K",
"--.": "G",
"---": "O"
}
const getPossibillitiesBySignals = (signals: string): string[] => {
if (signals.indexOf('?') === -1)
return [signals];
return [
...getPossibillitiesBySignals(signals.replace('?', '.')),
...getPossibillitiesBySignals(signals.replace('?', '-'))
];
}
const charactersPossibilities = (signals: string): string[] => {
return getPossibillitiesBySignals(signals).map(
possibility => MORSE_DICTIONARY[possibility]
);
};
export default charactersPossibilities;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment