#Sam Henry

#4/8/10

#Potter

#Contest #4 Stacks and Queues

#Enloe HS

#Senior-3

 

active_file=open("H:\\SENIORSTACKSNQUEUES.txt","r")#read from this file

 

def printcommand(printnum,struct,stack):

    if stack==True:

        print struct[len(struct)-printnum:]

    else:

        print struct[:printnum]

 

for line in active_file:

    struct=['A','B','C','D','E']

    hold=''

    commands=[]

    for character in line[2:]:

        if character==',' and hold[:3]=="INS" and not temp:

            temp=True

        elif character==',':

            commands.append(hold)

            hold=''

            temp=False

        else:

            hold+=character

    commands.append(hold)

    if line[0]=='S':

        stack=True

    else:

        stack=False

    for com in commands:

        if com[:3]=="POP":

            struct=struct[int(com[3])*int((not stack)):len(struct)-int(com[3])*int(stack)]

        elif com[:3]=="PSH":

            struct.append(com[3])

        elif com[:3]=="DUP":

            struct+=struct[:int(com[3])]

        elif com[:3]=="SWP":

            struct=struct[len(struct)-int(com[3]):]+struct[int(com[3]):len(struct)-int(com[3])]+struct[0:int(com[3])]

        elif com[:3]=="SWH":

            stack=not stack

        elif com[:3]=="CRC":

            struct=struct[len(struct)-int(com[3]):]*int(stack)+struct[int(com[3])*int(not stack):len(struct)-int(com[3])*int(stack)]+(struct[:int(com[3])]*int(not stack))

        elif com[:3]=="INS":

            struct.insert((len(struct)*int(stack))+((1-(2*int(stack)))*(int(com[3])-1)),com[4])

        elif com[:3]=="PIN":

            struct.insert(0,com[3])

        elif com[:3]=="SRT":

            struct.sort()

            if com[3]=="d":

                struct.reverse()

        elif com[:3]=="PRT":

            printcommand(int(com[3]),struct,stack)