70
10
Preamble
Integers are always either even or odd. Even integers are divisible by two, odd integers are not.
When you add two integers you can infer whether the result will be even or odd based on whether the summands were even or odd:
- Even + Even = Even
- Even + Odd = Odd
- Odd + Even = Odd
- Odd + Odd = Even
Likewise, when you multiply two integers you can infer whether the result will be even or odd based on whether the factors were even or odd:
- Even * Even = Even
- Even * Odd = Even
- Odd * Even = Even
- Odd * Odd = Odd
Thus, if you know the evenness or oddness of all the variables in a math expression that only involves addition and multiplication, you can infer whether the result will be even or odd.
For example, we can confidently say that (68 + 99) * 37
results in an odd because an even plus an odd (68 + 99
) is an odd, and that odd times another odd (odd * 37
) gives an odd.
Challenge
Write a program or function that takes in a string only containing the four characters eo+*
. This string represents a mathematical expression given in prefix notation involving only addition (+
) and multiplication (*
). Each e
represents some arbitrary even number, and each o
represents some arbitrary odd number.
Your task is to simplify the expression, printing or returning a single e
or o
based on whether the result of the expression is even or odd.
You can assume that the input will always be in valid prefix notation. Specifically, each +
and *
will always have two corresponding operands occurring after it. These operands may be a single e
or o
, or another +
or *
expression that in turn has operands.
For example, the input *+eoo
could be read as mul(add(e, o), o)
, or (e + o) * o
in normal infix notation. The e
and the first o
are the operands corresponding to the +
, and +eo
and the last o
are the operands corresponding to the *
.
Just to make it clear, here are some invalid inputs that have incorrect prefix notation:
eo
ooe
o+e
ee*
+*oe
+e*o
A single trailing newline in the output is fine, but otherwise a plain e
for even or o
for odd is all that should be output.
The shortest code in bytes wins.
Test Cases
(Empty lines are only to help visually separate similar cases.)
e -> e
o -> o
+ee -> e
+eo -> o
+oe -> o
+oo -> e
*ee -> e
*eo -> e
*oe -> e
*oo -> o
+e+ee -> e
+e+eo -> o
+e+oe -> o
+e+oo -> e
+e*ee -> e
+e*eo -> e
+e*oe -> e
+e*oo -> o
+o+ee -> o
+o+eo -> e
+o+oe -> e
+o+oo -> o
+o*ee -> o
+o*eo -> o
+o*oe -> o
+o*oo -> e
*e+ee -> e
*e+eo -> e
*e+oe -> e
*e+oo -> e
*e*ee -> e
*e*eo -> e
*e*oe -> e
*e*oo -> e
*o+ee -> e
*o+eo -> o
*o+oe -> o
*o+oo -> e
*o*ee -> e
*o*eo -> e
*o*oe -> e
*o*oo -> o
++eee -> e
++eeo -> o
++eoe -> o
++eoo -> e
++oee -> o
++oeo -> e
++ooe -> e
++ooo -> o
+*eee -> e
+*eeo -> o
+*eoe -> e
+*eoo -> o
+*oee -> e
+*oeo -> o
+*ooe -> o
+*ooo -> e
*+eee -> e
*+eeo -> e
*+eoe -> e
*+eoo -> o
*+oee -> e
*+oeo -> o
*+ooe -> e
*+ooo -> e
**eee -> e
**eeo -> e
**eoe -> e
**eoo -> e
**oee -> e
**oeo -> e
**ooe -> e
**ooo -> o
+e+e+e+ee -> e
+o+o+o+oo -> o
*e*e*e*ee -> e
*o*o*o*oo -> o
+e+o+e+oe -> e
+o+e+o+eo -> o
*e*o*e*oe -> e
*o*e*o*eo -> e
+e*e+e*ee -> e
+o*o+o*oo -> o
*e+e*e+ee -> e
*o+o*o+oo -> o
+**++*+*eeoeeooee -> e
+**++*+***eooeoeooeoe -> e
+**+***+**++**+eooeoeeoeeoeooeo -> o
+e*o*e**eoe -> e
+*e+e+o+e**eeoe -> e
**o++*ee*++eoe*eo+eoo -> o
8Can we take 1 and 0 instead of e and o as input? – ghosts_in_the_code – 2015-12-02T10:30:45.977
8@ghosts_in_the_code No, sorry. – Calvin's Hobbies – 2015-12-02T10:32:14.627
2Is using
eval
OK? – xnor – 2015-12-02T10:45:40.4001@xnor Sure. Whatever works. – Calvin's Hobbies – 2015-12-02T10:47:02.920
2I doubt I can use this to beat the 13 bytes already posted, but I notice that addition matches an exclusive or and multiplication a simple or. – WGroleau – 2015-12-03T16:19:11.930
@WGroleau oops, scratch that last comment - if we're being consistent with outputs, it's XOR and AND (even = 0, odd = 1) or XNOR and NAND (switched) – question_asker – 2015-12-04T12:47:59.517
@question_asker, Correct on your second. My bad. – WGroleau – 2015-12-07T06:38:16.840
Will any language don't hardcore the symbol of
o
be sshorter? – l4m2 – 2018-11-18T12:42:58.767