# Written Word Equation

Word equations, but not as you know it! Given a sentence which will include two numbers, numerically, and a spelt operator, in the order seen in the examples, your goal is to give the numerical answer

The operators will be spelt as: "add", "minus", "times" and "divide"

Any real number integer may be used, as suggested by caird and Jonathan

The solution must take one input and give one output

Examples:

Input: 7 add 8         Output: 15
Input: 9 times -2      Output: -18
Input: 24 divide 2     Output: 12
Input: 4 minus 5       Output: -1


You are not required to deal with divide by zero errors.

If you use a method that parses the expression, such as the python eval or exec, there will be no penalties, but if possible try to give a another interesting solution

Scoring:

Fewest bytes will win

• Can we take input as [7, 'add', 8]? Or, perhaps less reasonably, as [7, 8] for one input, and 'add' as a second input? Sep 15, 2021 at 18:17
• @cairdcoinheringaahing the first one yes, as its still one input, the list, and it the correct order, but the second one no Sep 15, 2021 at 18:19
• @JonathanAllan Real numbers positive or negative rational numbers Sep 15, 2021 at 18:37
• It might be simpler just to limit the input to integers. I doubt it'll change any potential methods, but using "reals" or "decimals" can lead you to spend time/effort into specifying details such as format and precision, and using integers can allow more languages to take part that may not support floats/reals/decimals as easily Sep 15, 2021 at 18:46

# Jelly, 14 bytes

Ḣ€Oị2¦“×+ _÷”V


Try it online!

Takes input as e.g. [7, 'add', 8]. Requires no leading 0 for floats (e.g. -.5 instead of -0.5)

## How it works

Ḣ€Oị2¦“×+ _÷”V - Main link. Takes a list [l, op, r]
Ḣ€             - First of each.
O            - Convert to ordinal
2¦         - To the element at the 2nd index (the op):
ị  “×+ _÷”  - Index into the string “×+ _÷”
V - Evaluate as Jelly


# Japt, 15 bytes

Takes input as an array. Thought there might be some other clever way of working with the charcodes to get the required operator but using caird's method of indexing into the operators seems to be optimal.

OvUg1_Îcg"/*+ -


Try it

Or without using eval:

rUj1 ÎÎcg"/*+ -


Try it

OvUg1_Îcg"/*+ -     :Implicit input of array U
Ov                  :Eval as Japt
Ug1               :  Modify the element at 0-based index 1 in U
_              :  By passing it through the following function
Î             :    First character
c            :    Charcode
g"/*+ -     :    Index into "/*+ -"

rUj1 ÎÎcg"/*+ -     :Implicit input of array U
r                   :Reduce by
Uj1                :  Remove & return, as a singleton array, the element at 0-based index 1 in U
Î              :  First element
Îcg"/*+ -     :  As above


# Python 3, 91 bytes

def f(a):a,b,c=a;b=b[0];d=b=='d';return(a+c*((b=='a')-(b=='m')))*[1,[c,1/c][d]][b=='t'or d]


Takes a single input as a listed of the values, i.e. [7, "add", 8] and returns the result of the equation.

Try it online!

# Python 3, 71 bytes

x,y,z=input().split()
print(eval(f'{x}{"/*+ -"[ord(str(y)[0])%5]}{z}'))


Try it online!

Inspired by various other answers featuring ord(str(y)[0])%5.

If the input is taken as a list like [5, 'minus', 8], then you can get an answer of only 67 bytes;

lambda l:print(eval(f'{l[0]}{"/*+ -"[ord(str(l[1])[0])%5]}{l[2]}'))


Try it online!

# Charcoal, 26 bytes

Ｎθ≡§Ｓ⁰aＩ⁺θＮmＩ⁻θＮtＩ×θＮdＩ∕θＮ


Try it online! Link is to verbose version of code. Explanation: Inputs the first number, switches on the string then inputs the second number as it outputs the result of the appropriate operation. 20 bytes with a boring eval-based approach:

ＩＵＶ⁺θ⁺§+-*/⌕amtd§η⁰ζ


Try it online! Link is to verbose version of code. Explanation: Gets the appropriate Python operator depending on the first letter of the string and evaluates the result as a Python expression.

# Perl 5-p, 44 bytes

s/a/+/;s/t/*/;s|v|/|;s/n/-/;s/\pL//g;\$_=eval
`

Try it online!