Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Manish Jain maniish-jaiin

🎯
Focusing
View GitHub Profile
View FinalSolution.java
public double evaluate(String s) {
String[] elements = getStringArray(s);
Stack<Double> numbers = new Stack();
double result = 0.0;
for (String c : elements) {
switch (c) {
case "+": {
result = numbers.push(numbers.pop() + numbers.pop());
break;
}
View EvaluateAll.java
@Test
public void evaluateWhenAllOperatorAreFound() {
ReversePolishNotation rpn = new ReversePolishNotation();
double result = rpn.evaluate("10 6 9 3 + -11 * / * 17 + 5 +");
assertEquals(21.5, result, 0.1);
}
View AllOperators.java
public double evaluate(String s) {
String[] elements = getStringArray(s);
Stack<Double> numbers = new Stack();
double result = 0.0;
for (String c : elements) {
switch (c) {
case "+":
result = numbers.push(numbers.pop() + numbers.pop());
break;
case "-":
View MinusOperator.java
@Test
public void addNumbersWhenMinusOperatorIsFound() {
ReversePolishNotation rpn = new ReversePolishNotation();
double result = rpn.evaluate("5 5 2 + -");
assertEquals(2.0, result, 0.1);
}
@Test
public void addNumbersWhenMultiplyOperatorIsFound() {
ReversePolishNotation rpn = new ReversePolishNotation();
View EvaluateWithStack.java
public double evaluate(String s) {
String[] elements = getStringArray(s);
Stack<Double> numbers = new Stack();
double result = 0.0;
for (int i = 0; i < elements.length; i++) {
String c = elements[i];
if (c.equals("+") && i >= 2) {
result = numbers.push(numbers.pop() + numbers.pop());
} else {
numbers.push(Double.parseDouble(c));
View AddMultipleOperator.java
@Test
public void addNumbersWhenMultiplePlusOperatorIsFound() {
ReversePolishNotation rpn = new ReversePolishNotation();
double result = rpn.evaluate("5 5 2 + +");
assertEquals(12.0, result, 0.1);
}
View AddNumber.java
public double evaluate(String s) {
String[] elements = getStringArray(s);
double result = 0.0;
for (int i = 0; i < elements.length; i++) {
String c = elements[i];
if (c.equals("+") && i >= 2) {
result = parseDouble(elements[i - 1]) + parseDouble(elements[i - 2]);
}
}
return result;
View AddNumbersTest.java
@Test
public void addNumbersWhenPlusOperatorIsFound() {
ReversePolishNotation rpn = new ReversePolishNotation();
double result = rpn.evaluate("3 4 +");
assertEquals(7.0, result, 0.1);
}
View AddTwoNumbersTest.java
public void addNumbersWhenPlusOperatorIsFound() {
ReversePolishNotation rpn = new ReversePolishNotation();
double result = rpn.evaluate("5 5 +");
assertEquals(10.0, result, 0.1);
}
View GetStringArray.java
public String[] getStringArray(String s) {
return s.split(" ");
}