Hi, Seokin,
Not sure that this can be considered as an improvement of your code, but here is my solution:
A = Inner[Times, ElementData[#, "AtomicMass"] & /@ #1, #2, Plus] & ;
A[{"Carbon", "Hydrogen"}, {4, 10}]
As for fancy user input, you can try something similar to the following:
c = "Carbon" ;
h = "Hydrogen" ;
arg = Transpose@Partition[#, 2] & @ (ToExpression[#] & /@ StringSplit["c 4 h 10"]) ;
A @@ arg
and the final version:
A = Inner[Times, ElementData[#, "AtomicMass"] & /@ #1, #2, Plus] & @@ (
Transpose@Partition[#, 2] & @ (ToExpression[#] & /@
StringSplit[#])
) & ;
A["c 4 h 10"]
Also Characters[] function can be used to form input arg without blanks, but it will split 10 into 1 and 0 :
Characters["c4h10"]
I.M.