How many atoms in the hydrocarbon?

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.

79037662

Posted 2019-12-05T20:31:44.057

Reputation: 1 739

3@Grimmy Yes, if it works for hexANe that is good enough. And it is reasonable to allow undefined behaviour for methene and methyne since they aren't real chemicals, though I wasn't expecting that to help anyone's solutions. – 79037662 – 2019-12-06T14:44:23.500

Wouldn't methyne just be pure carbon? – Skyler – 2019-12-06T19:34:13.333

1@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 it methyne. – 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

Answers

15

05AB1E,  27 26  24 bytes

Saved 1 byte thanks to @KevinCruijssen
Saved 2 bytes thanks to @Grimmy

Note: This code was based on my understanding that only the capitalization of the first letter was customizable. It has since been clarified that it applies to all letters, leading to this significantly shorter answer by Grimmy.

Expects mEpBPhHond for the capitalization of the first letter.

ćC•>·¿*•s÷θD>,₂I3(èH÷+·,

Try it online!

How?

We define the following magic constant:

$$M=1902159854$$

The number of carbon atoms is given by:

$$c=\left(\left\lfloor\frac{M}{n}\right\rfloor\bmod 10\right)+1$$

where \$n\$ is the first character of the input turned into an 05AB1E code point (0-9, A-Z, a-z, ...).

 input |  n  | floor(M/n) | mod 10
-------+-----+------------+--------
 meth- |  48 |   39628330 |   0
 Eth-  |  14 |  135868561 |   1
 prop- |  51 |   37297252 |   2
 But-  |  11 |  172923623 |   3
 Pent- |  25 |   76086394 |   4
 hex-  |  43 |   44236275 |   5
 Hept- |  17 |  111891756 |   6
 oct-  |  50 |   38043197 |   7
 non-  |  49 |   38819588 |   8
 dec-  |  39 |   48773329 |   9

Commented

                         # example input: 'Heptyne'
ć                        # extract the first character -> 'H'
 C                       # turn it into an integer -> 17
  •>·¿*•                 # push the magic constant 1902159854
        s÷               # swap + integer division -> 1902159854 / 17 = 111891756
          θ              # keep the last digit (therefore a modulo 10) -> 6
           D>            # duplicate and increment the copy
             ,           # print the number of carbon atoms
              ₂          # push 26
               I         # push the input string
                3(è      # extract the 3rd to last character -> 'y'
                   H     # parse it as base-36 -> 34
                    ÷    # integer division -> 26 / 34 = 0
                     +   # add to the number of carbon atoms - 1 -> 6
                      ·  # double
                       , # print the number of hydrogen atoms

Arnauld

Posted 2019-12-05T20:31:44.057

Reputation: 111 334

2

I'm sure @Grimmy will come in and golf 5 bytes away, since he usually does that to my 05AB1E answers. ;) But here is a straightforward -1 byte for now: ¹ can be s (since the remainder-list from ć is still on the stack), and then the second Ç can be removed: 26 bytes.

– Kevin Cruijssen – 2019-12-06T07:47:13.840

-2 bytes. Note that there's no need to brute-force the magic constant, you can just use the chinese theorem. – Grimmy – 2019-12-06T13:42:13.673

1

My bad, the title of your answer says 05AB1E (legacy), but the 24 I suggested only works on modern 05AB1E due to my use of C and H. Here's a 23 that works on both modern and legacy: Ç•cýºÍ•s÷ćθ>=s3(è6%-Ì·, (expects MEpbPhHond capitalization).

– Grimmy – 2019-12-06T16:54:27.543

5

R, 96 86 82 79 bytes

`!`=utf8ToInt;(5-rev(K<-!scan(,""))[3]%%16)/2*0:1+match(K[1],!"mepbPhHond")*1:2

Try it online!

Accepts the prefixes with capitalization mepbPhHond in order; returns c(Carbon, Hydrogen).

yea for this answer.

Thanks to Robin Ryder for golfing down 4 bytes.

Giuseppe

Posted 2019-12-05T20:31:44.057

Reputation: 21 077

yea this is the kind of answer I was looking forward to with the capitalization rule :) – 79037662 – 2019-12-05T21:34:53.113

82 bytes – Robin Ryder – 2019-12-05T22:31:49.853

@RobinRyder aahhh, I was too fixated on the match. I messed with the arithmetic as well to cut down three more bytes. – Giuseppe – 2019-12-05T23:06:19.993

5

JavaScript (ES6), 62 bytes

Similarly to other answers, expects all names in lowercase, except Pent- and Hept-. Returns [carbon, hydrogen].

s=>[n='epbPhHond'.search(s[0])+2,n+/a/.test(s)-/y/.test(s)<<1]

Try it online!


JavaScript (ES6),  84  79 bytes

A version where the capitalization doesn't matter. Returns [carbon, hydrogen].

s=>[n=-~'519.80.76423'[parseInt(s[0]+s[2],28)%17],n+/a/.test(s)-/y/.test(s)<<1]

Try it online!

How?

Carbon

We can concatenate the first and third characters of each prefix to build a unique string identifier.

Example: methmt

We can convert this string ID to an integer ID in \$[0..11]\$ (still unique) by parsing it as base-28 and applying a modulo \$17\$ (these values were found with a brute-force search).

Because the character set of base-28 is '0' to 'r', the parsing stops if a character above 'r' is encountered.

Example: mtm\$22 \bmod 17=5\$

The results are summarized in the following table:

 input | key  | base 28 -> dec. | mod 17
-------+------+-----------------+--------
 meth- | 'm'  |        22       |    5
 eth-  | 'eh' |       409       |    1
 prop- | 'po' |       724       |   10
 but-  | 'b'  |        11       |   11
 pent- | 'pn' |       723       |    9
 hex-  | 'h'  |        17       |    0
 hept- | 'hp' |       501       |    8
 oct-  | 'o'  |        24       |    7
 non-  | 'nn' |       667       |    4
 dec-  | 'dc' |       376       |    2

Hydrogen

Neither 'a' nor 'y' appears in any of the prefixes. Therefore, it is safe to test whether the suffix is -ane or -yne by looking for these letters in the entire input string.

The following expression:

/a/.test(s) - /y/.test(s)

evaluates to either \$-1\$ for -yne, \$0\$ for -ene or \$1\$ for -ane.

Given the number \$n\$ of carbon atoms, the number of hydrogen atoms is:

(n + /a/.test(s) - /y/.test(s)) << 1

(and because << has a higher precedence than + and -, the parentheses can be omitted)

Arnauld

Posted 2019-12-05T20:31:44.057

Reputation: 111 334

Hi Arnauld - would you mind adding some explanation - especially for the second version? I see that it works, but have no idea how (let alone how did you get there...) – G0BLiN – 2019-12-06T22:06:08.043

1@G0BLiN I've added an explanation of my original answer. Please let me know if something is still unclear. – Arnauld – 2019-12-07T11:55:16.450

4

Pyth, 33 31 bytes

Kx"mepbPhHond"hQhKy+K/%CePPQ7 3

Try it online!

Uses, as you can see, mepbPhHond capitalization. Probably can be golfed much further. Returns carbon, hydrogen on newlines

How it works

Kx"mepbPhHond"hQhKy+K/%CePPQ7 3

 x            hQ    - The index of the first letter of the input
  "mepbPhHond"      - in "mepnPhHond"
K               hK  - Assign to K... and then print K+1

                       CePPQ       - The codepoint of the third last character 
                                       in the input (Q[:-1][:-1][-1])
                      /%     7 3    - Modulo 7 divided by 3 (ane, ene, yne -> 2, 1, 0)
                   +K               - ... plus K
                  y                 - ... times 2, and print implicitly

frank

Posted 2019-12-05T20:31:44.057

Reputation: 941

4

Wolfram Language 28 bytes

I'm unsure whether this violates the rules. After all, it simply retrieves the information from an online database, WolframAlpha.

WolframAlpha[#<>" formula"]&

This works with or without caps.

Example

WolframAlpha[#<>" formula"]&["heptyne"]

heptyne

DavidC

Posted 2019-12-05T20:31:44.057

Reputation: 24 524

2

this seems like it's in violation of this standard loophole

– Giuseppe – 2019-12-06T17:11:37.577

@Guiseppe, I tend to agree with you. I'm leaving the answer posted anyway. Some people may find it of interest to know that such information (and much more) is readily available. – DavidC – 2019-12-07T20:26:30.813

4

05AB1E, 16 12 11 bytes

.uJ4ôCx<O1ǝ

Try it online!

This takes full advantage of the rule that only one possible capitilization has to be handled for each input. All inputs are at least 6 letters long, so we can store 6 bits of data in the capitilization. 4 of those bits are used to store the carbon count, and the remaining 2 store half of the extra hydrogen count (0 for alkynes, 1 for alkenes, 2 for alkanes).

.u             # uppercase? (returns a list of 0s and 1s)
  J            # join that list to a string
   4ô          # split in chunks of 4 digits
     C         # convert each chunk from binary
      x        # double each, without popping the original list
       <       # -1 from each
        O      # sum
         1ǝ    # replace the second element of the list with this sum

Alternative 11:

.u2β3‰<.¥¦ƶ

Try it online!

Grimmy

Posted 2019-12-05T20:31:44.057

Reputation: 12 521

1Perfectly acceptable. – 79037662 – 2019-12-06T14:45:01.717

1Stupid rules deserve brilliant answers. ;) – COTO – 2019-12-08T12:13:50.097

3

Python 3.8, 60 bytes

As other answers, all the names except for those starting with Prop- or Hept-, must be lowercase.

lambda x:(c:=' mePbphHond'.find(x[0]),c+'ea'.find(x[-3])<<1)

-4 bytes thanks to @Arnauld

Try it online!


Fun fact:

Another interesting fact I observerd is the following one:

  • \$ord(a)=97\to9-7=2\$ which is the number we have to add for alkanes.
  • \$ord(e)=101\to1-0-1=0\$ which is the number we have to add for alkenes.
  • \$ord(y)=121\to1-2-1=-2\$ which is the number we have to add for alkynes.

I don't think that I can modify my answer using this way to be less bytes thank it is right now, but I thought it was worth mentioning and maybe another answer can use it effectively!

An implementation of the above idea is the following one:

lambda x:(c:=' mePbphHond'.find(x[0]),2*c+reduce(lambda i,j:i-j,map(int,str(ord(x[-3])))))
from functools import reduce

which obviously is much more bytes (119).

game0ver

Posted 2019-12-05T20:31:44.057

Reputation: 621

60 bytes – Arnauld – 2019-12-06T02:52:30.197

@Arnauld Thanks! That's way more elegant :) – game0ver – 2019-12-06T02:55:52.080

2

Python 2, 66 bytes

def f(s):n='mePbpHhond'.find(s[0]);return-~n,2*(n+ord(s[-3])%50%3)

Try it online!

Over thinking it a bit. Accepts only lower case, except for 'Prop' and 'Hept' (which must be capitalized). Returns (carbon, hydrogen).

Python 2, 84 82 bytes

def f(s):n=int('8702563914'[hash(s[:-3])%876%10]);return n+1,2*(n+ord(s[-3])%50%3)

Try it online!

Only accepts lower case.

Chas Brown

Posted 2019-12-05T20:31:44.057

Reputation: 8 959

-1 with %7/3 – frank – 2019-12-05T22:23:05.300

%11%5 works to give already-doubled values. – xnor – 2019-12-06T08:21:18.440

2

Ruby, 74 67 bytes

->s{z="  etthroutenexepctonec".index s[1,2];[z/2,z-2*(s[-3]<=>?e)]}

Try it online!

Thanks @Value Ink for golfing it for me. :-) -7 bytes

G B

Posted 2019-12-05T20:31:44.057

Reputation: 11 099

1

Python 3, 83 bytes

x=input();n=list('_mepbPhHond').index(x[0]);print(n,2*n+{'a':2,'e':0,'y':-2}[x[-3]])

Try it online!

-51 bytes thanks to 79037662

sugarfi

Posted 2019-12-05T20:31:44.057

Reputation: 1 239

Can save some bytes by only considering the first letter, instead of the first three: https://tio.run/##BcFBC4MgGAbgvxJdPl02wt2M7h13F4lGQsL2@lEOjOi32/PwkdYIXUoeAvifhOwxfMOeBE0/z5/3OkYsJJ8Bi88i287JnreAVEHpB5qTZjJakSfTKTrItPqy2bYv50qpeYs8w9c3

– 79037662 – 2019-12-05T22:00:28.380

1

Charcoal, 39 bytes

≔⊘⁺³⌕”↶↓>AkQN!{a⁶⟧”…θ⁻Lθ⁵ηI⟦η⊗⁻⁺η№θa№θy

Try it online! Link is to verbose version of code. Takes input in lower case and outputs carbon and hydrogen atoms on separate lines. Explanation:

…θ⁻Lθ⁵

Remove the last 5 characters of the input.

⌕”↶↓>AkQN!{a⁶⟧”...

Search for the resulting prefix within the compressed string e prb peh heo n d. Note that there's a space before the e, so that the the prefixes are found at positions -1 for me(th?ne), 1 for e(th?ne), 3 for pr(op?ne) etc.

≔⊘⁺³...η

Add 3 to the position, then halve and store the resulting number of carbon atoms.

I⟦η

Output the carbon atoms...

⊗⁻⁺η№θa№θy

... and adjust for whether there is an a or y in the input, before doubling to give the number of hydrogen atoms.

Neil

Posted 2019-12-05T20:31:44.057

Reputation: 95 035

1

C# (Visual C# Interactive Compiler), 65 bytes

x=>(l=" mepbPhHond".IndexOf(x[0]),~-l+x[x.Count-3]%7/3<<1);int l;

Try it online!

Embodiment of Ignorance

Posted 2019-12-05T20:31:44.057

Reputation: 7 014

1

GolfScript, 32 30 bytes

Thanks to this user for pointing out that I forgot to account for -ane, -ene, -yne

.0="mepbPhHond"?).@-3=7%3/(+2*

Try it online!

This uses the same concept as in this answer

Explanation:

.0=                                 Make a copy of input and select first letter
   "mepbPhHond"?)                   Find index of letter and increase by one for number of carbons
                 .@                 Duplicate for number of hydrogens and bring input to top of stack
                   -3=              Get the a, e, or y in ane, ene, yne
                      7%3/          Ascii value mod 7 integer divide 3
                          (+        Decrement and add to number of carbons
                            2*      Double

Uses mepbPhHond capitalization.

Example Execution: Input: propene

.0=                                 Stack: 'propene' 'p'
   "mepbPhHond"?)                   Stack: 'propene' 3
                 .@                 Stack: 3 3 'propene'
                   -3=              Stack: 3 3 'e'
                      7%3/          Stack: 3 3 1
                          (+        Stack: 3 3
                            2*      Stack: 3 6

Pseudo Nym

Posted 2019-12-05T20:31:44.057

Reputation: 181