8
\$\begingroup\$

Challenge

Find the oxidation states of each of the atoms in a given molecule. These can be output as a list or otherwise.

Rules

The total oxidation state of a molecule will always be zero. The total oxidation state is the sum of the oxidation states of each of the individual atoms in the molecule.

The following atoms have constant oxidation states:

  • Hydrogen, H, always has state +1
  • Oxygen, O, always has state -2
  • All Group 1 elements have a state +1
  • All Group 2 elements have a state +2
  • Fluorine, F, always has state -1
  • Chlorine, Cl, always has state -1

In monatomic molecules (such as H2 or S8), all of the atoms always have a state 0. I.e. (H2 is 0, 0 and S8 is 0, 0, 0, 0, 0, 0, 0, 0)

The Group 1 elements are: Li, Na, K, Rb, Cs, Fr. The Group 2 elements are: Be, Mg, Ca, Sr, Ba, Ra.

You will be able to work out the states of every atom in the molecule given. There will be no ambiguous inputs (i.e. you won't be given H2O2 or P4Br6).

You should output the oxidation states of the individual atoms, not the total state.

If there is an element which is not listed in the list above in the molecule, you need to work out its oxidation state yourself since the sum of the oxidation states of all the atoms adds up to zero.

The molecules do not have to exist in real life.

You will never get a single atom such as Ti or F.

Built-in functions which access data about oxidation states are disallowed.

Examples

Input > Output
H2O > +1, +1, -2
CO2 > +4, -2, -2
CH4 > -4, +1, +1, +1, +1
H2SO4 > +1, +1, +6, -2, -2, -2, -2
NaHCO3 > +1, +1, +4, -2, -2, -2
XeF8 > +8, -1, -1, -1, -1, -1, -1, -1, -1
TiCl2 > +2, -1, -1
P4 > 0, 0, 0, 0

Winning

The shortest code in bytes wins.

\$\endgroup\$
13
  • \$\begingroup\$ For H2O, can we output +1, +1, -2? Also, are we allowed to output without the + for positive integers? \$\endgroup\$
    – Okx
    May 22, 2017 at 10:25
  • \$\begingroup\$ @Okx Yes, you can output +1, +1, -2 and yes, you don't need the positive sign \$\endgroup\$
    – Beta Decay
    May 22, 2017 at 10:26
  • \$\begingroup\$ Can we accept input with an multiplier after every Atom: H2O1 or C1O2 \$\endgroup\$ May 22, 2017 at 10:59
  • \$\begingroup\$ @RomanGräf Nope, you must take input as it is shown \$\endgroup\$
    – Beta Decay
    May 22, 2017 at 11:11
  • 1
    \$\begingroup\$ (okay this might be cheating) Are we allowed to assume there will always be less than 10 of the same atom in a molecule, as my solution breaks with double digits it? :P \$\endgroup\$
    – Okx
    May 22, 2017 at 12:28

3 Answers 3

2
\$\begingroup\$

Ruby, 288 255 bytes

->m{h={[?O]=>-2,%w[F Cl]=>-1,%w[H Li Na K Rb Cs Fr]=>1,%w[Be Mg Ca Sr Ba Ra]=>2};a=m.scan /([A-Z][a-z]*\d*)/;r=a.flat_map{|(b)|b=~/(\D+)(\d*)/;[a.size==1?0:h[h.keys.find{|v|v.include?$1}]]*($2=='' ? 1:$2.to_i)};r.map{|j|j||-((r-[p]).inject:+)/r.count(p)}}

Try it online!

  • Saved 1 byte thanks to Jenkar
  • Saved 32 more bytes by inlining definitions and removing parentheses

Ungolfed

oxidation_state = -> molecule {
    h = {
        %w[H] => 1,
        %w[O] => -2,
        %w[F] => -1,
        %w[Cl] => -1,
        %w[Li Na K Rb Cs Fr] => 1,
        %w[Be Mg Ca Sr Ba Ra] => 2,
    }
    find_oxidation = -> a {
        k = h.keys.find { |as| as.include?(a) }
        h[k]
    }
    atoms = molecule.scan(/([A-Z][a-z]*\d*)/)
    r = atoms.flat_map { |(a)|
        a.match(/([^\d]+)(\d*)/)
        [atoms.length == 1 ? 0 : find_oxidation[$1]] * ($2=='' ? 1 : $2.to_i)
    }
    r.map { |j|
        sum = r.compact.inject(:+)
        !j ? -sum / r.count(nil) : j
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Jenkar suggests saving one byte with [?O] instead of %w[O]. \$\endgroup\$ May 22, 2017 at 13:43
1
\$\begingroup\$

Ruby, 245 bytes

->m{h={[?O]=>-2,%w[F Cl]=>-1,%w[H Li Na K Rb Cs Fr]=>1,%w[Be Mg Ca Sr Ba Ra]=>2};a=m.scan /([A-Z][a-z]*\d*)/;r=a.flat_map{|(b)|b=~/(\D+)(\d*)/;[a[1]?h[h.keys.find{|v|v.include?$1}]:0]*[1,$2.to_i].max};r.map{|j|j||-((r-[p]).inject:+)/r.count(p)}}

Try it online!

As I prepare this post, with a ton of optimizations, sudee's answer gets an update xD

Which reduces my changes to two :

  • a.size==1 becomes a[1] with an inverted consequence. This is because we know that the string is well formed and so a.size != 0
  • ($2=='' ? 1:$2.to_i) becomes [1,$2.to_i].max

Both of these save 5 bytes.

\$\endgroup\$
1
  • \$\begingroup\$ The max solution is pretty nice, didn't occur to me. I was hesitant about a[1] but then I dismissed it for some reason. :D We could also replace .inject:+ with .sum for newer versions of Ruby to save a few bytes. (Doesn't work with TIO sadly.) \$\endgroup\$
    – sudee
    May 22, 2017 at 18:25
1
\$\begingroup\$

Python 3, 272 bytes

s=input()+'A';n='';l=[1]
for i in s:
 if'Z'<i:l[-1]+=i
 elif'9'<i:l+=[l.pop()]*int(n or 1)+[i];n=''
 else:n+=i
i=lambda s:s.split().count
l=[i('Li H Na K Rb Cs Fr')(s)+i('Be Mg Ca Sr Ba Ra')(s)*2-i('F Cl O O')(s)for s in l[1:-1]]
print([k or-sum(l)//l.count(0)for k in l])

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Doesn't work with more than one digit in a number. \$\endgroup\$
    – Okx
    May 22, 2017 at 15:40
  • \$\begingroup\$ @Okx fixed it . \$\endgroup\$
    – ovs
    May 22, 2017 at 17:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.