5 kyu

Molecule to atoms

2,158 of 5,991romanzes

Description:

For a given chemical formula represented by a string, count the number of atoms of each element contained in the molecule and return an object (associative array in PHP, Dictionary<string, int> in C#, Map<String,Integer> in Java).

For example:

var water = 'H2O';
parseMolecule(water); // return {H: 2, O: 1}

var magnesiumHydroxide = 'Mg(OH)2';
parseMolecule(magnesiumHydroxide); // return {Mg: 1, O: 2, H: 2}

var fremySalt = 'K4[ON(SO3)2]2';
parseMolecule(fremySalt); // return {K: 4, O: 14, N: 2, S: 4}
parse_molecule('H2O'); // => ['H' => 2, 'O' => 1]
parse_molecule('Mg(OH)2'); // => ['Mg' => 1, 'O' => 2, 'H' => 2]
parse_molecule('K4[ON(SO3)2]2'); // => ['K' => 4, 'O' => 14, 'N' => 2, 'S' => 4]
Kata.ParseMolecule("H2O"); // => new Dictionary<string, int> {{"H", 2}, {"O", 1}}
Kata.ParseMolecule("Mg(OH)2"); // => new Dictionary<string, int> {{"Mg", 1}, {"O", 2}, {"H", 2}}
Kata.ParseMolecule("K4[ON(SO3)2]2"); // => new Dictionary<string, int> {{"K", 4}, {"O", 14}, {"N", 2}, {"S", 4}}
water = 'H2O'
parse_molecule(water)                 # return {H: 2, O: 1}

magnesium_hydroxide = 'Mg(OH)2'
parse_molecule(magnesium_hydroxide)   # return {Mg: 1, O: 2, H: 2}

var fremy_salt = 'K4[ON(SO3)2]2'
parse_molecule(fremySalt)             # return {K: 4, O: 14, N: 2, S: 4}
>>> parseMolecule "H2O" -- water
Right [("H",2),("O",1)]

>>> parseMolecule "Mg(OH)2" -- magnesium hydroxide
Right [("Mg",1),("O",2),("H",2)]

>>> parseMolecule "K4[ON(SO3)2]2" -- Fremy's salt
Right [("K",4),("O",14),("N",2),("S",4)]

>>> parseMolecule "pie"
Left "Not a valid molecule"
parse_molecule("H2O");           // water
// Ok([("H", 2), ("O", 1)])

parse_molecule("Mg(OH)2");       // magnesium hydroxide
// Ok([("Mg", 1), ("O", 2), ("H", 2)]

parse_molecule("K4[ON(SO3)2]2"); // Fremy's salt
// Ok([("K", 4), ("O", 14),("N", 2),("S", 4)])

parse_molecule("pie")
// Err(ParseError)
String water = "H2O";
parseMolecule.getAtoms(water); // return [H: 2, O: 1]

String magnesiumHydroxide = "Mg(OH)2";
parseMolecule.getAtoms(magnesiumHydroxide); // return ["Mg": 1, "O": 2, "H": 2]

String fremySalt = "K4[ON(SO3)2]2";
parseMolecule.getAtoms(fremySalt); // return ["K": 4, "O": 14, "N": 2, "S": 4]

parseMolecule.getAtoms("pie"); // throw an IllegalArgumentException

As you can see, some formulas have brackets in them. The index outside the brackets tells you that you have to multiply count of each atom inside the bracket on this index. For example, in Fe(NO3)2 you have one iron atom, two nitrogen atoms and six oxygen atoms.

Note that brackets may be round, square or curly and can also be nested. Index after the braces is optional.

Parsing
Algorithms
Strings

Similar Kata:

More By Author:

Check out these other kata created by romanzes

Stats:

CreatedFeb 10, 2014
PublishedFeb 11, 2014
Warriors Trained41385
Total Skips12799
Total Code Submissions83240
Total Times Completed5991
JavaScript Completions2158
CoffeeScript Completions58
Haskell Completions183
Python Completions2202
Rust Completions174
PHP Completions135
C# Completions350
Java Completions435
TypeScript Completions307
Kotlin Completions128
Groovy Completions13
Total Stars1950
% of votes with a positive feedback rating93% of 1145
Total "Very Satisfied" Votes1020
Total "Somewhat Satisfied" Votes100
Total "Not Satisfied" Votes25
Ad
Contributors
  • romanzes Avatar
  • jhoffner Avatar
  • tko Avatar
  • MMMAAANNN Avatar
  • myjinxin2015 Avatar
  • donaldsebleung Avatar
  • aweleshetu Avatar
  • imjasonmiller Avatar
  • kazk Avatar
  • vguzev Avatar
  • Blind4Basics Avatar
  • Voile Avatar
  • ice1000 Avatar
  • FArekkusu Avatar
  • hobovsky Avatar
  • user9240328 Avatar
  • saudiGuy Avatar
Ad