2
\$\begingroup\$

(Note: I know that the VSEPR method fails sometimes, and that there are exceptional molecules. The challenge is about the molecules which conform.)

Most people who have taken an introductory chemistry course know about molecules, and (probably) the VSEPR theory of chemical bonding. Basically, the theory predicts the shape of a molecule given three main properties:

  • A: The central atom. You may assume this is the first element appearing in the chemical formula; for example, in CH4, you may assume the central atom is C.
  • X: The number of atoms bonded to the central atom A.
  • E: The number of "lone electron pairs" on the central atom. For the purposes of this challenge, the number of outer electrons A has is given by the last digit of the element's column number on the periodic table.

    • If A is bonded to an element X in column 17 of the periodic table, there will be a mutual sharing of electrons, so that A effectively has one additional electron for each atom of X attached to it. (This does not apply for elements such as O or S in column 16.)

Each bond requires two of these electrons. Leftover electrons not used in bonding with other atoms are found in pairs of two, called lone pairs. So E is the number of electrons not used in bonding by the central atom A divided by 2. (It may be assumed that input will always lead to A having an even number of electrons.)

For example, a molecule which has 1 lone pair and 3 atoms bonded to the main atom is AX3E1, which is the trigonal pyramidal configuration. (There is always only 1 central atom, so A never has a subscript.)

The Challenge

Your job is, given a string representing a covalent chemical compound, to output the shape of that molecule. "But wait!", exclaim the exasperated programmers, "you can't expect us to input all of the molecular data for every element!" Of course we can, but as that wouldn't be fun, we'll only consider the compounds formed by the following 7 elements:

Carbon    C
Sulfur    S
Oxygen    O
Nitrogen  N
Chlorine  Cl
Bromine   Br
Fluorine  F

The input is any molecule composed of the above 7, such as CO2 or SF4, that is in the form AXN, where A and X are each one of the above seven element symbols, X is not C or N, and N is a number between 2 and 6 inclusive.

The output should be the name of the geometric shape of the molecule, for example "linear".

The following (don't get scared, I swear they're less than they seem!) is a list of all possible configurations along with their AXE number and a drawing of how they look (adapted from Wikipedia's nifty table):

All AXE configurations

Test Cases

CO2: CO2 -> linear
OF2: OF2 -> bent
CCl2: CCl2 -> bent
BrF3: BrF3 -> T-shaped
SF4: SF4 -> seesaw

Notes:
- We won't consider compounds in which the first element has a subscript other than 1, such as C2H4.
- Standard loopholes apply.
- This is a code golf, so the shortest answer wins (obviously)

\$\endgroup\$
11
  • 1
    \$\begingroup\$ Welcome to PPCG. Apart from the fact that Ethylene is C2H4 there are multiple ambiguities here. 1.I think you should abandon substances like ethylene and guarantee that the first named element will be A and there will be only one. 2.Your explanation is inadequate for a non chemist, you should at least explain that the number of electrons in the outer shell can be found by looking at the periodic table. 3. Is X guaranteed to be mono or divalent? is it only halogens, hydrogen and oxygen? 4.there is nothing in the basic spec about ionic compounds such as NaCl, which ones have to be detected? \$\endgroup\$ Jul 20, 2015 at 11:28
  • \$\begingroup\$ Voting to close as unclear. I suggest you simplify the bonuses and give a fixed list of elements that must be supported (and that edge cases where VSEPR fails, including ionic compounds, be ignored, otherwise it's a chemistry question not a programming one.) You should also list the molecule shapes exactly as they are to be output. Writing a good question is hard. I suggest you consider using meta.codegolf.stackexchange.com/q/2140/15599 \$\endgroup\$ Jul 20, 2015 at 11:31
  • \$\begingroup\$ BTW, I don't think this deserves a downvote (if whoever gave it is reading.) There's potential for a good question. It's just that codegolf requires a really tight spec to be fair and sporting. A few more points: 5. What is the maximum value of X+E to be supported? The examples go up to 6, I'm used to going up to 7, but Wikipedia goes up to 9! 6. you should explain subtleties around AB3X2 : T shaped preferred to keep the lone pairs far apart. 7. Angles can only be found by looking them up, not calculating, so have no place in a programming question like this. BrF3 is actually 86.2deg, not 90. \$\endgroup\$ Jul 20, 2015 at 18:41
  • \$\begingroup\$ @steveverrill Hello and thanks for such a thorough breakdown of where my question doesn't add up. I actually did place the question in the sandbox a day or two before, but got no feedback, so I decided to just try my luck. About your suggestions (thanks again for those!): 1. Changed. 2. Tried to make clearer and more elementary. 3. I only allowed 7 elements, so that should make the question less broad. 4. I defined what counts as an ionic compound here, didn't fully understand what you meant though. Simplified the bonuses and other small things like removing the angles. \$\endgroup\$
    – user42079
    Jul 20, 2015 at 19:39
  • \$\begingroup\$ 5. I think X + E is bounded by the restrictions on the possible compounds, otherwise 6 should be good (and I doubt it goes higher than that.) 6. I'd prefer to remove it altogether so as not to make the question too technical. 7. Took them out. \$\endgroup\$
    – user42079
    Jul 20, 2015 at 19:44

1 Answer 1

1
\$\begingroup\$

Ruby, 250

s=gets
e=7-"FBOSN C".index(s[0])/2+(s[1]==?l?3:0)
x=s.reverse!.to_i
e+="Frl".index(s[2])?x:0
t="trigonal "
puts [["linear",b="bent",b],[t+"planar",t+p="pyramidal","T-shaped"],["tetr"+h="ahedral","seesaw"],[t+"bi"+p,"square "+p],["oct"+h]][x-2][e/2-x]

Note that with the rules as they are, AX2E3 and AX4E2 are not possible with the given elements, so they are not supported. All test cases passed.

with comments

s=gets                                     #get input with newline attached
e=7-"FBOSN C".index(s[0])/2+(s[1]==?l?3:0) #calculate the number of electrons (not lone pairs) from A. Note the addition required to differentiate C and Cl.
x=s.reverse!.to_i                          #reverse the input string so the number is at the beginning (after the newline) and evaluate the string as a number to get X.
e+="Frl".index(s[2])?x:0                   #if the character after the number is in "Frl" we have a halogen so increment number of electrons accordingly.
t="trigonal "                              #as "trigonal " goes at the beginning of the string, we cannot define it on the fly, it is defined as a constant here.
                                           #array of outputs below. note the subscripts at the end: x-2 to put it in the range 0..4 and e/2-x to convert e into the actual number of lone pairs.
puts [["linear",b="bent",b],[t+"planar",t+p="pyramidal","T-shaped"],["tetr"+h="ahedral","seesaw"],[t+"bi"+p,"square "+p],["oct"+h]][x-2][e/2-x]
\$\endgroup\$
1
  • \$\begingroup\$ Nice answer. I like the way you were able to cut down on the bytes for the names by concatenating bits and pieces of the words. Cheers! \$\endgroup\$
    – user42079
    Jul 23, 2015 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.