4
Air balloons need a gas that is lighter than air. However, hydrogen is flammable, while helium is not sustainable, so we need a replacement! You must write code that determines whether any given gas is lighter than air.
Input: a molecular chemical formula of a gas (ASCII)
Output: true
if the gas is lighter than air; false
otherwise (see here for consensus on what can be used as true
and false
).
If the chemical formula doesn't represent a gas (at standard conditions), or if it's a nonsense, any behavior is acceptable.
That is to say, you can assume the following:
- The input is a valid chemical formula
- The input is a molecular chemical formula: all element symbols are mentioned only once, and there are no parentheses
- The chemical substance exists at normal conditions
- The chemical substance is a gas at normal conditions
As a special case, H2O
(water vapor) is considered a gas, because you can mix a significant quantity of it with air at standard conditions.
Whether or not a gas is lighter than air can be determined by calculating its molar mass:
Extract the chemical element symbols from the formula. Their atomic masses are given by the following table:
- H - 1
- He - 4
- B - 11
- C - 12
- N - 14
- O - 16
- F - 19
- Ne - 20
- Others - greater values
Calculate the molar mass, which is the sum of all the atomic masses. If the molar mass is less than 29, the gas is lighter than air.
Test cases:
H2 true He true B2H6 true CH4 true C2H2 true C2H4 true HCN true N2 true NH3 true H2O true CO true HF true Ne true O2 false C2H6 false C4H10 false H2S false COH2 false CO2 false SiH4 false NO false BN behavior nor specified: not a gas HBO2 behavior nor specified: not a gas F2O behavior nor specified: incorrect formula CH3 behavior nor specified: incorrect formula HCOOH behavior nor specified: not a molecular formula B(OH)3 behavior nor specified: not a molecular formula XYZ behavior nor specified: nonsense
(Note: this is meant to be an enumeration of all possible inputs that generate true
. If I forgot some, I'll add it. In any case, Wikipedia is the answer for questions like "Does substance X exist?", "Is it a gas?")
A related (but different) question: calculate the molar mass
5Can we assume that
He
areNe
are noble and so don't combine? – Luis Mendo – 2016-05-26T15:00:26.4074This seems like just take your linked "calculate the molar mass" challenge and add a conditional on the output. How does that make this challenge different enough to not be a duplicate? – AdmBorkBork – 2016-05-26T15:03:22.143
1'Others - greater values'. What are the others, and those greater values? Maybe I just don't know enough chemisty, but it's not obvious to me why
F2O
andCH3
and invalid. – Morgan Thrapp – 2016-05-26T15:11:53.360@TimmyD This is significantly easier (fewer possibilities; no floating-point), so I think you can make code significantly smaller. And also maybe different approaches are possible with this challenge (dictionary?). – anatolyg – 2016-05-26T15:12:19.247
Can I assume that the input will not have parentheses? – Leaky Nun – 2016-05-26T15:13:26.843
@LeakyNun Yes (it's a molecular formula - they don't have parentheses); see test cases for other forms that won't appear – anatolyg – 2016-05-26T15:14:36.043
Can I assume that
H10
will not appear (two-digit)? – Leaky Nun – 2016-05-26T15:16:19.640@MorganThrapp Please note that code doesn't need to detect errors. "Behavior not specified" in test cases means "the code can apply some faulty logic and come up with a result". If you don't know any chemistry, you can write an acceptable answer. If you know chemistry then maybe you can write a better answer (though I highly doubt that). – anatolyg – 2016-05-26T15:17:38.073
@LeakyNun
H10
can appear (updated the list of test cases) – anatolyg – 2016-05-26T15:21:49.407Why is F2O an invalid formula? Oxygen Difluoride (https://en.wikipedia.org/wiki/Oxygen_difluoride) appears to exist...
– Tom Will – 2016-05-26T17:29:21.580@TomWill It's in the wrong order - the correct formula is
OF2
. Doesn't matter much though, because code is not required to detect this. – anatolyg – 2016-05-26T17:46:58.230People who voted to close, please explain what is bad about this question now (what is unclear) and how it could be improved (which parts to clarify) – anatolyg – 2016-05-29T14:19:02.923