aboutsummaryrefslogtreecommitdiff
path: root/mendeleev.py
blob: 6c01e0d59dfb4f1cd1738a2fe8a2eeca4bd79a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import sys


# fmt: off
ELEMENTS = [
  "Ac", "Ag", "Al", "Am", "Ar", "As", "At", "Au", "B", "Ba", "Be", "Bh",
  "Bi", "Bk", "Br", "C", "Ca", "Cd", "Ce", "Cf", "Cl", "Cm", "Cn", "Co",
  "Cr", "Cs", "Cu", "Db", "Ds", "Dy", "Er", "Es", "Eu", "F", "Fe", "Fl",
  "Fm", "Fr", "Ga", "Gd", "Ge", "H", "He", "Hf", "Hg", "Ho", "Hs", "I",
  "In", "Ir", "K", "Kr", "La", "Li", "Lr", "Lu", "Lv", "Mc", "Md", "Mg",
  "Mn", "Mo", "Mt", "N", "Na", "Nb", "Nd", "Ne", "Nh", "Ni", "No", "Np",
  "O", "Og", "Os", "P", "Pa", "Pb", "Pd", "Pm", "Po", "Pr", "Pt", "Pu",
  "Ra", "Rb", "Re", "Rf", "Rg", "Rh", "Rn", "Ru", "S", "Sb", "Sc", "Se",
  "Sg", "Si", "Sm", "Sn", "Sr", "Ta", "Tb", "Tc", "Te", "Th", "Ti", "Tl",
  "Tm", "Ts", "U", "V", "W", "Xe", "Y", "Yb", "Zn", "Zr"
]
# fmt: on

elements = [el.lower().encode() for el in ELEMENTS]


def search(rng, shift, char):
    upper = rng[1]
    lower = rng[0]
    while lower < upper:
        mid = int((lower + upper) / 2)
        if elements[mid][shift] < char:
            lower = mid + 1
        else:
            upper = mid

    if lower == rng[1] or elements[lower][shift] != char:
        rng[1] = 0
        return

    upper = rng[1]
    rng[0] = lower
    while lower < upper:
        mid = int((lower + upper) / 2)
        if char < elements[mid][shift]:
            upper = mid
        else:
            lower = mid + 1

    rng[1] = upper


def split(tail):
    result = []

    rng = [0, len(ELEMENTS)]
    shift = 0

    while shift < len(tail):
        search(rng, shift, tail[shift])
        if rng[0] >= rng[1]:
            break

        shift += 1
        if len(elements[rng[0]]) == shift:
            result.append((ELEMENTS[rng[0]], tail[shift:]))
            rng[0] += 1

    return result or [("?", tail[1:])]


def explode(tail):
    return [(x[0], explode(x[1]) if x[1] else None) for x in split(tail)]


def analyze(word):
    return explode(word.lower().encode())


def print_plain(tree, formula):
    for x in tree:
        formula.append(x[0])
        if x[1]:
            print_plain(x[1], formula)
        else:
            print(" " + " ".join(formula))
        formula.pop()


for w in sys.argv[1:]:
    print(w + ":")
    if w:
        print_plain(analyze(w), [])