You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
43
45
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:
0 commit comments