WIP: math parser
This commit is contained in:
41
2kyu/evaluate-mathematical-expression/description.md
Normal file
41
2kyu/evaluate-mathematical-expression/description.md
Normal file
@@ -0,0 +1,41 @@
|
||||
Instructions
|
||||
Given a mathematical expression as a string you must return the result as a number.
|
||||
|
||||
Numbers
|
||||
Number may be both whole numbers and/or decimal numbers. The same goes for the returned result.
|
||||
|
||||
Operators
|
||||
You need to support the following mathematical operators:
|
||||
|
||||
Multiplication *
|
||||
Division / (as floating point division)
|
||||
Addition +
|
||||
Subtraction -
|
||||
Operators are always evaluated from left-to-right, and * and / must be evaluated before + and -.
|
||||
|
||||
Parentheses
|
||||
You need to support multiple levels of nested parentheses, ex. (2 / (2 + 3.33) * 4) - -6
|
||||
|
||||
Whitespace
|
||||
There may or may not be whitespace between numbers and operators.
|
||||
|
||||
An addition to this rule is that the minus sign (-) used for negating numbers and parentheses will never be separated by whitespace. I.e all of the following are valid expressions.
|
||||
|
||||
1-1 // 0
|
||||
1 -1 // 0
|
||||
1- 1 // 0
|
||||
1 - 1 // 0
|
||||
1- -1 // 2
|
||||
1 - -1 // 2
|
||||
1--1 // 2
|
||||
|
||||
6 + -(4) // 2
|
||||
6 + -( -4) // 10
|
||||
And the following are invalid expressions
|
||||
|
||||
1 - - 1 // Invalid
|
||||
1- - 1 // Invalid
|
||||
6 + - (4) // Invalid
|
||||
6 + -(- 4) // Invalid
|
||||
Validation
|
||||
You do not need to worry about validation - you will only receive valid mathematical expressions following the above rules.
|
||||
35
2kyu/evaluate-mathematical-expression/solution.ts
Normal file
35
2kyu/evaluate-mathematical-expression/solution.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
type Token = number | '+' | '-' | '*' | '/' | '(' | ')';
|
||||
|
||||
|
||||
|
||||
type ASTNode = {
|
||||
type: string,
|
||||
}
|
||||
|
||||
type OperatorNode = {
|
||||
type: 'operator',
|
||||
value: '+' | '-' | '*' | '/',
|
||||
left: ASTNode,
|
||||
right: ASTNode,
|
||||
}
|
||||
|
||||
type NumberNode = {
|
||||
type: 'number',
|
||||
value: number,
|
||||
}
|
||||
|
||||
type GroupNode = {
|
||||
type: 'group',
|
||||
value: ASTNode,
|
||||
}
|
||||
|
||||
type NegationNode = {
|
||||
type: 'negation',
|
||||
value: ASTNode,
|
||||
}
|
||||
|
||||
export function calc(expression: string): number {
|
||||
// evaluate `expression` and return result
|
||||
return 0;
|
||||
}
|
||||
24
2kyu/evaluate-mathematical-expression/tests.ts
Normal file
24
2kyu/evaluate-mathematical-expression/tests.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { calc } from './solution';
|
||||
import { expect } from "chai";
|
||||
|
||||
var tests: [string, number][] = [
|
||||
['1+1', 2],
|
||||
['1 - 1', 0],
|
||||
['1* 1', 1],
|
||||
['1 /1', 1],
|
||||
['-123', -123],
|
||||
['123', 123],
|
||||
['2 /2+3 * 4.75- -6', 21.25],
|
||||
['12* 123', 1476],
|
||||
['2 / (2 + 3) * 4.33 - -6', 7.732],
|
||||
];
|
||||
|
||||
describe("calc", function() {
|
||||
it("should evaluate correctly", () => {
|
||||
tests.forEach(function(m) {
|
||||
var x = calc(m[0]);
|
||||
var y = m[1];
|
||||
expect(x).to.equal(y, 'Expected: "' + m[0] + '" to be ' + y + ' but got ' + x);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user