Created
October 6, 2023 08:23
-
-
Save syed-ahmad/27b05cae9c9577a40ef85a820a54a5d3 to your computer and use it in GitHub Desktop.
Morse code dichotomic search.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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