Last active
June 10, 2022 14:53
-
-
Save metaory/2e964026e7cc3cbb97a18d88d74fa512 to your computer and use it in GitHub Desktop.
replace wildcard with non-repeating characters
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
/* | |
Rick is a fan of logic-based games. However, he is bored of the classic ones, like Sudoku and Mastermind, since he has solved so many of them. Recently he found a new game in which one is given a string with some question marks in it. The objective is to replace all of the question marks with letters (one letter per question mark) in such a way that no letter appears next to another letter of the same kind. | |
Write a function: | |
function solution(riddle); | |
that, given a string riddle, returns a copy of the string with all of the question marks replaced by lowercase letters (a−z) in such a way that the same letters do not occur next to each other. The result can be any of the possible answers as long as it fulfils the above requirements. | |
Examples: | |
1. Given riddle = "ab?ac?", your function might return "abcaca". Some other possible results are "abzacd", "abfacf". | |
2. Given riddle = "rd?e?wg??", your function might return "rdveawgab". | |
3. Given riddle = "????????", your function might return "codility". | |
Write an efficient algorithm for the following assumptions: | |
the length of the string is within the range [1..100,000]; | |
string riddle consists only of lowercases letters (a − z) or '?'; | |
it is always possible to turn string 'riddle' into a string without two identical consecutive letters. | |
*/ | |
const { log, table, clear } = console | |
clear() | |
const CHARECTERS = 'abcdefghijklmnopqrstuvwxyz' | |
const randomChar = () => CHARECTERS[Math.floor(Math.random() * CHARECTERS.length)] | |
const replaceWildcard = (pre, nex) => { | |
const potential = randomChar() | |
if ([pre, nex].includes(potential)) { | |
return replaceWildcard(pre, nex) | |
} else { | |
return potential | |
} | |
} | |
const Solution = (S) => | |
S.split('').reduce((acc, cur, i, arr) => { | |
const pre = acc[acc.length - 1] | |
const nex = arr[i + 1] | |
if (cur !== '?') acc += cur | |
else { | |
acc += replaceWildcard(pre, nex) | |
} | |
return acc | |
}, '') | |
const test = (str) => str.split('').reduce((acc, cur, i, arr) => { | |
if (acc === false) return acc | |
const pre = arr[i - 1] | |
const nex = arr[i + 1] | |
if ([pre, nex].includes(cur)) acc = false | |
return acc | |
}, true) | |
//////////////////////////////////////////// | |
const t1 = 'ab?ac?' | |
const t2 = 'rd?e?wg??' | |
const t3 = '????????' | |
table([ | |
{ riddle: t1, result: Solution(t1), pass: test(Solution(t1)) }, | |
{ riddle: t2, result: Solution(t2), pass: test(Solution(t2)) }, | |
{ riddle: t3, result: Solution(t3), pass: test(Solution(t3)) } | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment