-
Notifications
You must be signed in to change notification settings - Fork 0
/
Balance.py
251 lines (220 loc) · 10.1 KB
/
Balance.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Classe gérant l'equilibrage d'équation
"""
#Disclaimer : Pour tester, utiliser l'equation de combustion du méthane
#dans le dioxygene :
#CH4 + O2 -> CO2 + H2O
class Balance:
def __init__(self):
self.A = dict()
self.B = dict()
self.C = dict()
self.D = dict()
self.coeffA = 1
self.coeffB = 1
self.coeffC = 1
self.coeffD = 1
self.LIMIT = 20
def balanceEquation(self, rOne, rTwo, pOne, pTwo):
"""
Equilibre une equation en calculant les différents
coefficients stoechiométriques.
:param rOne: Premier reactif de l'équation
:param rTwo: Second reactif de l'équation
:param pOne: Premier produit de l'équation
:param pTwo: Second produit de l'équation
:type rOne: ChemicalElement
:type rTwo: ChemicalElement
:type pOne: ChemicalElement
:type pTwo: ChemicalElement
:return: Les coefficients stoechiometriques de chaque element
:rtype: int[3]
"""
print "balance equation"
return self.parseCoef(rOne, rTwo, pOne, pTwo)
def parseCoef(self, rOne, rTwo, pOne, pTwo):
"""
Analyse une equation pour recuperer le nombre d'atomes
de chaque côté de l'équation
:param rOne: Premier reactif de l'équation
:param rTwo: Second reactif de l'équation
:param pOne: Premier produit de l'équation
:param pTwo: Second produit de l'équation
:type rOne: ChemicalElement
:type rTwo: ChemicalElement
:type pOne: ChemicalElement
:type pTwo: ChemicalElement
:return: - (les resultats sont sauvegardés dans l'instance de classe)
:rtype: void
"""
self.A = self.parseEntry(rOne)
self.B = self.parseEntry(rTwo)
self.C = self.parseEntry(pOne)
self.D = self.parseEntry(pTwo)
print "A =>"
print self.A
print "B =>"
print self.B
print "C =>"
print self.C
print "D =>"
print self.D
# BruteForce : Tq l'equation n'est pas equilibre, on incremente les coeffs jusqu'a la limite
while (not(self.isEquilibry())) :
self.upCoeff()
if (not(self.D)) :
print str(self.coeffA) + str(rOne) + "+" + str(self.coeffB) + str(rTwo) + "=" + str(self.coeffC) + str(pOne)
else :
print str(self.coeffA) + str(rOne) + "+" + str(self.coeffB) + str(rTwo) + "=" + str(self.coeffC) + str(pOne) + "+" + str(self.coeffD) + str(pTwo)
result = []
result.append(self.coeffA)
result.append(self.coeffB)
result.append(self.coeffC)
result.append(self.coeffD)
# Re initialise les coeffs pour la prochaine execution
self.coeffA = 1
self.coeffB = 1
self.coeffC = 1
self.coeffD = 1
return result
def upCoeff(self) :
if (self.coeffD == self.LIMIT or not(self.D)) :
self.coeffD = 1
if (self.coeffC == self.LIMIT) :
self.coeffC = 1
if (self.coeffB == self.LIMIT):
self.coeffB = 1
if (self.coeffA == self.LIMIT):
self.coeffA = 1
else :
self.coeffA += 1
else :
self.coeffB += 1
else :
self.coeffC += 1
else :
self.coeffD += 1
def isEquilibry(self):
if (self.equilibreA()) :
if (self.equilibreB()):
return True
return False
def equilibreA(self) :
numberL = ""
letterL = ""
numberR = ""
letterR = ""
for k, v in self.A.items() :
numberL = v
letter = k
numberL_B = self.getResultForLetter(k, self.B)
numberR_C = self.getResultForLetter(k, self.C)
numberR_D = self.getResultForLetter(k, self.D)
if (numberR_C != -1 and numberR_D != -1) :
if (numberL_B != -1) :
#print str(numberL) + "x" + str(self.coeffA) + "+" + str(numberL_B) + "x" + str(self.coeffB) + "=" + str(numberR_C) + "x" + str(self.coeffC) + "+" + str(numberR_D) + "x" + str(self.coeffD)
if (int(numberL) * int(self.coeffA) + int(numberL_B) * int(self.coeffB) != int(numberR_C) * int(self.coeffC) + int(numberR_D) * int(self.coeffD)):
return False
else :
#print str(numberL) + "x" + str(self.coeffA) + "=" + str(numberR_C) + "x" + str(self.coeffC) + "+" + str(numberR_D) + "x" + str(self.coeffD)
if (int(numberL) * int(self.coeffA) != int(numberR_C) * int(self.coeffC) + int(numberR_D) * int(self.coeffD)):
return False
elif (numberR_C != -1 and numberR_D == -1):
if (numberL_B != -1) :
#print str(numberL) + "x" + str(self.coeffA) + "+" + str(numberL_B) + "x" + str(self.coeffB) + "=" + str(numberR_C) + "x" + str(self.coeffC)
if (int(numberL_B) * int(self.coeffB) + int(numberL) * int(self.coeffA) != int(numberR_C) * int(self.coeffC)):
return False
else :
#print str(numberL) + "x" + str(self.coeffA) + "=" + str(numberR_C) + "x" + str(self.coeffC)
if (int(numberL) * int(self.coeffA) != int(numberR_C) * int(self.coeffC)):
return False
elif (numberR_C == -1 and numberR_D != -1):
if (numberL_B != -1) :
#print str(numberL) + "x" + str(self.coeffA) + "+" + str(numberL_B) + "x" + str(self.coeffB) + "=" + str(numberR_D) + "x" + str(self.coeffD)
if (int(numberL_B) * int(self.coeffB) + int(numberL) * int(self.coeffA) != int(numberR_D) * int(self.coeffD)):
return False
else :
#print str(numberL) + "x" + str(self.coeffA) + "=" + str(numberR_D) + "x" + str(self.coeffD)
if (int(numberL) * int(self.coeffA) != int(numberR_D) * int(self.coeffD)):
return False
return True
def equilibreB(self) :
numberL = ""
letterL = ""
numberR = ""
letterR = ""
for k, v in self.B.items() :
numberL = v
letter = k
numberL_A = self.getResultForLetter(k, self.A)
numberR_C = self.getResultForLetter(k, self.C)
numberR_D = self.getResultForLetter(k, self.D)
if (numberR_C != -1 and numberR_D != -1) :
if (numberL_A != -1) :
#print str(numberL) + "x" + str(self.coeffB) + "+" + str(numberL_A) + "x" + str(self.coeffA) + "=" + str(numberR_C) + "x" + str(self.coeffC) + "+" + str(numberR_D) + "x" + str(self.coeffD)
if (int(numberL) * int(self.coeffB) + int(numberL_A) * int(self.coeffA) != int(numberR_C) * int(self.coeffC) + int(numberR_D) * int(self.coeffD)):
return False
else :
#print str(numberL) + "x" + str(self.coeffB) + "=" + str(numberR_C) + "x" + str(self.coeffC) + "+" + str(numberR_D) + "x" + str(self.coeffD)
if (int(numberL) * int(self.coeffB) != int(numberR_C) * int(self.coeffC) + int(numberR_D) * int(self.coeffD)):
return False
elif (numberR_C != -1 and numberR_D == -1):
if (numberL_A != -1) :
#print str(numberL) + "x" + str(self.coeffB) + "+" + str(numberL_A) + "x" + str(self.coeffA) + "=" + str(numberR_C) + "x" + str(self.coeffC)
if (int(numberL_A) * int(self.coeffA) + int(numberL) * int(self.coeffB) != int(numberR_C) * int(self.coeffC)):
return False
else :
#print str(numberL) + "x" + str(self.coeffB) + "=" + str(numberR_C) + "x" + str(self.coeffC)
if (int(numberL) * int(self.coeffB) != int(numberR_C) * int(self.coeffC)):
return False
elif (numberR_C == -1 and numberR_D != -1):
if (numberL_A != -1) :
#print str(numberL) + "x" + str(self.coeffB) + "+" + str(numberL_A) + "x" + str(self.coeffA) + "=" + str(numberR_D) + "x" + str(self.coeffD)
if (int(numberL_A) * int(self.coeffA) + int(numberL) * int(self.coeffB) != int(numberR_D) * int(self.coeffD)):
return False
else :
#print str(numberL) + "x" + str(self.coeffB) + "=" + str(numberR_D) + "x" + str(self.coeffD)
if (int(numberL) * int(self.coeffB) != int(numberR_D) * int(self.coeffD)):
return False
return True
def getResultForLetter(self, letter, collec) :
if letter in collec :
return collec[letter]
else :
return -1
def parseEntry(self, entry):
"""
Filtre une entrée et retourne
:param entry: Molecule
:type entry: string
:return: Le nombre de chaque atome
:rtype: dictionary<string, int>
"""
"""else:
if (element != ""):
result[element] = 1
element = ""
element += char"""
result = dict()
element = ""
value = ""
for char in entry:
if str.isupper(char):
if element != "":
result[element] = 1
element = ""
element += char
elif str.islower(char):
element += char
elif str.isdigit(char):
value += char
if (element != ""):
result[element] = value
element = ""
value = ""
if (element != ""):
result[element] = 1
print result
return result