Created
September 16, 2018 18:04
-
-
Save brybrophy/563579c4c8621218c2b9988f7ad8297e to your computer and use it in GitHub Desktop.
JS solution to CodeSignal isCryptSolution exercise.
This file contains 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
function isCryptSolution(crypt, solution) { | |
let hasLeadingZeros = false; | |
const decrypted = crypt.map(word => { | |
const number = word.split('').map(letter => { | |
return solution.find(key => key[0] === letter)[1]; | |
}).join(''); | |
if (number.startsWith('0') && number.length > 1) { | |
hasLeadingZeros = true; | |
} | |
return Number.parseInt(number); | |
}); | |
return !hasLeadingZeros && decrypted[0] + decrypted[1] === decrypted[2]; | |
} | |
/* A cryptarithm is a mathematical puzzle for which the goal is to find the | |
correspondence between letters and digits, such that the given arithmetic | |
equation consisting of letters holds true when the letters are converted to digits. | |
You have an array of strings crypt, the cryptarithm, and an an array containing | |
the mapping of letters and digits, solution. The array crypt will contain | |
three non-empty strings that follow the structure: [word1, word2, word3], which | |
should be interpreted as the word1 + word2 = word3 cryptarithm. | |
If crypt, when it is decoded by replacing all of the letters in the cryptarithm | |
with digits using the mapping in solution, becomes a valid arithmetic equation | |
containing no numbers with leading zeroes, the answer is true. If it does not | |
become a valid arithmetic solution, the answer is false. */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment