Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/org/fundacionjala/coding/jose/Calc.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.fundacionjala.coding.jose;

import java.util.Stack;

/**
* Created by JoseTorrez on 9/4/2017.
* Your job is to create a calculator which evaluates expressions in Reverse Polish notation.
* For example expression 5 1 2 + 4 * + 3 - (which is equivalent to
* 5 + ((1 + 2) * 4) - 3 in normal notation) should evaluate to 14.
* Note that for simplicity you may assume that there are always spaces between numbers and operations,
* e.g. 1 3 + expression is valid, but 1 3+ isn't.
* Empty expression should evaluate to 0.
* Valid operations are +, -, *, /.
* You may assume that there won't be exceptional situations (like stack underflow or division by zero).
*/
public class Calc {

/**
*This method is for calculate expressions in reverse.
* @param expression String.
* @return Double.
*/
public double evaluate(String expression) {
if ("".equals(expression)) {
return 0;
}
Stack<Double> stack = new Stack<Double>();
for (String s : expression.split("\\s")) {
if ("+".equals(s)) {
stack.push(stack.pop() + stack.pop());
} else if ("-".equals(s)) {
stack.push(-1 * (stack.pop() - stack.pop()));
} else if ("*".equals(s)) {
stack.push(stack.pop() * stack.pop());
} else if ("/".equals(s)) {
stack.push(1 / (stack.pop() / stack.pop()));
} else {
stack.push(Double.parseDouble(s));
}
}
return stack.pop();
}
}
69 changes: 69 additions & 0 deletions src/test/java/org/fundacionjala/coding/jose/CalcTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.fundacionjala.coding.jose;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* Created by JoseTorrez on 9/4/2017.
*/
public class CalcTest {

private Calc calc = new Calc();

/**
* Firs Test.
*/
@Test
public void shouldWorkWithEmptyString() {
assertEquals("Should work with empty string", 0, calc.evaluate(""), 0);
}

/**
* Second Test.
*/
@Test
public void shouldParseNumbers() {
assertEquals("Should parse numbers", 3, calc.evaluate("1 2 3"), 0);
}

/**
* Third Test.
*/
@Test
public void shouldParseFloatNumbers() {
assertEquals("Should parse float numbers", 3.5, calc.evaluate("1 2 3.5"), 0);
}

/**
* Fourtht Test.
*/
@Test
public void shouldSupportAddition() {
assertEquals("Should support addition", 4, calc.evaluate("1 3 +"), 0);
}

/**
* Fifth Test.
*/
@Test
public void shouldSupportMultiplication() {
assertEquals("Should support multiplication", 3, calc.evaluate("1 3 *"), 0);
}

/**
* Sixth Test.
*/
@Test
public void shouldSupportSubstraction() {
assertEquals("Should support substraction", -2, calc.evaluate("1 3 -"), 0);
}

/**
* Seventh Test.
*/
@Test
public void shouldSupportDivision() {
assertEquals("Should support division", 2, calc.evaluate("4 2 /"), 0);
}
}