30
11
Bernd is a high school student who has some problems in chemistry. In class he has to design chemical equations for some experiments they are doing, such as the combustion of heptane:
C7H16 + 11O2 → 7CO2 + 8H2O
Since mathematics isn't exactly Bernd's strongest subject, he often has a hard time finding the exact ratios between the pro- and educts of the reaction. Since you are Bernd's tutor, it is your job to help him! Write a program, that calculates the amount of each substance needed to get a valid chemical equation.
Input
The input is a chemical equation without amounts. In order to make this possible in pure ASCII, we write any subscriptions as ordinary numbers. Element names always start with a capital letter and may be followed by a minuscule. The molecules are separated with +
signs, an ASCII-art arrow ->
is inserted between both sides of the equation:
Al+Fe2O4->Fe+Al2O3
The input is terminated with a newline and won't contain any spaces. If the input is invalid, your program may do whatever you like.
You may assume, that the input is never longer than 1024 characters. Your program may either read the input from standard input, from the first argument or in an implementation defined way at runtime if neither is possible.
Output
The output of your program is the input equation augmented with extra numbers. The number of atoms for each element must be the same on both sides of the arrow. For the example above, a valid output is:
2Al+Fe2O3->2Fe+Al2O3
If the number for a molecule is 1, drop it. A number must always be a positive integer. Your program must yield numbers such that their sum is minimal. For instance, the following is illegal:
40Al+20Fe2O3->40Fe+20Al2O3
If there is no solution, print
Nope!
instead. A sample input that has no solution is
Pb->Au
Rules
- This is code-golf. The shortest code wins.
- Your program must terminate in reasonable time for all reasonable inputs.
Test Cases
Each test case has two lines: An input and a correct output.
C7H16+O2->CO2+H2O
C7H16+11O2->7CO2+8H2O
Al+Fe2O3->Fe+Al2O3
2Al+Fe2O3->2Fe+Al2O3
Pb->Au
Nope!
5"Since you are Bernds tutor, it is your job to help him!" - I would have thought a tutor should be teaching Bernd to think for himself, rather than write software for him so he doesn't have to :P – naught101 – 2015-01-30T04:44:19.383
1I could be wrong, but this seems like a natural candidate for a programming challenge rather code golf. – DavidC – 2012-10-16T02:06:20.100
The algorithm is not so dificult once you think about it. Hint: Vectors. – FUZxxl – 2012-10-16T05:24:15.117
1I once wrote a chemical equation solver on my TI-89 graphing calculator, using the built-in
solve(
function andeval(
to interpret the input :) – mellamokb – 2012-10-16T12:48:05.8733@mellamokb why don't you post it, you'll get an upvote from me for originality – ratchet freak – 2012-10-16T15:57:41.010
http://www.webqc.org/balance.php?reaction=Al%2BFe2O4%3DAl2O3%2BFe says that Al+Fe2O4->Fe+Al2O3 is 8Al+3Fe2O4->4Al2O3+6Fe – baby-rabbit – 2012-10-16T22:49:11.707
@baby-rabbit: The equation was wrong. Fixed. – FUZxxl – 2012-10-17T05:46:36.473
The equation under Input is still wrong, saying Al+Fe2O4 instead of Fe2O3 – Kuilin Li – 2016-12-25T05:26:46.427
1@KuilinLi It is not wrong, just different. – FUZxxl – 2016-12-25T09:37:59.227
What about ion charges? Cu2+ +2e- ->Cu ? – devRicher – 2016-12-25T22:50:50.140
@devRicher Out of scope. – FUZxxl – 2016-12-25T23:57:53.727