Skip to content

Commit 56bd743

Browse files
committed
Merge branch 'main' of https://github.com/csehammad/CodingProblems into main
2 parents 31d8fa1 + 144bf54 commit 56bd743

File tree

1 file changed

+131
-1
lines changed

1 file changed

+131
-1
lines changed

README.md

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,131 @@
1-
# CodingProblems
1+
# Coding Challenges
2+
### This repository contains various coding challenges taken on Sites like HackerRank, LeetCode etc.
3+
4+
## Challenge#1- Prefix Notation
5+
#### Write a function that evaluates an expression written in Prefix Notiation and returrns a value
6+
7+
##### Problem Statement:
8+
9+
Prefix notation (also known as polish notation) is an alternative to the more familier infix notation.
10+
11+
In infix notation, operators(add,multiply,etc) are written between their operands(number, variables, or sub-expression).
12+
* In prefix notation, operators are written before their operands
13+
Some examples follow of expression in infix notation and their equivalents in prefix notation.
14+
In this example the operator is + and its operands are 1 and 2:
15+
16+
```
17+
Infix expression: 1 + 2
18+
Prefix expression: + 1 2
19+
Value: 3
20+
```
21+
22+
In this example, the sub-expression + 1 2 is the first operand of the + operator
23+
24+
```
25+
Infix expression: (1+2) * 3
26+
Prefix expression: * + 1 2 3
27+
Value: 9
28+
```
29+
30+
Sometimes there are multiple sub-expressions:
31+
```
32+
Infix expression: ((1 + 2) *3)-4
33+
Prefix expression: - * + 1 2 3 4
34+
value: 5
35+
```
36+
37+
Sub-expressions can be nested arbitrarily deeply:
38+
```
39+
Infix expression: 6 + (( 4 - (2 + 3 ) ) * 8 )
40+
Prefix expression: + 6 * - 4 + 2 3 8
41+
value: -2
42+
```
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.
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:
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+
```
127+
128+
##### Solution: CodingChallenge.HackerRank/PrefixNotation.cs
129+
##### Test Cases : CodingChallenge.Tests/UT_PrefixNotation.cs
130+
131+

0 commit comments

Comments
 (0)