Help me with my chemistry homework!

3

Near the middle of the semester in chemistry, we get problems like:

Convert 1.283850057 to Scientific Notation

and

Convert 3.583✕106 to Standard Notation

This is just stupid busy work for people who already know how to do this. So…

The Challenge

Given an input in any reasonable, convenient format, in either standard notation or scientific notation, output the same number in the other notation. However, note that e will not be accepted in place of *10^, as this gives a significant advantage to languages with builtins with e.

If not otherwise specified, standard notation will take this format:

x             (an integer)
xxxxxxxxxx    (a large integer with no more than 10 digits)
x.xxxx        (a float)
x.xxxxxxxxxx  (a float with no more than 10 x's after the decimal point)

While scientific notation will take this format:

x*10^x
x.zzzz*10^z (x will be >= 1)
x*10^-x
x.zz*10^-xx (x will be >= 1)

I/O

Input will never contain more than 10 leading+10 trailing digits, and you may decide how to deal with trailing zeroes after the decimal point. The exponent will never be so large as to max out your language's allowance for large numbers. You should only need to handle up to about 1010.

input => output

---scientific to standard---

4.5*10^7 => 45000000
1*10^0 => 1
8.9*10^-2 => 0.089
1.0001*10^5 => 100010
9.999999*10^9 => 9999999000
4.00038*10^2 => 400.038

---standard to scientific---

7354 = 7.354*10^3
385.43 => 3.8543*10^2
0.32 = 3.2*10^-1
0.00044 => 4.4*10^-4
0.053 => 5.3*10^-2

As always, standard loopholes are disallowed. This is , so shortest bytes wins!

FantaC

Posted 2017-12-11T20:44:15.070

Reputation: 1 425

@Uriel that is true and yes, this isn't an extremely hard challenge (litotes), but it also involves doing the opposite conversion, which adds a degree of complexity – FantaC – 2017-12-11T20:55:01.703

I realize I answered already, but do we have to use *10^ for e when outputting scientific notation? – Giuseppe – 2017-12-11T21:14:36.710

1@Giuseppe Yessir – FantaC – 2017-12-11T21:55:13.683

Answers

4

R, 70 bytes

function(s)sub("e","*10^",format(eval(parse(t=s)),sc=!grepl("\\^",s)))

Try it online!

An anonymous function. Takes input as a string and outputs as a string.

This function evaluates the string eval(parse(t=s)), then formats it, with the flag scientific set to whether the string contains a ^ or not. Then it replaces e with *10^ to match the right output spec.

Giuseppe

Posted 2017-12-11T20:44:15.070

Reputation: 21 077

2

Python 3, 100 bytes

lambda a:eval(a.replace("*10^","e+"))if type(a)==str else re.sub("0*e\+?0*","*10^","%e"%a)
import re

Try it online!

Neil

Posted 2017-12-11T20:44:15.070

Reputation: 2 417

1

JavaScript (ES6), 83 80 bytes

a=>(r=a.split(N='*10^'))[1]?+r.join`e`+'':(+a).toExponential().replace(/e\+?/,N)

An anonymous function. Takes input as a string and outputs as a string.

XavCo7

Posted 2017-12-11T20:44:15.070

Reputation: 274

Hacked on '1.3*10^-8' – l4m2 – 2017-12-13T17:31:20.620

1

Python 2, 87 bytes

lambda s:eval(s.replace('^','**'))if'^'in`s`else re.sub('e\+?','*10^','%e'%s)
import re

Try it online!

TFeld

Posted 2017-12-11T20:44:15.070

Reputation: 19 246

1

Excel VBA, 80 Bytes

Anonymous VBE immediate window function that takes input from cell [A1] and outputs to the VBE immediate window.

v=Evaluate("="&[A1]):e=Int(Log(v)/2.3):?IIf(Instr([A1],"^"),v,v*10^-e &"*10^"&e)

''  Or

v=Evaluate("="&[A1]):e=Int(Log(v)/2.3):?IIf(Instr([A1],"^"),v,v/(10^e)&"*10^"&e)

''  Or

[B1]="="&[A1]:v=[B1]:e=Int(Log(v)/2.3):?IIf(Instr([A1],"^"),v,v*10^-e &"*10^"&e)

''  Or

[B1]="="&[A1]:e=[Int(Log10(B1))]:?IIf(Instr([A1],"^"),[B1],[B1]/(10^e)&"*10^"&e)

Taylor Scott

Posted 2017-12-11T20:44:15.070

Reputation: 6 709