Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

How many atoms in the hydrocarbon?

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 \2ドルn+2\$ hydrogen atoms. An alkene with \$n\$ carbon atoms contains \2ドルn\$ hydrogen atoms. An alkyne with \$n\$ carbon atoms contains \2ドルn-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.

Answer*

Draft saved
Draft discarded
Cancel
3
  • \$\begingroup\$ yea this is the kind of answer I was looking forward to with the capitalization rule :) \$\endgroup\$ Commented Dec 5, 2019 at 21:34
  • \$\begingroup\$ 82 bytes \$\endgroup\$ Commented Dec 5, 2019 at 22:31
  • \$\begingroup\$ @RobinRyder aahhh, I was too fixated on the match. I messed with the arithmetic as well to cut down three more bytes. \$\endgroup\$ Commented Dec 5, 2019 at 23:06

AltStyle によって変換されたページ (->オリジナル) /