25
How many atoms in the hydrocarbon?
A hydrocarbon is a chemical compound which consists of only hydrogen and carbon atoms. For this challenge, we will only consider the three simplest kinds of hydrocarbons: alkanes, alkenes, and alkynes with no branches.
An alkane with \$n\$ carbon atoms contains \$2n+2\$ hydrogen atoms. An alkene with \$n\$ carbon atoms contains \$2n\$ hydrogen atoms. An alkyne with \$n\$ carbon atoms contains \$2n-2\$ hydrogen atoms.
Each kind of hydrocarbon is named with a prefix indicating the number of carbon atoms it contains, followed by the suffix ane
, ene
, or yne
if it is an alkane, alkene, or alkyne respectively. The numerical prefixes are as follows:
1 <-> meth
2 <-> eth
3 <-> prop
4 <-> but
5 <-> pent
6 <-> hex
7 <-> hept
8 <-> oct
9 <-> non
10 <-> dec
For example, we can see propane
has 3 carbon and 8 hydrogen atoms, and heptyne
has 7 carbon and 12 hydrogen atoms.
Challenge
Your task is to write a function or program that receives a string or list of characters representing the name of a hydrocarbon, and produces or outputs the number of carbon and hydrogen atoms in a molecule of that hydrocarbon.
For each of the 30 hydrocarbon names, the code must accept at least one possible capitalization of that name. For example, it is fine if your code works for mEthane
but not methane
, and ETHAnE
but not ETHANE
, and Propane
but not propane
. The inputs methene
and methyne
may give undefined output since those are not real chemicals.
The input and output can be in any convenient format. You don't need to label which number represents carbon and which represents hydrogen, just keep the order of the numbers consistent.
You may assume the input will correspond to a valid hydrocarbon, and there are at most 10 carbon atoms.
Examples
Possible Input -> Possible Output
Methane -> 1 4
propane -> 3 8
Heptyne -> 7 12
Decene -> 10 20
Rules
- No standard loopholes.
- Shortest code in bytes wins.
3@Grimmy Yes, if it works for
hexANe
that is good enough. And it is reasonable to allow undefined behaviour formethene
andmethyne
since they aren't real chemicals, though I wasn't expecting that to help anyone's solutions. – 79037662 – 2019-12-06T14:44:23.500Wouldn't
methyne
just be pure carbon? – Skyler – 2019-12-06T19:34:13.3331@Skyler As was pointed out in a now-deleted comment, the
-yne
suffix actually means the molecule has a triple bond between carbon atoms. Obviously a single carbon atom does not have a triple bond in it, so we don't call itmethyne
. – 79037662 – 2019-12-06T19:38:45.857@Skyler Carbon isn’t a noble gas, so “pure carbon” isn’t a thing. – Grimmy – 2019-12-07T02:41:23.530
1"the code must accept at least one possible capitalization of that name" <-- isn't this a loophole that allows you to just require the invoking user to encode the result in the case bits? Then you can say inputs that produce wrong outputs are just not the supported capitalization. – R.. GitHub STOP HELPING ICE – 2019-12-07T21:29:02.327
@R.. Is it a loophole if the author intended it to be possible? I wrote this challenge knowing one could store information in the capitalization. Not all languages are able to take advantage of this though. – 79037662 – 2019-12-08T00:27:27.247
1@79037662: Well, it's not clear if it was intentional or not, but I think the problem is very different if that's allowed. – R.. GitHub STOP HELPING ICE – 2019-12-08T00:46:59.897
@Grimmy Something doesn't have to be a noble gas to be pure (e.g. "pure gold"). – Skyler – 2019-12-12T15:13:27.240
@Skyler Now you’re just switching up the meaning of “pure”. When you said methyne would just be pure carbon, you obviously meant something with molecular formula C, not something composed entirely of C (that would be graphene). Pure gold is like graphene in that regard. – Grimmy – 2019-12-12T16:02:39.430
ah, I see what you mean. – Skyler – 2019-12-12T16:49:05.067