Skip to content

Instantly share code, notes, and snippets.

@syed-ahmad
Created October 6, 2023 08:23
Show Gist options
  • Save syed-ahmad/27b05cae9c9577a40ef85a820a54a5d3 to your computer and use it in GitHub Desktop.
Save syed-ahmad/27b05cae9c9577a40ef85a820a54a5d3 to your computer and use it in GitHub Desktop.
Morse code dichotomic search.
// Determine the possible characters that could result from a certain morse code word with some potentially unknown signals (?).
// ? is either dit (.) or dah (-)
// This is incomplete, sorry.
const possibilities = signal => {
const signals = signal.split('');
console.log(`🚀`, signals);
const hasUnknownSignal = signals.includes('?');
if(!hasUnknownSignal) return [MORSE_CODE[signal]];
if (signals.length == 1) return [MORSE_CODE['.'], MORSE_CODE['-']];
const result = signals.map((item, index, arr) => {
if(item == '?') return '.,-';
})
console.log('🎉', result);
return result;
};
var MORSE_CODE = {
".-": "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"
}
possibilities('-?'); // []
// ..-, -.-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment