Created
July 30, 2023 04:10
-
-
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
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
| /** | |
| * 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