Advertisement
Vermiculus

ugh

Mar 18th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1. def d(s):
  2.     raw_input("----DEBUG----{0}".format(s))
  3.  
  4. # Class Molecule:
  5. #   elements: a dictionary of elements and their subscripts
  6. #   count: the coefficient on the molecule
  7. class Molecule:
  8.     class Atomic: # Represents a {Element}_{Subscript} structure (no coefficient)
  9.         def __init__(self, el, sub):
  10.             self.element = el
  11.             self.subscript = sub
  12.         def getElement(self):
  13.             return self.element
  14.         def getSubscript(self):
  15.             return self.subscript
  16.  
  17.     def __init__(self):
  18.         self.elements = dict()
  19.         self.count = 1
  20.  
  21.     def __str__(self):
  22.         ret = "{"+str(self.count)+"}"
  23.         for el in self.elements:
  24.             ret += "[{0}_{1}]".format(el,self.elements[el])
  25.         return ret
  26.  
  27.     def add(self, atomic):
  28.         self.add(atomic.getElement(), atomic.getSubscript())
  29.  
  30.     def add(self, element, subscript):
  31.         # if the molecule already contains this element, just increase the subscript
  32.         if element in self.elements:
  33.             self.elements[element] += subscript
  34.         else: # otherwise, add the element and its subscript
  35.             self.elements[element] = subscript
  36.    
  37.     def fold(self, mol):
  38.         for el in mol.elements.keys():
  39.             self.add(el, mol.elements[el])
  40.  
  41. def getWord(s,i): # Returns a [isElem, token, lastIndex] list from a string s starting at index i
  42.     if i >= len(s) or i < 0:
  43.         return Nothing
  44.    
  45.     ret = s[i]
  46.     isElem = ret.isalpha()
  47.     i += 1
  48.     while i < len(s):
  49.         #d(str((isDigit, s[i], i, ret)))
  50.         if not isElem:
  51.             if s[i].isdigit():
  52.                 ret += s[i]
  53.             else:
  54.                 return [isElem,ret,i]
  55.         else:
  56.             if s[i].isupper() or s[i].isdigit():
  57.                 return [isElem,ret,i]
  58.             else:
  59.                 ret += s[i]
  60.         i = i + 1
  61.     return [isElem,ret,i]
  62.  
  63. def tokenize(s):
  64.     l = list()
  65.     i = 0
  66.     while i < len(s):
  67.         newi = getWord(s, i)
  68.         i = newi[2]
  69.         l.append(newi)
  70.     return l
  71.  
  72. def standardize(tokens): # inserts implicit subscripts and puts them into ints
  73.     if tokens[0][0]:
  74.         tokens.insert(0,[False,1,0])
  75.     i = 0
  76.     while i < len(tokens):
  77.         if tokens[i][0]: # if this is an element
  78.             if i+1 == len(tokens):
  79.                 tokens.append([False,1,tokens[i][2]])
  80.             elif tokens[i+1][0]: # if the next is also an element (ie not a subscript)
  81.                 tokens.insert(i+1,[False,1,tokens[i][2]])
  82.         else: # if this was a subscript
  83.             tokens[i][1] = int(tokens[i][1])
  84.         i = i + 1
  85.    
  86.     return tokens
  87.  
  88. def molecularize(tokens):
  89.     mol = Molecule()
  90.     mol.count = tokens[0][1]
  91.     for i in range(1,len(tokens),2):
  92.         mol.add(tokens[i][1], tokens[i+1][1])
  93.     return mol
  94.  
  95. def pl(l):
  96.     for n in l:
  97.         print n
  98.  
  99. def fission(s): # splits a string 'A=B' up into molecular components [[A1,A2,...],[B1,B2,...]
  100.     lnr = s.split('=')
  101.     if len(lnr) != 2:
  102.         print 'error'
  103.         return Nothing
  104.     left = lnr[0].split('+')
  105.     right = lnr[1].split('+')
  106.     return [left,right]
  107.  
  108.  
  109. # NaOH = "NaOH"
  110. # Na2O3H = "Na2O3HC"
  111. #
  112. # t_NaOH = tokenize(NaOH)
  113. # standardize(t_NaOH)
  114. # m_NaOH = molecularize(t_NaOH)
  115. #
  116. # print "Molecule: {}".format(m_NaOH)
  117. #
  118. #
  119. # t_Na2O3H = tokenize(Na2O3H)
  120. # standardize(t_Na2O3H)
  121. # m_Na2O3H = molecularize(t_Na2O3H)
  122. #
  123. # print "Molecule: {}".format(m_Na2O3H)
  124. #
  125. # m_NaOH.fold(m_Na2O3H)
  126. #
  127. # print "Molecule: {}".format(m_NaOH)
  128.  
  129. print fission("H2O+NaOH=C6H12O6")
  130.  
  131. # to do the function multiple times, map(balance, sequence_of_split_tokens)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement