def get_atomic_weight_for_compound(compound: str) -> float: """ This function needs to be implemented. Given the chemical formula of a compount return it's corresponding atomic weight. Chemical formulas are expressed in a very simple grammar. Element symbols always start with a capital letter, and may have one or two following characters which are always lowercase, for example: * "H" -> Hydrogen * "He" -> Helium * "Uuh" -> Ununhexium Element symbols are followed by an optional number which indicates how many atoms of a symbol are present in a single molecule of the substance. * "O2" -> Two atoms of oxygen * "H2SO4" -> Two hydrogens, One Sulphur, 4 Oxygens * "He" -> A single atom of Helium The number is omitted in the case that there is single atom of an element in a molecule, hence this is not valid: * "H2S1O4" -> Invalid because "1" is redundant here. """ if not compound: return 0.0 c = periodic_table.Compound(compound) return c.weight()
def test_compound_weight_multi_2letter(): c = periodic_table.Compound("Al4O2") assert c.weight() == 15.999 * 2 + 26.98153857 * 4
def test_compound_weight_multi(): c = periodic_table.Compound("N2O2") assert c.weight() == 15.999 * 2 + 14.007 * 2
def test_compound_weight_simple(): c = periodic_table.Compound("O2") assert c.weight() == 15.999 * 2
def test_compound_create_3letter1letter(): c = periodic_table.Compound("CaCl2O")
def test_compound_create_1letter1letternumbered(): c = periodic_table.Compound("N2O2")
def test_compound_create_1letter1letter(): c = periodic_table.Compound("NO")
def test_compound_create_2letter1letter(): """Use an initial 2 char symbol that can be confused with a single character one.""" c = periodic_table.Compound("CaC")
def test_compound_create_badchars_end(): with pytest.raises(periodic_table.InvalidFormula): c = periodic_table.Compound("He,.$£")
def test_compound_create_badchars_beginning(): with pytest.raises(periodic_table.InvalidFormula): c = periodic_table.Compound(",.$£He")
def test_compound_create_completely_invalid(): with pytest.raises(periodic_table.InvalidFormula): c = periodic_table.Compound(",.$£")
def test_compound_create_simple_invalid_count_1(): with pytest.raises(periodic_table.InvalidFormula): c = periodic_table.Compound("O1")
def test_compound_create_simple_numbered(): c = periodic_table.Compound("O2")
def test_compound_create_simple(): c = periodic_table.Compound("O")
def test_compound_create_empty(): c = periodic_table.Compound("") assert c.weight() == 0.0