Leave hydrogen and oxygen for last because they are more often found in more than one chemical on each side of the equation.ī. Pick an element that appears in one molecule on the left side and in one molecule on the left.Ī. The complete code has been attached below import re from sympy import Matrix, lcm elementList= elementMatrix= print("please input your reactants, this is case sensitive") print("your input should look like: H2O Ag3(Fe3O)4") reactants=input("Reactants: ") print("please input your products, this is case sensitive") products=input("Products: ") reactants=reactants.replace(' ', '').split(" ") products=products.replace(' ', '').split(" ") def addToMatrix(element, index, count, side): if(index = len(elementMatrix)): elementMatrix.append() for x in elementList: elementMatrix.append(0) if(element not in elementList): elementList.append(element) for i in range(len(elementMatrix)): elementMatrix.append(0) column=elementList.index(element) elementMatrix =count*side def findElements(segment,index, multiplier, side): elementsAndNumbers=re.split('(?)',segment) i=0 while(i0): if(elementsAndNumbers.isdigit()): count=int(elementsAndNumbers)*multiplier addToMatrix(elementsAndNumbers, index, count, side) i =1 else: addToMatrix(elementsAndNumbers, index, multiplier, side) def compoundDecipher(compound, index, side): segments=re.split('(\(*\)*)',compound) for segment in segments: if segment.startswith("("): segment=re.split('\)(*)',segment) multiplier=int(segment) segment=segment else: multiplier=1 findElements(segment, index, multiplier, side) for i in range(len(reactants)): compoundDecipher(reactants,i,1) for i in range(len(products)): compoundDecipher(products,i len(reactants),-1) elementMatrix = Matrix(elementMatrix) elementMatrix = anspose() solution=elementMatrix.nullspace() multiple = lcm() solution = multiple*solution coEffi=solution.1. Thanks for reading, you are now able to answer all those pesky Balancing chemical Equations questions on Khan academy. we will use the to remove the first character of our segment as it’s a bracket we don’t need. The rest of the compound will be the first segment. we now know that the number to the right of the parenthesis which I refer to as a “multiplier” will be the second half of our split. We are now going to loop through our segments, if a segment begins with a ( we want to split the segment so that the part with the number is separated from the rest. If you want to play around with regular expressions you can view this one here. and the near the end, indicate that we want to include ALL digits to the right of our parenthesis in our split. It means that we are ok with having zero or infinite many letters(of any case) or numbers within our parenthesis. The inner parenthesis with the forward slash before them mean that we want to literally find parenthesis(this is called escaping) the indicate that we are ok with any letter(of any case) or number within our parentheses and the asterisk after the square brackets is a quantifier. To breakdown the regex, the outermost parenthesis(near the single quotes) indicate that that is our capture group and it is what we want to keep.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |