2 kyu
Parsing and evaluation of mathematical expressions
182 of 183smile67
Description:
Your task is to parse and Calculate Mathematical Expressions.
Don't use any 'parse', 'compile' or 'eval' function of the Compiler (by the way C# has no 'eval' function like Java/Javascript/..., but surely there are ways to use similar methods, or parts of it) - write your own parser/evaluation function.
I don't want to write down each syntactical fineness of the mathematical expressions you have to evaluate, but here are some infos or rules and later on some examples:
Generally "normal programming code syntax and operator priority" for the expression/input string
Operators: *, /, +, -, & (last one instead of Math.Pow, i.e. 2&3= 8)
Operator- priority: "&" > "* /" > "+ -"; [hint: -2+2=0, but -2&2=-4 i.e. -2&2=-(2&2) and not (-2)&2]
-- => + is ok; ++ is not allowed (---5 [=-5] or (-5+1) [=-4] is allowed, but not ++5 or (+5+1))
Numbers: integers, doubles, scientific notation too (0.01= 1e-2 and 100= 1e2 or 1e+2, -0.1= -1e-1 for example)
List of allowed mathematical functions: "log", "ln", "exp", "sqrt", "abs", "atan", "acos", "asin", "sinh", "cosh", "tanh", "tan", "sin", "cos" (all exist in "C#/Math" and can be used for concrete calculation)
Simple error- handling is required (minimal output on errors is string "ERROR", details aren't necessary, for example (5+2)) or 5/0 are errors)
Now some easy examples:
Some generall infos for coding and testcases:
You have to implement a function
"public string eval (string expression)"
which returns the result of the calculated expression as a string. If the result is valid, you have to convert the double to a string (the expression value is a double, the eval output a string) and if an error occurs, you have to return "ERROR" (with optional message like "ERROR: Stack overflow"). The expression can contain [Spaces], so "sin ( 2) ist ok, 5 + 6 -2 too, but not "si n (2)" (function names not splitted). The included mathematical functions can use a mix of upper and lower letters, so Sin(2), SIN(2) and sin(2) are allowed.The test cases contain some "constant/static expressions", but the later and more difficult tests for your code- submission include random tests too. Here your results are accepted, if there's not more than 1e-8 difference to the calculated test- results.
Hope you have fun;-)!
Algorithms
Data Structures
Parsing
Interpreters
Mathematics
Similar Kata:
Stats:
Created | Nov 19, 2015 |
Published | Nov 20, 2015 |
Warriors Trained | 1152 |
Total Skips | 250 |
Total Code Submissions | 4033 |
Total Times Completed | 183 |
C# Completions | 182 |
Total Stars | 143 |
% of votes with a positive feedback rating | 90% of 64 |
Total "Very Satisfied" Votes | 54 |
Total "Somewhat Satisfied" Votes | 7 |
Total "Not Satisfied" Votes | 3 |
Total Rank Assessments | 5 |
Average Assessed Rank | 2 kyu |
Highest Assessed Rank | 1 kyu |
Lowest Assessed Rank | 3 kyu |