- Jimmy has invented a new kind of calculator that works with words
rather than numbers.
Input is read from stdin and consists of up to 1000 commands, one per
line.
Each command is a definition, a calculation or a clear.
All tokens within a command are separated by single spaces.
A definition has the format def x y where x is a variable name and y
is an integer in the range [-1000, 1000].
Existing definitions are replaced by new ones i.e. if x has been
defined previously, defining x again erases its old definition.
Variable names consist of 1-30 lowercase characters.
No two variables are ever defined to have the same value at the same
time.
The clear command erases all existing variable definitions.
A calculation command starts with the word calc, and is followed by
one or more variable names separated by addition or subtraction
operators.
The end of a calculation command is an equals sign.
The goal is to write a program for Jimmy's calculator.
Some rules are:
- The program should produce no output for definitions, but for
calculations it should output the value of the calculation.
Where there is no word for the result, or some word in a calculation
has not been defined, then the output should be unknown. (The word
unknown is never used as a variable name.)
Your solution may only import content from the sys module.
Your solution may not use the eval() function.
Here is a sample input below:
calc foo + bar =
def bar 7
def programming 10
calc foo + bar =
def is 4
def fun 8
calc programming - is + fun =
def fun 1
calc programming - is + fun =
clear
And a sample output:
foo + bar = unknown
foo + bar = programming
programming - is + fun = unknown
programming - is + fun = bar
Jimmy has invented a new kind of calculator that works with words
rather than numbers.
Input is read from stdin and consists of up to 1000 commands, one
per line.
Each command is a definition, a calculation or clear.
All tokens within a command are separated by single spaces.
A definition has the format def x y where x is a variable name and y is an integer in the range [-1000, 1000].
Existing definitions are replaced by new ones i.e. if x has been defined previously, defining x again erases its old definition.
Variable names consist of 1-30 lowercase characters.
No two variables are ever defined to have the same value at the
same time.
The clear command erases all existing variable definitions.
A calculation command starts with the word calc, and is followed
by one or more variable names separated by addition or subtraction
operators.
The end of a calculation command is an equals sign.
The goal is to write a program for Jimmy's calculator. Some rules are:
- The program should produce no output for definitions, but for calculations it should output the value of the calculation.
- Where there is no word for the result, or some word in a calculation has not been defined, then the output should be
unknown. (The word unknown is never used as a variable name.)
- Your solution may only import content from the
sys module.
- Your solution may not use the
eval() function.
Here is a sample input below:
calc foo + bar =
def bar 7
def programming 10
calc foo + bar =
def is 4
def fun 8
calc programming - is + fun =
def fun 1
calc programming - is + fun =
clear
And the corresponding output:
foo + bar = unknown
foo + bar = programming
programming - is + fun = unknown
programming - is + fun = bar
It does feel quite clunky but it gets the job done,. I am just wondering if anybody has any better ways in which it could be improved upon - and hearing your own different approaches and solutions would be awesome!