You work for a company that wants to make a user-friendly calculator, and thus you have been tasked with adding the ability for users to use "numerical shorthands," that is, letters that represent numerical values, such as
1000. Because your company wants to save money on storage in said calculators, you must minimize your code as much as possible to reduce the cost of storage.
You are to create a function that either reads an expression as input from STDIN or takes it as a parameter and returns its evaluation or prints it to STDOUT.
Let me do some definitions. First off, we have the input, which I call an expression. This can be something like the following:
x + y / z
Within this expression we have three numbers:
z, separated by operators (
/). These numbers are not necessarily positive integers (or even integers). What complicates things is when we have to evaluate shorthands contained within numbers. For example, with
for purposes of evaluation, we split this up into three numbers:
1000 (which is
15. Then, according to the rules, we combine them to obtain
2*1000 + 15 = 2015
Hopefully this makes it a bit easier to understand the following rules.
N.B. Unless specified otherwise, you can interpret the word "numbers" or its synonyms to include shorthands.
The following constitutes the numerical shorthands your function must be able to process:
k, m, b, t, and e.
k, m, b, and tcorrespond to the values
1000, 1000000, 1000000000, and 1000000000000respectively (one thousand, one million, one billion, and one trillion). The
eshorthand will always be followed by another number,
n, and represents
10^n. You must allow for numerical shorthands to be present in
nand present before
e. For example,
For simplicity's sake, if a number has the shorthand
ein it, it will only be used once.
Any number (shorthands included) before a shorthand is multiplied by it. e.g.
120kkwould be evaluated as
120 * 1000 * 1000. If there is no number before it, you must assume that the number is 1 (like how you might, in mathematics, treat a variable
10^10. Another example:
2*1000000*2*1000(nothing is added to it).
Any number (shorthands do not apply) following the last shorthand in a number containing a shorthand is added to it. e.g.
2k12would be evaluated as
2*1000 + 12. The exception to this is if the shorthand
eis used, in which case the number (shorthands included) following
ewill be treated as
nand evaluated as
10^n(see the first rule).
Your function must be able to process the operators
+, -, *, and /which are addition, subtraction, multiplication, and division respectively. It may process more, if you so desire.
Operations are evaluated according to the order of operations.
Numbers in shorthands are not only integers.
3.5b1.2is valid and is to be evaluated as
3.5*1000000000 + 1.2 = 3500000001.2
Built-ins are not allowed, if they exist for this sort of thing. The exception that I'll add would be if your language automatically converts large numbers to scientific notation, in which case that is admissible for your output.
Shortest code in bytes wins, standard loopholes applying.
The input will be an expression with each number and operator separated by spaces. Numbers may or may not contain a shorthand. A sample is shown below:
10 + 1b - 2k
Your function must output the evaluation of the expression as a number. It is admissible to use scientific notation if the output would be too large to display. You must have at least three decimal places if the number is not an integer. It is admissible if you retain these decimal places if the number is an integer.
1 + 4b / 10k11
e2 + k2ke-1 - b12
This is my first challenge posted (with some help from users on the sandbox). If anything is unclear, make it known to me and I will do my best to clarify.