-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchemical_reaction.py
51 lines (41 loc) · 1.47 KB
/
chemical_reaction.py
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
import atom
import molecule
import known_molecules as km
class ChemicalReaction():
def __init__(self, left, right):
self.left_side = left
self.right_side = right
def list_of_atoms(self, side):
atoms = {}
molecules = {}
if side == 'left':
molecules = self.left_side
if side == 'right':
molecules = self.right_side
atoms = {}
for m in molecules:
elements = m.list_of_atoms()
for element in elements.keys():
if element in atoms:
atoms[element] += elements[element]
else:
atoms[element] = elements[element]
return atoms
def is_balanced(self):
left = self.list_of_atoms('left')
right = self.list_of_atoms('right')
return left == right
if __name__ == '__main__':
reaction = ChemicalReaction([km.CO,km.O2,km.CO], [km.CO2,km.CO2])
print reaction.list_of_atoms('left')
print reaction.list_of_atoms('right')
print reaction.is_balanced()
left_atoms = {}
for mol in reaction.left_side:
elements = mol.list_of_atoms()
for element in elements.keys():
if element in left_atoms:
left_atoms[element] = left_atoms[element] + elements[element]
else:
left_atoms[element] = elements[element]
print left_atoms