testFlag = 0

from string import *
class element:
    def __init__(self,_etype):
        self.etype = _etype

    def IsSameAs(self,elt): #returns 1 if the object is the same else 0
        return 0 #all derived classes must override this function

    def getCompStr(self):
        return '' #all derived classes override this - provides a string representation of the element

class SElt(element):
    def __init__(self,_obj):
        self.etype = 1
        self.obj = _obj #for simpleElts, obj is a primitive

    def IsSameAs(self,elt):
        if (elt.etype!=self.etype):
            return 0
        if (elt.obj!=self.obj):
            return 0
        return 1

    def getCompStr(self):
        return repr(self.obj) + ','

class component(element):
    def __init__(self):
        self.etype = 2
        self.eltList = []

    def __del__(self):
        del self.eltList

    def getCompStr(self):
        retstr = ''
        for i in range(0,len(self.eltList)):
            retstr = retstr + self.eltList[i].getCompStr()
        return retstr

    def CreateComponent(self,mystring): #from a string such as '1,2,3,4,10' it will create eltList
        while (1):
            idx = find(mystring,',')
            if idx==-1:
                a = SElt(mystring)
                self.eltList.append(a)
                break
            string1 = mystring[0:idx]
            mystring = mystring[idx+1:]
            a = SElt(string1)
            self.eltList.append(a)

    def IsSameAs(self,elt):
        if elt.etype!=self.etype:
            return 0
        if len(elt.eltList)!=len(self.eltList):
            return 0
        for i in range(0,len(self.eltList)):
            if (self.eltList[i].IsSameAs(elt.eltList[i])!=1):
                return 0
        return 1

    def AddToEltList(self,_element):
        self.eltList.append(_element)

    def getComponent(self):
        return self.eltList

    def MakePeriodic(self):
        global testFlag
        eltIdx = 0
        while (1):
            if eltIdx>=len(self.eltList):
                break
            val0 = self.eltList[eltIdx]
            val0idxs = []
            #append starting points to the list for this element
            for i in range(eltIdx,len(self.eltList)):
                if (self.eltList[i].IsSameAs(val0)==1):
                    val0idxs.append(i)
            #Now check to see if there are any adjacent indices in val0idxs and only
            #store the higher of them
            i = 1
            while(1):
                if i>=len(val0idxs):
                    break
                if val0idxs[i]==val0idxs[i-1]+1:
                    del val0idxs[i-1] #the elements are shifted down so you don't need to
                else:               #decrement i.
                    i = i+1

            #if there are no repeats of this element then go on to the next element                    
            if len(val0idxs)==1:
                eltIdx = eltIdx +1
                continue
            
            #Now check to see if consequetive elements are periodic
            testComp = component()
            for i in range(eltIdx,val0idxs[1]): #eltIdx should equal val0idxs[0]
                testComp.AddToEltList(self.eltList[i])
                
            pcount = 1
            idx = 1
            while (1):
                if idx>=len(val0idxs):
                    break
                #if it is the last one then it may be truncated
                if (idx==(len(val0idxs)-1)) and (len(self.eltList)-val0idxs[idx])<len(testComp.eltList):
                    break
                testComp1 = component()
                for i in range(val0idxs[idx],val0idxs[idx]+len(testComp.eltList)):
                    if i>=len(self.eltList):
                        break
                    testComp1.AddToEltList(self.eltList[i])
                                                
                #if testComp1 is the same as testComp then there is another period
                testFlag = 0
                x = testComp1.IsSameAs(testComp)
                testFlag = 0
                if (x==1):
                    pcount = pcount +1
                    idx = idx+1
                else:
                    break #if it isn't then the sequence stops here

            if pcount>1:
                #print 'pcount = '+repr(pcount)
                #print 'val0idxs' + repr(val0idxs)
                eltList1 = self.eltList
                
                #work out how many elements need to be replaced
                noElts = len(testComp.eltList)*pcount
                for i in range(1,noElts): #range object is inclusive
                    del eltList1[eltIdx+1] #elements shifted down
                #just left with eltList1[eltIdx]
                eltList1[eltIdx] = periodElt() #allowed to do this in PYTHON since run-time language
                eltList1[eltIdx].SetNoOfPeriods(pcount)
                for i in range(0,len(testComp.eltList)):
                    eltList1[eltIdx].AddToEltList(testComp.eltList[i])
    
                self.eltList = eltList1
                self.eltList[eltIdx].MakePeriodic()
            eltIdx=eltIdx+1

class periodElt(component):
    def __init__(self):
        self.eltList = []
        self.etype = 3
        self.nperiods = 0

    def __del__(self):
        del self.eltList

    def getCompStr(self):
        retstr = 'P,'
        for i in range(0,len(self.eltList)):
            retstr = retstr + self.eltList[i].getCompStr()
        retstr = retstr + 'N,' + repr(self.nperiods) + 'E,'
        return retstr

    def IsSameAs(self,elt):
        if component.IsSameAs(self,elt)==0:
            return 0
        if self.nperiods!=elt.nperiods:
            return 0
        return 1

    def SetNoOfPeriods(self,_num):
        self.nperiods = _num

    def MakePeriodic(self):
        component.MakePeriodic(self)
    