15
3
Introduction
One of the first things I learned in chemistry, was giving the formula for the combustion of alkanes. The most basic example was: 2CH4 + 4O2 > 4H2O + 2CO2
. Note that the equation could have been simplified by dividing every coefficient by 2, we would get CH4 + 2O2 > 2H2O + CO2
. To make things easier, we are going to ignore this step.
You can see that with the combustion of any alkane, oxygen is used. After the reaction, only CO2 and H2O are made.
The task:
Give a complete program that takes input from STDIN or the nearest possible equivalent, and outputs the total reaction using STDOUT or the nearest possible equivalent.
The input will always be in the form C(n)H(2n+2)
, with n > 0
. These are some example inputs:
CH4
C2H6
C3H8
C4H10
C5H12
C6H14
etc.
A useful tip:
Every alkane has a standard pattern in the combustion reaction:
2C(n)H(2n+2) + (3n+1)O2 > (2n+2)H2O + (2n)CO2
e.g.
C4H10
gives the following equation: 2C(4)H(2*4+2) + (3*4+1)O2 > (2*4+2)H2O + (2*4)CO2
. After calculating everything, we get this final equation: 2C4H10 + 13O2 > 10H2O + 8CO2
Examples:
input: CH4
output: 2CH4 + 4O2 > 4H2O + 2CO2
input: C3H8
output: 2C3H8 + 10O2 > 8H2O + 6CO2
input: C4H10
output: 2C4H10 + 13O2 > 10H2O + 8CO2
input: C12H26
output: 2C12H26 + 37O2 > 26H2O + 24CO2
Rules:
- You need to provide a full program.
- Your program needs to take input from STDIN, or the nearest equivalent if not possible.
- Your program needs to output using STDOUT, or the nearest equivalent if not possible.
- Note that I used spaces for the examples to increase readability, these are not necessary.
2CH4 + 4O2 > 4H2O + 2CO2
and2CH4+4O2>4H2O+2CO2
are both valid outputs. However, if you consistently use spaces for the output, you get a -10% bonus - This is code-golf, so the program with the least amount of bytes wins!
totally different but related: Drawing Lewis Structures of Alkanes
– Not that Charles – 2015-11-20T20:18:02.320So we can assume that the input will be correct formulas? – TanMath – 2015-11-20T20:26:23.007
@TanMath, yes, you don't have to worry about invalid formulas – Adnan – 2015-11-20T20:28:47.093
"consistently use spaces" - would consistently using 0 spaces count? – Mego – 2015-11-20T22:59:34.757
1@Mego, no. Using 0 spaces is not using spaces at all, so the rule doesn't apply – Adnan – 2015-11-20T23:07:01.043
Is C1H4 valid? Or it has to be CH4? – Zereges – 2015-11-20T23:23:31.640
@Zereges, it has to be CH4 – Adnan – 2015-11-20T23:25:22.860
I'm disappointed at the lack of Unicode subscripts. – Neil – 2015-11-21T17:27:40.320
Is a newline character required at the end of the output? – Tom Carpenter – 2015-11-22T04:18:07.293
@TomCarpenter no, that is not required – Adnan – 2015-11-22T06:38:52.327