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

Return to Answer

added a note
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

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.

Expects mEpBPhHond for the capitalization.

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.

fixed the 05AB1E version
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

05AB1E (legacy) 05AB1E, (削除) 27 26 (削除ここまで) 24 bytes

05AB1E (legacy), (削除) 27 26 (削除ここまで) 24 bytes

05AB1E, (削除) 27 26 (削除ここまで) 24 bytes

saved 2 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

05AB1E (legacy), (削除) 27 26 (削除ここまで) 2624 bytes

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

Expects MepbPhHOndmEpBPhHond for the capitalization.

Çć•ql±S•s÷11%=s3ćC•>·¿*•s÷θD>,2I3(è7%3÷<+·èH÷+·,

Try it online! Try it online!

TheWe define the following magic number was found after some brute-forcingconstant:

$$M=865332583$$$$M=1902159854$$

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

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

 input | n | floor(M/n) | mod 1110
-------+-----+------------+--------
 Methmeth- | 7748 | 11238085 39628330 | 10
 ethEth- | 101 | 14 | 8567649 135868561 | 21
 prop- | 112 |51 | 7726183 37297252 | 32
 butBut- | 9811 | 8829924 172923623 | 43
 Pent- | 8025 | 10816657 76086394 | 54
 hex- | 104 |43 | 8320505 44236275 | 65
 Hept- | 7217 | 12018508 111891756 | 76
 Octoct- | 7950 | 10953577 38043197 | 87
 non- | 110 |49 | 7866659 38819588 | 98
 dec- | 100 |39 | 8653325 48773329 | 10 9
 # example input: 'Heptyne'
Ç ć # getextract ASCIIthe codesfirst character -> [72, 101, 112, 116, 121, 110, 101]'H'
 ć  C # extractturn theit firstinto valuean integer -> 7217
 •ql±S•  •>·¿*• # push the magic numberconstant 8653325831902159854
 s # swap + #integer swap
division -> 1902159854 / 17 = 111891756
 ÷ θ  # integerkeep divisionthe ->last 865332583digit /(therefore 72a =modulo 12018508
10) -> 6
 11% D> # modulo 11duplicate ->and 7
increment the copy
 = , # print the number of carbon atoms
 s  2 # swap again to bring back the list of ASCIIpush codes26
 3(èI # extractpush the 3rd to last value -> 121input ('y')string
 3(è 7% # extract the #3rd moduloto 7last character -> 2'y'
 H # integerparse divisionit byas 3base-36 -> 034
 <÷ # decrementinteger division -> -1
26 / 34 = 0
 + # add to the number of carbon atoms - 1 -> 6
 · # double -> 12
 , # print the number of hydrogen atoms

NB: the %7/3 trick was taken from @frank's answer .

05AB1E (legacy), (削除) 27 (削除ここまで) 26 bytes

Saved 1 byte thanks to @KevinCruijssen

Expects MepbPhHOnd for the capitalization.

Çć•ql±S•s÷11%=s3(è7%3÷<+·,

Try it online!

The following magic number was found after some brute-forcing:

$$M=865332583$$

$$c=\left\lfloor\frac{M}{n}\right\rfloor\bmod 11$$

where \$n\$ is the ASCII code of the first character of the input.

 input | n | floor(M/n) | mod 11
-------+-----+------------+--------
 Meth- | 77 | 11238085 | 1
 eth- | 101 | 8567649 | 2
 prop- | 112 | 7726183 | 3
 but- | 98 | 8829924  | 4
 Pent- | 80 | 10816657 | 5
 hex- | 104 | 8320505 | 6
 Hept- | 72 | 12018508 | 7
 Oct- | 79 | 10953577 | 8
 non- | 110 | 7866659 | 9
 dec- | 100 | 8653325 | 10
 # example input: 'Heptyne'
Ç  # get ASCII codes -> [72, 101, 112, 116, 121, 110, 101]
 ć  # extract the first value -> 72
 •ql±S•  # push magic number 865332583
 s # swap
 ÷ # integer division -> 865332583 / 72 = 12018508
 11% # modulo 11 -> 7
 = # print the number of carbon atoms
 s  # swap again to bring back the list of ASCII codes
 3(è # extract the 3rd to last value -> 121 ('y')
 7% # modulo 7 -> 2
  # integer division by 3 -> 0
 < # decrement -> -1
 + # add to the number of carbon atoms -> 6
 · # double -> 12
 , # print the number of hydrogen atoms

NB: the %7/3 trick was taken from @frank's answer .

05AB1E (legacy), (削除) 27 26 (削除ここまで) 24 bytes

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

Expects mEpBPhHond for the capitalization.

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

Try it online!

We define the following magic constant:

$$M=1902159854$$

$$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
 # example input: 'Heptyne'
ć # extract the first character -> 'H'
 C # turn it into an integer -> 17
 •>·¿*• # push the magic constant 1902159854
  # 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
 2 # 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
saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
now using the smallest possible magic number (no bytes saved, though)
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 2 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

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