Tuesday, January 26, 2016

How to remove left factoring (Python source code)


# s = [['a'],['a','b'],['a','b','c'], ['a','b','c','d']]
# s = [ ['b','s','s','a','a','s'], ['b','s','s','a','s','b'], ['b','s','b'], ['a']]
# s = s = [ ['b','s'], ['b','s','s','a','s','b'], ['b','s','b'], ['a']]
s = [ ['a','s','s','b','s'],['a','s','a','s','b'], ['a','b','b'] ,['b'] ]
# s = [['a'],['a','b']]
# s = [ ['c','c','d'], ['d','d','c']]
# s = [ ['a','A','d'],['a','B'] ]



#length = len(s)
# print (length)

def calcInnerListLen( aList ):
innerListLength = []
cListLen = 0 # current list length

length = len(aList)
for i in range(length):
cListLen = len( aList[i] )
innerListLength.append( [i,cListLen,False] ) #grammar index, length, can't swap this i.e fixed flag
return innerListLength


def minLengthIndex( aList, excludingIndex):

return

#########################################
fixedFlag = 2 #can swapp for current index
iLength = 1 #index For Length
orgGramIndex = 0 #orginal grammar index
#represented by tuple
# [oGramIndex, iLength]
# #i.e. iLength is length for oGramIndex
def sortByLength( indexLengthList ):

length = len( indexLengthList ) #advantage of oo, don't calc length twice :p

smallest = indexLengthList[0]
current2 = indexLengthList[0]
current2n = current2
tmp = current2

value = current2[iLength]
valueN = value

for i in range(length-1):
for j in range(length-1-i):
current2 = indexLengthList[j]
value = current2[iLength]

current2N = indexLengthList[j + 1]
valueN = current2N[iLength]

if value > valueN:
tmp = indexLengthList[j]
indexLengthList[j] = indexLengthList[j + 1]
indexLengthList[j + 1] = tmp

#current2[j], current2[j+1] = current2[j+1], current2[j]

return indexLengthList


def matchLeastLength( sortedIndexLengthList):
length = len( sortedIndexLengthList)


for i in range(length):
cIndexLenList = sortedIndexLengthList[i]
cLength = cIndexLenList[iLength]
cIndex = cIndexLenList[ orgGramIndex]
cg = s[ cIndex ] #current grammar
print
print ["currentIndex => ", cIndex ,cg]

hasCommon = False
j = 0
while( j < length ):
hasCommon = False
nIndexLenList = sortedIndexLengthList [j]
nIndex = nIndexLenList[ orgGramIndex ]
nfixedFlag = nIndexLenList [ fixedFlag ]
ng = s[ nIndex] #nextGrammar
print ["NextIndex => ", nIndex ,ng]

k = 0
if (cg): #if not empty, i.e. dequed, i.e. [[], ['a', 'b'], ['a', 'b', 'c'], ['a', 'b', 'c', 'd']]
cgChar = cg[k]

#dbgIndexMove = []
#while(k < cLength): #finding common from grammar

try:
print [ "Current => ", cg[k], "Next => ", ng[k] ]
except Exception, e:
print [ "EXCEPTION => ","Current => ", cg, "Next => ", ng ]
else:
pass
finally:
pass

if( s [nIndex] and cIndex != nIndex ):
#dbgIndexMove.append(k)
#print cgChar
#print k
#print ng[k]
if cgChar == ng[k] and nfixedFlag == False:
if ( len(s) == 0):
continue
s[nIndex].pop(0) #no s not pop, s ko content will pop
hasCommon = True
#pop() removes last item, not first item :p
#i.e. index n, not index 0 .... \.0./

j+=1
if(hasCommon and j == length):
s[cIndex].pop(0)
j = 0
cIndexLenList[fixedFlag] = True

print s



return

print (s)

ill = calcInnerListLen( s)
print(ill)


sIndexLenList = sortByLength( ill )
print ( sIndexLenList )


final = matchLeastLength( sIndexLenList )


print (s)

print sIndexLenList

3 comments:

  1. Replies
    1. Well i have written it a long ago when i was still in my early life of college. So no wonder if it helps someone i don't care if its messy.

      Delete