Is the gas lighter than air?

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

anatolyg

Posted 2016-05-26T14:54:12.880

Reputation: 10 719

5Can we assume that He are Neare noble and so don't combine? – Luis Mendo – 2016-05-26T15:00:26.407

4This 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 and CH3 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.407

Why 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.230

People 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

Answers

1

Perl 5, 52 bytes

51 plus 1 for -p:

$_=/^(B2H6|C2H2|C2?H4|H(CN|F|2O?|e)|N2|NH3|Ne|CO)$/

or

$_=/^(B2H6|C2H2|C2?H4|H(CN|F|2O)|[HN][e2]|NH3|CO)$/

or any of a few similar scripts.

Based (obviously) on MattPutnam's Clojure answer.

msh210

Posted 2016-05-26T14:54:12.880

Reputation: 3 094

1

Retina, 108 104 bytes

[A-Z]
;$&
Ne
xF
F
xxxO
O
xxN
N
xxC
C
xB
B
11$*x
e
xxx
H
x
x(?=x*(\d+))
$1$*x
[A-Z]
30$*x
[^x]

M`x{29}
0

Try it online!

Test suite.

Leaky Nun

Posted 2016-05-26T14:54:12.880

Reputation: 45 011

1

JavaScript (ES6), 119 bytes

s=>s.replace(/([A-Z][a-z]?)(\d*)/g,(_,a,n)=>m+=[11,12,19,1,4,14,20,16]["B C F H HeN NeO".search(a)/2]*(n||1),m=0)&&m<29

Any unknown atom causes the result to compute to NaN which is not less than 29.

Neil

Posted 2016-05-26T14:54:12.880

Reputation: 95 035

1

Clojure, 69 bytes

#(#{"H2""He""B2H6""CH4""C2H2""C2H4""HCN""N2""NH3""H2O""CO""HF""Ne"}%)

If that's the full list of truthy answers...

MattPutnam

Posted 2016-05-26T14:54:12.880

Reputation: 521

1Can you compress it? I see a lot of quote characters there. – anatolyg – 2016-05-28T11:45:25.427

0

Python, 235 Bytes

import re
a={"H":1,"He":4,"B":11,"C":12,"N":14,"O":16,"F":19,"Ne":20}
try:print(str(29>sum([int(p[1])*a[p[0]] for p in [c if c[1]!="" else (c[0],"1") for c in re.findall(r'([A-Z][a-z]*)(\d*)',input())]])).lower())
except:print("false")

Explanation:

import re # For the Regex
a={...} # Defining masses
try... except: # Catches unexpected elements and return false
re.findall(r'([A-Z][a-z]*)(\d*)',input()) # Group capture of the elements (credit to Draxx)
c if c[1]!="" else (c[0],"1") for c in # Sometimes, they may not have numbers after them, thus, if else!
int(p[1])*a[p[0]] # Mass of each elements
str(29>sum(...)).lower() # Boolean whether the statement is true/false translated to lower case string

Or, taking the MattPutnam way of doing things...

Python, 71 Bytes

lambda n:n in"H2 He B2H6 CH4 C2H2 C2H4 HCN N2 NH3 H2O CO HF Ne".split()

(Credit to @Lynn)

Alexander Craggs

Posted 2016-05-26T14:54:12.880

Reputation: 469

lambda n:n in"H2 He B2H6 CH4 C2H2 C2H4 HCN N2 NH3 H2O CO HF Ne".split() might work – Lynn – 2016-05-29T10:53:46.333