diff --git a/2kyu/evaluate-mathematical-expression/solution.ts b/2kyu/evaluate-mathematical-expression/solution.ts index 619a3f2..b5fa661 100644 --- a/2kyu/evaluate-mathematical-expression/solution.ts +++ b/2kyu/evaluate-mathematical-expression/solution.ts @@ -197,7 +197,7 @@ function parseNumbers(tokens: (Token | ASTNode)[]): (Token | ASTNode)[] { nextNextToken as NumberToken ]); tokens.splice(i, 3, numberNode); - i += 3; + i += 2; continue; } else { // it is a regular number @@ -247,6 +247,11 @@ function parseGroups(tokens: (Token | ASTNode)[]): (Token | ASTNode)[] { for (const [start, end] of parenthesisPairs) { const groupNode = GroupNode.fromTokens(tokens.slice(start + 1, end)); tokens.splice(start, end - start + 1, groupNode); + // update pairs after splice + for (let i = 0; i < parenthesisPairs.length; i++) { + const [start2, end2] = parenthesisPairs[i]; + parenthesisPairs[i] = [start2 - (end - start), end2 - (end - start)]; + } } return tokens; @@ -293,6 +298,7 @@ function parseMultDiv(tokens: (Token | ASTNode)[]): (Token | ASTNode)[] { const operator = token.value as OperatorChars; const operatorNode = new OperatorNode(operator, left, right); tokens.splice(i - 1, 3, operatorNode); + continue; } i++; } @@ -316,6 +322,7 @@ function parseAddSub(tokens: (Token | ASTNode)[]): (Token | ASTNode)[] { const operator = token.value as OperatorChars; const operatorNode = new OperatorNode(operator, left, right); tokens.splice(i - 1, 3, operatorNode); + continue; } i++; } diff --git a/2kyu/evaluate-mathematical-expression/tests.ts b/2kyu/evaluate-mathematical-expression/tests.ts index bc7d664..14e9163 100644 --- a/2kyu/evaluate-mathematical-expression/tests.ts +++ b/2kyu/evaluate-mathematical-expression/tests.ts @@ -11,6 +11,9 @@ var tests: [string, number][] = [ ['2 /2+3 * 4.75- -6', 21.25], ['12* 123', 1476], ['2 / (2 + 3) * 4.33 - -6', 7.732], + ['(2 + (2 + 2) / 4) * 3', 9], + ['(1 - 2) + -(-(-(-4)))', 3], + ['((2.33 / (2.9+3.5)*4) - -6)', 7.45625] ]; describe("calc", function () {