Last active
April 21, 2020 14:30
-
-
Save filosofisto/58030353f9f1fe316941c52189399a2f to your computer and use it in GitHub Desktop.
Polish Reverse Calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.epam</groupId> | |
<artifactId>ups-interview</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>8</source> | |
<target>8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Calculator { | |
public int calculate(String value) { | |
Map<String, Operation> operations = initMapOperations(); | |
Deque<Integer> deque = new ArrayDeque<>(); | |
String[] items = value.split(" "); | |
Arrays.stream(items).forEach(item -> { | |
if (operations.containsKey(item)) { | |
Operation operation = operations.get(item); | |
int result = operation.apply(deque.pop(), deque.pop()); | |
deque.push(result); | |
} else { | |
deque.push(Integer.parseInt(item)); | |
} | |
}); | |
return deque.pop(); | |
} | |
private Map<String, Operation> initMapOperations() { | |
Map<String, Operation> operations = new HashMap<>(); | |
Arrays.stream(Operation.values()).forEach(operation -> operations.put(operation.getSignal(), operation)); | |
return operations; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.function.BiFunction; | |
public enum Operation { | |
ADD("+", (x,y) -> x+y), | |
SUB("-", (x,y) -> -x+y); | |
Operation(String signal, BiFunction<Integer, Integer, Integer> operation) { | |
this.signal = signal; | |
this.operation = operation; | |
} | |
public int apply(int x, int y) { | |
return this.operation.apply(x, y); | |
} | |
public String getSignal() { | |
return signal; | |
} | |
private final String signal; | |
private final BiFunction<Integer, Integer, Integer> operation; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Assert; | |
import org.junit.Test; | |
public class CalculatorTest { | |
private Calculator calculator = new Calculator(); | |
@Test | |
public void singleValue() { | |
Assert.assertEquals(1, calculator.calculate("1")); | |
} | |
@Test | |
public void simpleAddExpression() { | |
Assert.assertEquals(3, calculator.calculate("2 1 +")); | |
} | |
@Test | |
public void simpleMinusExpression() { | |
Assert.assertEquals(1, calculator.calculate("2 1 -")); | |
} | |
@Test | |
public void complexAddExpression() { | |
Assert.assertEquals(6, calculator.calculate("3 2 + 1 +")); | |
} | |
@Test | |
public void complexMinusExpression() { | |
Assert.assertEquals(3, calculator.calculate("3 2 - 2 +")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment