Skip to content

Commit f5762a3

Browse files
authored
Update README.md
1 parent 347df02 commit f5762a3

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,88 @@ Infix expression: 6 + (( 4 - (2 + 3 ) ) * 8 )
4040
Prefix expression: + 6 * - 4 + 2 3 8
4141
value: -2
4242
```
43+
##### Variables
44+
So far we have only considered numbers as our operands for the prefix expression. For this test, we would like your function to also support variables withing the expression.
4345

46+
For example:
47+
```
48+
expression: + 10 x
49+
variables {"x":3}
50+
value: 13
51+
```
52+
##### Task
53+
54+
Implement a function CalculateExpression(expression,variables)
55+
that takes as inputs:
56+
* expression, a string containing an expression in prefix notiation that might contain variables
57+
* variables, a JSON objeect containing the values for each variable in the expression
58+
and returns:
59+
* the result of the expression for the given values
60+
* throw exception the expression is invalid or if the expression does not have any valid result
61+
62+
The syntax supports 4 operators: +, -, *, and /. These are the standard artihmetic operators.
63+
* Note: / denotes integer division, that is / 7 2 evaluates to 3, not 3.5.
64+
65+
The only accepted numeric operands aer positive integers in base 10 (e.g 1,22, 85 are valid, -1,0x43, 0 are not valid).
66+
67+
A valid variable name is any sequence of characters that doesn't include whitespaces(spaces,tabs, newlines, etc).
68+
69+
##### Examples
70+
71+
```
72+
Expression: + 1 5
73+
variables: {}
74+
result: 6
75+
```
76+
77+
```
78+
Expression: + 1 2 3
79+
variables: {}
80+
result: 6
81+
```
82+
83+
```
84+
Expression: + 1
85+
variables: {}
86+
result: exception
87+
```
88+
89+
```
90+
Expression: 9
91+
variables: {}
92+
result: 9
93+
```
94+
95+
```
96+
Expression: * + 1 2 3
97+
variables: {}
98+
result: 9
99+
```
100+
101+
Although, negative numeric operands are invalid in expression, intermediate and final results may be negative
102+
```
103+
Expression: + 6 * - 4 + 2 3 8
104+
variables: {}
105+
result: -2
106+
```
107+
108+
Operators and operrands must be seperated by one or more white spaces:
109+
```
110+
Expression: -+1 5 3
111+
variables: {}
112+
result: exception
113+
```
114+
115+
```
116+
Expression: +1 2
117+
variables: {}
118+
result: 3
119+
```
120+
Expression containing variables:
121+
122+
```
123+
Expression: * + 2 x y
124+
variables: {"x":1,"y":3}
125+
result: 9
126+
```
44127

0 commit comments

Comments
 (0)