コード例 #1
0
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()
コード例 #2
0
def test_compound_weight_multi_2letter():
    c = periodic_table.Compound("Al4O2")
    assert c.weight() == 15.999 * 2 + 26.98153857 * 4
コード例 #3
0
def test_compound_weight_multi():
    c = periodic_table.Compound("N2O2")
    assert c.weight() == 15.999 * 2 + 14.007 * 2
コード例 #4
0
def test_compound_weight_simple():
    c = periodic_table.Compound("O2")
    assert c.weight() == 15.999 * 2
コード例 #5
0
def test_compound_create_3letter1letter():
    c = periodic_table.Compound("CaCl2O")
コード例 #6
0
def test_compound_create_1letter1letternumbered():
    c = periodic_table.Compound("N2O2")
コード例 #7
0
def test_compound_create_1letter1letter():
    c = periodic_table.Compound("NO")
コード例 #8
0
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")
コード例 #9
0
def test_compound_create_badchars_end():
    with pytest.raises(periodic_table.InvalidFormula):
        c = periodic_table.Compound("He,.$£")
コード例 #10
0
def test_compound_create_badchars_beginning():
    with pytest.raises(periodic_table.InvalidFormula):
        c = periodic_table.Compound(",.$£He")
コード例 #11
0
def test_compound_create_completely_invalid():
    with pytest.raises(periodic_table.InvalidFormula):
        c = periodic_table.Compound(",.$£")
コード例 #12
0
def test_compound_create_simple_invalid_count_1():
    with pytest.raises(periodic_table.InvalidFormula):
        c = periodic_table.Compound("O1")
コード例 #13
0
def test_compound_create_simple_numbered():
    c = periodic_table.Compound("O2")
コード例 #14
0
def test_compound_create_simple():
    c = periodic_table.Compound("O")
コード例 #15
0
def test_compound_create_empty():
    c = periodic_table.Compound("")
    assert c.weight() == 0.0