Back to the chemistry class
Python 3, 86 * 0.9 = 77.4 bytes
s=input()
N=int(s.split('H')[1])
print("2%s + %dO2 > %dH20 + %dCO2"%(s,N*1.5-2,N,N-2))
Extracts the number of H
's rather than the number of C
's from the input. This avoid special-casing CH4
and simplifies the output expressions in terms of N=2n+2
.
The output has parameters plugged in via string formatting. The first summand is just the input string, and the rest have calculated numbers plugged in. Note that N*1.5-2
(same as N*3/2-2
) gives a float, but the string formatting converts it to an int.
Java, 0.9*202 = 181.8 bytes
Sometimes, I wonder if I'm just hurting myself with Java.
Thanks to @TNT and @TFeld for shaving off a good 20 bytes!
class A{public static void main(String[]a){String s=a[0].substring(1,a[0].indexOf("H"));long n=Long.parseLong((s.length()>0)?s:"1");System.out.printf("2%s + %dO2 > %dH2O + %dCO2",a[0],3*n+1,2*n+2,2*n);}}
Pretty simple. Basically, I cut the input from C
to H
, and get that substring. If it's nothing, I set n
to one. Otherwise, I set it to the number between C
and H
. The code following just prints it out and puts it into proper notation.
Ungolfed:
class A{ public static void main(String[]a) { String s=a[0].substring(1,a[0].indexOf("H")); long n=Long.parseLong((s.length()>0)?s:"1"); System.out.printf("2%s + %dO2 > %dH2O + %dCO2",a[0],3*n+1,2*n+2,2*n); } }
Python 2, 122 91*0.9 = 81.9 bytes
i=input()
n=2*int(i[1:i.find('H')]or 1)
print'2%s + %dO2 > %dH2O + %dCO2'%(i,n*3/2+1,n+2,n)