I just had a lesson in school about alkanes and figured it would probably make for a great code golf challenge! Don't worry, it isn't as complicated as it may look!
A Quick Rehash
(Please Note: To keep this brief, not all information is 100% accurate.)
Alkanes are strings of carbon and hydrogen. Every carbon atom has 4 bonds, and every hydrogen atom 1 bond. All the carbon atoms of the alkane form a string where each C-atom is connected to 2 other C-atoms (left and right in the Lewis structure) and 2 H-atoms (up and down), except for the ends of the string, where the C-atom is connected to only 1 other C but 3 Hs. Here's a basic example for pentane (an alkane with 5 C-atoms and 12 H-atoms):
H H H H H | | | | | H-C-C-C-C-C-H | | | | | H H H H H
Alkanes also can have branches. But don't worry, all alkanes in this challenge can be expressed with only 1 level of branching. Example:
H | H-C-H H H H | H | | | | | H-C-C-C-C-C-H | | | | | H H H H H
For completing this challenge you also must understand the IUPAC naming convention for branched alkanes. First of there's the root alkane. In our previous example, this would be the "C-C-C-C-C" part. Depending on how long this chain is, it has a different name. 1 C is called methane, 2 C ethane, 3 C propane, then butane, pentane, hexane, heptane, octane, nonane and decane (10 C). Then, for each branch, there's a certain prefix to that: First of, there's the index (offset) of the C-atom the branch is appended to (count from the left). In the example, this would be 4 (aka it's the 4th C-atom from the left). Then there's a hyphen (this symbol: "-") and after that another name indicating the size of the branch. The naming of the size of the branch is almost same like the naming of the size of the root, just that instead of "ane" you append "yl". With that the full name of the example would be
If you have multiple branches, then they are prepended as well, separated by another hyphen. Example:
One last thing: if you have multiple branches of the same size, they get grouped; their offsets are separated by comma and they share the same size-name, which gets prepended by an extra syllable depending on how many branches are grouped: "di" for 2 branches, "tri" for 3, "tetra" for 4 (you don't need more for this challenge). Example:
FYI, this could look somewhat like this: (Omitted H-atoms)
| -C- | | -C- -C- | | | | | | | | | | -C-C-C-C-C-C-C-C-C-C- | | | | | | | | | | -C- -C- | |
Prefixes indicating numbers: | Num | Prefix | |------|--------| | 1 | meth | | 2 | eth | | 3 | prop | | 4 | but | | 5 | pent | | 6 | hex | | 7 | hept | | 8 | oct | | 9 | non | | 10 | dec | Suffix root: ane Suffix branch: yl Prefixes grouping: di, tri, tetra
Write a program that reads in such an IUPAC name from STDIN, program arguments or equivalent and draws it as an ASCII-art lewis structure to STDOUT (or equivalent).
- For simplicity, you DO NOT have to draw the H-atoms (Else you will run into spacing issues)
- You are NOT allowed to print any empty leading or trailing horizontal lines
- The chains you have to parse won't be any longer than 10, and the maximum of branches in a "group" is limited to 4.
- The maximum "offset" of a branch is 9 (meaning that you don't have to parse more than 1 digit)
- Your branches have to alternate between going up and down after every new branch. In case this space is already taken by another branch at the same offset, you have to draw it on the other side of the root. (up->down, down->up)
- On corrupt, not-correctly-formatted or otherwise not-drawable input, your program may behave unspecified.
This is code golf, shortest code in bytes wins!
Happy golfing! :)