Last active
July 22, 2016 06:53
-
-
Save LiuuY/a37d0843fffa540319e4f966fc6a9d55 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
// input string | |
var originString = [ | |
"st->io", | |
"io->cond", | |
"cond(yes)->op1", | |
"cond(no)->op2", | |
"op1->sop1", | |
"op2->sop2", | |
"sop1->io1", | |
"sop2->io2", | |
"io1->e1", | |
"io2->e2", | |
"e1->e", | |
"e(yes)->x1", | |
"e(no)->x2", | |
"e2(yes)->y1", | |
"e2(no)->y2" | |
] | |
var first = [] | |
var second = [] | |
var result = [] | |
var resultIndex = 0 | |
var findMatch = function(a, e) { | |
return a.indexOf(e) | |
} | |
var findConditionYes = function(a, e) { | |
return a.indexOf(e + "(yes)") | |
} | |
var findConditionNo = function(a, e) { | |
return a.indexOf(e + "(no)") | |
} | |
var getEmptyIndex = function(result) { | |
for (var i = 0; ; i++) { | |
if (result[i] === undefined) return i | |
} | |
} | |
var calc = function(frist, second, index, resultIndex) { | |
if (index === -1) return | |
result[resultIndex] = result[resultIndex] || [] | |
if (result[resultIndex].length === 0 || result[resultIndex][result[resultIndex].length - 1] !== first[index]) { | |
result[resultIndex].push(first[index]) | |
} | |
result[resultIndex].push(second[index]) | |
// first.splice(index, 1) | |
// second.splice(index, 1) | |
var nextIndex = findMatch(first, result[resultIndex][result[resultIndex].length - 1]) | |
if (nextIndex !== -1) { | |
calc(first, second, nextIndex, resultIndex) | |
} else if (first.length !== 0) { | |
var nextYesIndex = findConditionYes(first, result[resultIndex][result[resultIndex].length - 1]) | |
calc(first, second, nextYesIndex, getEmptyIndex(result)) | |
var nextNoIndex = findConditionNo(first, result[resultIndex][result[resultIndex].length - 1]) | |
calc(first, second, nextNoIndex, getEmptyIndex(result)) | |
} | |
return result[resultIndex].join("->") | |
} | |
originString.forEach(v => { | |
var e = v.split("->") | |
first.push(e[0]) | |
second.push(e[1]) | |
}) | |
calc(first, second, 0, 0) | |
result.forEach(v => { | |
console.log(v.join("->")) | |
}) | |
// output: | |
// st->io->cond | |
// cond(yes)->op1->sop1->io1->e1->e | |
// e(yes)->x1 | |
// e(no)->x2 | |
// cond(no)->op2->sop2->io2->e2 | |
// e2(yes)->y1 | |
// e2(no)->y2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment