Advertisement
Guest User

Untitled

a guest
Apr 9th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. import re
  2. from copy import deepcopy
  3.  
  4. def freePipes(s):
  5.     c = 0
  6.     for e in s:
  7.         if e == '(': c+=1
  8.         elif e == ')': c-=1
  9.         elif e == '|' and c == 0: return True
  10.     return False
  11.        
  12. def containPipes(s):
  13.     if freePipes(s):
  14.         return '(%s)'%s
  15.     else:
  16.         return s
  17.  
  18. class eq(object):
  19.     def __init__(self, eqNum, terms):
  20.         self.eqNum = eqNum
  21.         self.terms = terms[::]
  22.     def isolateRHS(self):
  23.         if self.terms[self.eqNum]:
  24.             #apply rule A=xA+y is equivelent to A=x*y
  25.             if len(self.terms[self.eqNum]) > 1:toadd = '(%s)*'%self.terms[self.eqNum]
  26.             else: toadd = self.terms[self.eqNum]+'*'
  27.             self.terms[self.eqNum] = ''
  28.             for i in xrange(len(self.terms)):
  29.                 if self.terms[i]:
  30.                     self.terms[i] = toadd+containPipes(self.terms[i])
  31.     def subIn(self, eq2):
  32.         if self.terms[eq2.eqNum]:
  33.             prefix = self.terms[eq2.eqNum]
  34.             self.terms[eq2.eqNum] = ''
  35.             for i,t in enumerate(eq2.terms):
  36.                 if not t: continue
  37.                 tmp = containPipes(prefix) + containPipes(t)
  38.                 if self.terms[i]:
  39.                     #tmp = '%s|%s'%(containPipes(self.terms[i]),containPipes(tmp))
  40.                     tmp = self.terms[i]+'|'+tmp
  41.                 self.terms[i] = tmp
  42.  
  43. def convertEquations(equations):
  44.         eqs = [None]*len(equations)
  45.         baseTerms = ['']*len(equations)
  46.         for i in xrange(len(equations)):
  47.                 terms = list(baseTerms)
  48.                 for k,v in equations[i].items():
  49.                         terms[k] = v
  50.                 eqs[i] = eq(i,terms)
  51.         return eqs
  52.  
  53. def compress(eqs, cmpto):
  54.         for i in xrange(len(eqs)-1,-1,-1):
  55.                 if i == cmpto: continue
  56.                 eqs[i].isolateRHS()
  57.                 for j in xrange(i):
  58.                         eqs[j].subIn(eqs[i])
  59.                 eqs[cmpto].subIn(eqs[i])
  60.         if len(eqs[cmpto].terms[cmpto]) == 1: return eqs[cmpto].terms[cmpto]+'*'
  61.         return ('('+eqs[cmpto].terms[cmpto]+')*') if eqs[cmpto].terms[cmpto] else ''
  62.  
  63. def getRegexFromEqs(equations):
  64.         eqs = convertEquations(equations)
  65.         return '|'.join(filter(None,(compress(deepcopy(eqs),i)for i in xrange(len(eqs)))))
  66.                
  67.  
  68.  
  69.  
  70.  
  71. class graph(object):
  72.        
  73.  
  74.         def __init__(self,maxheight, balls):
  75.                 self.edgeList = {}
  76.                 self.ids = {}
  77.                 self.idCount = 0
  78.                 s = (1,)*balls + (0,)*(maxheight-balls)
  79.                 self.makeGraph(s)
  80.        
  81.         def makeGraph(self, state):
  82.                 if state in self.ids: return
  83.                 throws = {}
  84.                 self.ids[state] = self.idCount
  85.                 self.idCount+=1
  86.                 for th in self.possThrows(state):
  87.                         st = self.doThrow(state,th)
  88.                         self.makeGraph(st)
  89.                         throws[self.ids[st]] = str(th)
  90.                 self.edgeList[self.ids[state]] = throws
  91.  
  92.         def possThrows(self, state):
  93.                 if state[0] == 0: yield 0
  94.                 else:
  95.                         for th in xrange(1,len(state)):
  96.                                 if not state[th]: yield th
  97.                         yield len(state)
  98.  
  99.         def doThrow(self, state, th):
  100.                 if th == 0: return state[1:]+(0,)
  101.                 elif th == len(state): return state[1:]+(1,)
  102.                 return state[1:th]+(1,)+state[th+1:]+(0,)
  103.  
  104.  
  105. def getFullRegex(maxheight):
  106.         res = []
  107.         for i in xrange(maxheight+1):
  108.                 g = graph(maxheight,i)
  109.                 res.append(getRegexFromEqs(g.edgeList))
  110.                 del g
  111.         return '^('+'|'.join(res)+')$'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement