Worded Calculator
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
A simplistic version of English numeral calculator
Task
Write a program that takes a string as input and outputs the result of the expression.
Rules
The input string will be worded and not numeral.
There will be no parentheses.
Order of calculation will be divide,multiply,subtract then add.
For same operations, the calculations must be done from left to right.
All input numbers will be integers from -999 to 999 (both inclusive)
Output will be an integer of any range.
Division will always be perfectly divisible and zero will never be a denominator.
Restriction of case for input is optional.
Number Format
0 to 20 -> zero,one,two...nineteen,twenty
21 to 99 -> twenty one,twenty two.....ninety eight,ninety nine
100 to 999 -> one hundred, one hundred one, one hundred two...one hundred ten....two hundred fifty....nine hundred ninety eight,nine hundred ninety nine
For negative numbers: Add minus
to its positive equivalent
Operation Format
Addition: one plus two
Subtraction: one minus two
Multiplication: one time two #Note that for one on the left of multiplication, it is one time and not times
two times one hundred
Division: forty divided by two
Examples:
o/p <- input
20 four times five
35 twenty plus fifteen
70 fifty plus five times four
-90 minus one time ninety
25 twenty one minus minus four
45 ninety divided by two
700 one time seven hundred
555 one hundred eleven times two plus three hundred thirty three
99 one hundred plus minus one
45 forty five plus two hundred times zero
4 four
-3 three minus three minus three
This is code-golf so shortest code wins
code-golf
add a comment |Â
up vote
1
down vote
favorite
A simplistic version of English numeral calculator
Task
Write a program that takes a string as input and outputs the result of the expression.
Rules
The input string will be worded and not numeral.
There will be no parentheses.
Order of calculation will be divide,multiply,subtract then add.
For same operations, the calculations must be done from left to right.
All input numbers will be integers from -999 to 999 (both inclusive)
Output will be an integer of any range.
Division will always be perfectly divisible and zero will never be a denominator.
Restriction of case for input is optional.
Number Format
0 to 20 -> zero,one,two...nineteen,twenty
21 to 99 -> twenty one,twenty two.....ninety eight,ninety nine
100 to 999 -> one hundred, one hundred one, one hundred two...one hundred ten....two hundred fifty....nine hundred ninety eight,nine hundred ninety nine
For negative numbers: Add minus
to its positive equivalent
Operation Format
Addition: one plus two
Subtraction: one minus two
Multiplication: one time two #Note that for one on the left of multiplication, it is one time and not times
two times one hundred
Division: forty divided by two
Examples:
o/p <- input
20 four times five
35 twenty plus fifteen
70 fifty plus five times four
-90 minus one time ninety
25 twenty one minus minus four
45 ninety divided by two
700 one time seven hundred
555 one hundred eleven times two plus three hundred thirty three
99 one hundred plus minus one
45 forty five plus two hundred times zero
4 four
-3 three minus three minus three
This is code-golf so shortest code wins
code-golf
Ignore my now deleted comment.. The expression is the input, and the number is the output.. I had it the other way around because usually left is input and right is output.
– Kevin Cruijssen
1 hour ago
1
Duplicate? - I think that's close enough to be considered as such.
– Kirill L.
1 hour ago
1
It's very close indeed. But I think this one is better specified and has more reasonable limitations.
– Arnauld
1 hour ago
@Arnauld I will keep this open then but if others think differently, just mark it as duplicate.
– Vedant Kandoi
59 mins ago
1
I sayone times two
. Is usingtime
normal?
– Jo King
9 mins ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
A simplistic version of English numeral calculator
Task
Write a program that takes a string as input and outputs the result of the expression.
Rules
The input string will be worded and not numeral.
There will be no parentheses.
Order of calculation will be divide,multiply,subtract then add.
For same operations, the calculations must be done from left to right.
All input numbers will be integers from -999 to 999 (both inclusive)
Output will be an integer of any range.
Division will always be perfectly divisible and zero will never be a denominator.
Restriction of case for input is optional.
Number Format
0 to 20 -> zero,one,two...nineteen,twenty
21 to 99 -> twenty one,twenty two.....ninety eight,ninety nine
100 to 999 -> one hundred, one hundred one, one hundred two...one hundred ten....two hundred fifty....nine hundred ninety eight,nine hundred ninety nine
For negative numbers: Add minus
to its positive equivalent
Operation Format
Addition: one plus two
Subtraction: one minus two
Multiplication: one time two #Note that for one on the left of multiplication, it is one time and not times
two times one hundred
Division: forty divided by two
Examples:
o/p <- input
20 four times five
35 twenty plus fifteen
70 fifty plus five times four
-90 minus one time ninety
25 twenty one minus minus four
45 ninety divided by two
700 one time seven hundred
555 one hundred eleven times two plus three hundred thirty three
99 one hundred plus minus one
45 forty five plus two hundred times zero
4 four
-3 three minus three minus three
This is code-golf so shortest code wins
code-golf
A simplistic version of English numeral calculator
Task
Write a program that takes a string as input and outputs the result of the expression.
Rules
The input string will be worded and not numeral.
There will be no parentheses.
Order of calculation will be divide,multiply,subtract then add.
For same operations, the calculations must be done from left to right.
All input numbers will be integers from -999 to 999 (both inclusive)
Output will be an integer of any range.
Division will always be perfectly divisible and zero will never be a denominator.
Restriction of case for input is optional.
Number Format
0 to 20 -> zero,one,two...nineteen,twenty
21 to 99 -> twenty one,twenty two.....ninety eight,ninety nine
100 to 999 -> one hundred, one hundred one, one hundred two...one hundred ten....two hundred fifty....nine hundred ninety eight,nine hundred ninety nine
For negative numbers: Add minus
to its positive equivalent
Operation Format
Addition: one plus two
Subtraction: one minus two
Multiplication: one time two #Note that for one on the left of multiplication, it is one time and not times
two times one hundred
Division: forty divided by two
Examples:
o/p <- input
20 four times five
35 twenty plus fifteen
70 fifty plus five times four
-90 minus one time ninety
25 twenty one minus minus four
45 ninety divided by two
700 one time seven hundred
555 one hundred eleven times two plus three hundred thirty three
99 one hundred plus minus one
45 forty five plus two hundred times zero
4 four
-3 three minus three minus three
This is code-golf so shortest code wins
code-golf
code-golf
edited 42 mins ago
asked 1 hour ago
Vedant Kandoi
1216
1216
Ignore my now deleted comment.. The expression is the input, and the number is the output.. I had it the other way around because usually left is input and right is output.
– Kevin Cruijssen
1 hour ago
1
Duplicate? - I think that's close enough to be considered as such.
– Kirill L.
1 hour ago
1
It's very close indeed. But I think this one is better specified and has more reasonable limitations.
– Arnauld
1 hour ago
@Arnauld I will keep this open then but if others think differently, just mark it as duplicate.
– Vedant Kandoi
59 mins ago
1
I sayone times two
. Is usingtime
normal?
– Jo King
9 mins ago
add a comment |Â
Ignore my now deleted comment.. The expression is the input, and the number is the output.. I had it the other way around because usually left is input and right is output.
– Kevin Cruijssen
1 hour ago
1
Duplicate? - I think that's close enough to be considered as such.
– Kirill L.
1 hour ago
1
It's very close indeed. But I think this one is better specified and has more reasonable limitations.
– Arnauld
1 hour ago
@Arnauld I will keep this open then but if others think differently, just mark it as duplicate.
– Vedant Kandoi
59 mins ago
1
I sayone times two
. Is usingtime
normal?
– Jo King
9 mins ago
Ignore my now deleted comment.. The expression is the input, and the number is the output.. I had it the other way around because usually left is input and right is output.
– Kevin Cruijssen
1 hour ago
Ignore my now deleted comment.. The expression is the input, and the number is the output.. I had it the other way around because usually left is input and right is output.
– Kevin Cruijssen
1 hour ago
1
1
Duplicate? - I think that's close enough to be considered as such.
– Kirill L.
1 hour ago
Duplicate? - I think that's close enough to be considered as such.
– Kirill L.
1 hour ago
1
1
It's very close indeed. But I think this one is better specified and has more reasonable limitations.
– Arnauld
1 hour ago
It's very close indeed. But I think this one is better specified and has more reasonable limitations.
– Arnauld
1 hour ago
@Arnauld I will keep this open then but if others think differently, just mark it as duplicate.
– Vedant Kandoi
59 mins ago
@Arnauld I will keep this open then but if others think differently, just mark it as duplicate.
– Vedant Kandoi
59 mins ago
1
1
I say
one times two
. Is using time
normal?– Jo King
9 mins ago
I say
one times two
. Is using time
normal?– Jo King
9 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
Python 2, 333 331 330 309 307 bytes
lambda s:eval(''.join([[w and`N(w,0)*100+N(w,2)`,'+-*/'['pmtd'.find(w[:1])]][i%2]for i,w in enumerate(re.split(' *(plus|minus|times?|divided by) ',s))]))
N=lambda x,y:sum(('zeontwthfofisiseeiniteel'.find(w[:2])/2+10*(w[-3:]in'lveen'))*10**('ty'==w[-2:])for w in x.rpartition('hundred')[y].split())
import re
Try it online!
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Python 2, 333 331 330 309 307 bytes
lambda s:eval(''.join([[w and`N(w,0)*100+N(w,2)`,'+-*/'['pmtd'.find(w[:1])]][i%2]for i,w in enumerate(re.split(' *(plus|minus|times?|divided by) ',s))]))
N=lambda x,y:sum(('zeontwthfofisiseeiniteel'.find(w[:2])/2+10*(w[-3:]in'lveen'))*10**('ty'==w[-2:])for w in x.rpartition('hundred')[y].split())
import re
Try it online!
add a comment |Â
up vote
4
down vote
Python 2, 333 331 330 309 307 bytes
lambda s:eval(''.join([[w and`N(w,0)*100+N(w,2)`,'+-*/'['pmtd'.find(w[:1])]][i%2]for i,w in enumerate(re.split(' *(plus|minus|times?|divided by) ',s))]))
N=lambda x,y:sum(('zeontwthfofisiseeiniteel'.find(w[:2])/2+10*(w[-3:]in'lveen'))*10**('ty'==w[-2:])for w in x.rpartition('hundred')[y].split())
import re
Try it online!
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Python 2, 333 331 330 309 307 bytes
lambda s:eval(''.join([[w and`N(w,0)*100+N(w,2)`,'+-*/'['pmtd'.find(w[:1])]][i%2]for i,w in enumerate(re.split(' *(plus|minus|times?|divided by) ',s))]))
N=lambda x,y:sum(('zeontwthfofisiseeiniteel'.find(w[:2])/2+10*(w[-3:]in'lveen'))*10**('ty'==w[-2:])for w in x.rpartition('hundred')[y].split())
import re
Try it online!
Python 2, 333 331 330 309 307 bytes
lambda s:eval(''.join([[w and`N(w,0)*100+N(w,2)`,'+-*/'['pmtd'.find(w[:1])]][i%2]for i,w in enumerate(re.split(' *(plus|minus|times?|divided by) ',s))]))
N=lambda x,y:sum(('zeontwthfofisiseeiniteel'.find(w[:2])/2+10*(w[-3:]in'lveen'))*10**('ty'==w[-2:])for w in x.rpartition('hundred')[y].split())
import re
Try it online!
edited 5 mins ago
answered 45 mins ago


TFeld
12.9k2836
12.9k2836
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175369%2fworded-calculator%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Ignore my now deleted comment.. The expression is the input, and the number is the output.. I had it the other way around because usually left is input and right is output.
– Kevin Cruijssen
1 hour ago
1
Duplicate? - I think that's close enough to be considered as such.
– Kirill L.
1 hour ago
1
It's very close indeed. But I think this one is better specified and has more reasonable limitations.
– Arnauld
1 hour ago
@Arnauld I will keep this open then but if others think differently, just mark it as duplicate.
– Vedant Kandoi
59 mins ago
1
I say
one times two
. Is usingtime
normal?– Jo King
9 mins ago