Created
December 14, 2018 07:34
-
-
Save hafidbuilds/f88653d7e815b972eecb5ca99362397f to your computer and use it in GitHub Desktop.
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
let outputs = [] | |
function printSequence(strings) { | |
const optionsRange = [] | |
const alphabets = strings.toUpperCase().split('') | |
const functionQueues = [] | |
let count = 1 | |
if (alphabets.length < 9) { | |
for (let index = 0; index <= alphabets.length; index++) { | |
optionsRange.push(index + 1) | |
} | |
for (const optionRange of optionsRange) { | |
if (optionRange === 1) { | |
functionQueues.push(evaluateFirstCharacter) | |
} | |
if (optionRange > 2) { | |
functionQueues.push(evaluateNextCharacter) | |
} | |
} | |
for (const func of functionQueues) { | |
func(alphabets, optionsRange) | |
} | |
const numbers = outputs.map(number => number.toString()) | |
// reset Outputs | |
outputs = [] | |
return numbers.join('') | |
} | |
const msg = ` | |
|========================================================= | |
| Maximum character reached | |
| Please input less than 9 Charaters, instead of ${alphabets.length} | |
|========================================================= | |
` | |
return msg | |
} | |
function evaluateFirstCharacter(alphabets, optionsRange) { | |
const smallestOption = optionsRange[0] | |
const firstCharacter = alphabets[0] | |
if (firstCharacter === 'M') { | |
outputs.push(2, 1) | |
optionsRange.shift() | |
optionsRange.shift() | |
return | |
} else { | |
outputs.push(1, 2) | |
optionsRange.shift() | |
optionsRange.shift() | |
return | |
} | |
} | |
function evaluateNextCharacter(alphabets, optionsRange) { | |
// loop start from 2nd index character | |
for (let index = 1; index < alphabets.length; index++) { | |
const character = alphabets[index]; | |
const smallestOption = optionsRange[0] | |
const isPreviousCharacterM = alphabets[index - 1] === 'M' | |
const isPreviousCharacterN = alphabets[index - 1] === 'N' | |
// Evaluate M | |
if (character === 'M') { | |
if (isPreviousCharacterM) { | |
const index = outputs.findIndex(i => smallestOption - i === 1) | |
if (smallestOption) { | |
outputs = [ | |
...outputs.slice(0, index), | |
smallestOption, | |
...outputs.slice(index) | |
] | |
optionsRange.shift() | |
} | |
} | |
if (isPreviousCharacterN) { | |
const index = outputs.findIndex(i => smallestOption - i === 1) | |
if (smallestOption) { | |
outputs = [ | |
...outputs.slice(0, index), | |
smallestOption, | |
...outputs.slice(index) | |
] | |
optionsRange.shift() | |
} | |
} | |
} | |
// Evaluate N | |
if (character === 'N') { | |
if (isPreviousCharacterM) { | |
const index = outputs.findIndex(i => smallestOption - i === 1) | |
if (smallestOption) { | |
outputs.push(smallestOption) | |
optionsRange.shift() | |
} | |
} | |
if (isPreviousCharacterN) { | |
if (smallestOption) { | |
outputs.push(smallestOption) | |
optionsRange.shift() | |
} | |
} | |
} | |
} | |
} | |
console.log(printSequence('M')) | |
console.log(printSequence('N')) | |
console.log(printSequence('MM')) | |
console.log(printSequence('NN')) | |
console.log(printSequence('MN')) | |
console.log(printSequence('NM')) | |
console.log(printSequence('MNMN')) | |
console.log(printSequence('NNMMM')) | |
console.log(printSequence('MMNMMNNM')) | |
/* | |
Input: M Output: 21 | |
Input: N Output: 12 | |
Input: MM Output: 321 | |
Input: NN Output: 123 | |
Input: MN Output: 213 | |
Input: NM Output: 132 | |
Input: MNMN Output: 21435 | |
Input: NNMMM Output: 126543 | |
Input: MMNMMNNM Output: 321654798 | |
PSEUDOCODE: | |
------(1)------ | |
== evaluateFirstCharacter() == | |
if M | |
look up smallestOption integer inside OptionsRange, | |
push 2 sequence, | |
delete smallestOption inside OptionsRange | |
OptionsRange become [5,4,3] | |
M => Outputs: [2,1] | |
if N | |
look up smallestOption integer inside OptionsRange, | |
push 2 sequence, | |
delete smallestOption inside OptionsRange | |
OptionsRange become [5,4,3] | |
N => Outputs: [1,2] | |
------(2)------ | |
== evaluateNextCharacter() == | |
if M | |
look up smallestOption integer inside OptionsRange, | |
if (isPreviousCharacterM) => | |
findIndexOf smallestOption inside Outputs | |
if (smallestOption - currentOutput === 1) => swap left => merge smallestOption into output, | |
delete smallestOption from OptionsRange | |
OptionsRange become [5,4] | |
MM => Outputs: [3,2,1] | |
if (isPreviousStepN) => | |
findIndexOf smallestOption inside Outputs | |
if (smallestOption - currentOutput === 1) => swap left => merge smallestOption into outputs, | |
delete smallestOption from OptionsRange | |
OptionsRange become [5,4] | |
MM => Outputs: [1,3,2] | |
if N | |
look up smallestOption integer inside OptionsRange, | |
if (isPreviousStepM) => | |
findIndexOf smallestOption inside Outputs | |
if (smallestOption - currentOutput === 1) => swap left => merge smallestOption into outputs, | |
delete smallestOption from OptionsRange | |
OptionsRange become [5,4] | |
MN => Outputs: [2,1,3] | |
if (isPreviousStepNN) => | |
push smallestOption into Outputs, | |
delete smallestOption from OptionsRange | |
Options: [5,4] | |
NN => Outputs: [1,2,3] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment