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}
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:
Stats:
Created | Feb 10, 2014 |
Published | Feb 11, 2014 |
Warriors Trained | 41385 |
Total Skips | 12799 |
Total Code Submissions | 83240 |
Total Times Completed | 5991 |
JavaScript Completions | 2158 |
CoffeeScript Completions | 58 |
Haskell Completions | 183 |
Python Completions | 2202 |
Rust Completions | 174 |
PHP Completions | 135 |
C# Completions | 350 |
Java Completions | 435 |
TypeScript Completions | 307 |
Kotlin Completions | 128 |
Groovy Completions | 13 |
Total Stars | 1950 |
% of votes with a positive feedback rating | 93% of 1145 |
Total "Very Satisfied" Votes | 1020 |
Total "Somewhat Satisfied" Votes | 100 |
Total "Not Satisfied" Votes | 25 |