Skip to content

Instantly share code, notes, and snippets.

@steve-taylor
Created September 27, 2012 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steve-taylor/3794578 to your computer and use it in GitHub Desktop.
Save steve-taylor/3794578 to your computer and use it in GitHub Desktop.
Semi (actual submission)
public class Problem1 {
static enum Oper {
PLUS,
MINUS
}
public static void main(String[] args) {
Oper lastOp = null;
Integer lastNum = null;
Integer res = null;
for (String s : Input.read().split(" ")) {
System.out.println("" + s + " " + res);
int n = num(s);
if (n > 0) {
if (res == null) {
res = n;
lastNum = n;
continue;
}
if (lastOp != null) {
switch (lastOp) {
case PLUS:
res += lastNum;
break;
case MINUS:
res -= lastNum;
break;
default:
// ??
}
}
lastNum = n;
}
else if (s.equals("plus")) {
lastOp = Oper.PLUS;
}
else if (s.equals("minus")) {
lastOp = Oper.MINUS;
}
else {
// ???
}
}
Output.write("" + res);
}
static int num(String input) {
if (input.equals("one")) return 1;
if (input.equals("two")) return 2;
if (input.equals("three")) return 3;
if (input.equals("four")) return 4;
if (input.equals("five")) return 5;
if (input.equals("six")) return 6;
if (input.equals("seven")) return 7;
if (input.equals("eight")) return 8;
if (input.equals("nine")) return 9;
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment