#ACSL Junior Division Contest 4:  ACSL RUMMY

 

#By Rohan Boyapati

 

#Stone Hill Middle School

 

#Python 2.7.3

 

 

INP = 1

OLIST=[] #list for the outputs

 

#following section prepares the output list

OLIST.append(" ")

OLIST.append("-----------------------------------RESULTS-----------------------------------")

OLIST.append(" ")

OLIST.append("TEST INPUT                                        " + "TEST OUTPUT          ")

OLIST.append(" ")

 

#following section gets the inputs and performs the logic

for INP in (1, 2, 3, 4, 5): #loop to get inputs and perform the logic   

 

    TLIST=[] #list for the input cards

    INPUT_LINE = raw_input("Please Enter Input Line "+ str(INP) +" (with a comma in between): ") #asks for input from the user

  

    K = INPUT_LINE

    K = K.replace(" ","") #replaces spaces with no space

    K = K.upper()

   

    for I in (1,2,3,4,5,6):

        TLIST.append(K[:K.find(",")]) #puts individual cards into an array

        K = K[K.find(",")+1:] #rest of the input

 

    TLIST.append(K[K.find(",")+1:]) #puts last card into an array

   

 

## The following section splits the card value and suit and places them in VLIST

    VLIST=[] #list for value and suit of the cards

    for item in TLIST:

        if item[:1] == 'A':

           VLIST.append([1, item, 1, item[-1:],'NM'])

        if item[:1] == 'T':

           VLIST.append([10, item, 10, item[-1:],'NM'])

        if item[:1] == 'J': 

           VLIST.append([11, item, 11, item[-1:],'NM'])

        if item[:1] == 'Q':

           VLIST.append([12, item, 12, item[-1:],'NM'])

        if item[:1] == 'K':

           VLIST.append([13, item, 13, item[-1:],'NM'])

        if item[:1] <> 'A' and item[:1] <> 'T' and item[:1] <> 'J' and item[:1] <> 'Q' and item[:1] <> 'K':

           VLIST.append([int(item[:1]), item, int(item[:1]), item[-1:],'NM'])

 

    VLIST.sort()

 

       

 

## The following section will check for sets. VLIST will have all the cards after checking for sets

    for I, X in enumerate(VLIST):

       curcard = X[1]

       curnum = X[2]

       cursuit = X[3]

       curmat = X[4]

 

       if curmat == 'NM':

        KLIST=VLIST

        CNT = 1

        for J, Y in enumerate(KLIST):

 

         if curcard <> Y[1]:

           if curnum == Y[2] and Y[4] == 'NM':

             Y[4] = 'MNUM'+str(I)

             X[4] = 'MNUM'+str(I)

             CNT = CNT + 1

        if CNT < 3:

           for K, Z in enumerate(KLIST):

               if Z[4] == 'MNUM'+str(I):

                   Z[4] = 'NM'

                   X[4] = 'NM'

        if CNT == 4:

           for K, Z in enumerate(KLIST):

               if Z[4] == 'MNUM'+str(I):

                   Z[4] = 'MNUMONE'

                   X[4] = 'MNUMONE'

 

 

## SETLIST will have all the cards that belong to a set

    SETLIST = []

    for X in (VLIST):

       if X[4] <> 'NM':

        SETLIST.append([X[0], X[1], X[2], X[3], X[4]])

      

 

## PLIST will have all the cards that do not belong to a set

    PLIST = []

    for X in (VLIST):

       if X[4] == 'NM':

        PLIST.append([X[0], X[1], X[2], X[3], X[4]])

 

 

## The following section checks for sequences. PLIST will have all the cards that do not belong to a set and could possibly be a sequence

    for I, X in enumerate(PLIST):

       maincard = X[1]

       mainnum = X[2]

       mainsuit = X[3]

       mainmatch = X[4]

 

       if mainmatch == 'NM':

        KLIST=PLIST

        CNT = 1

        for J, Y in enumerate(KLIST):

         curcard = Y[1]

         curnum = Y[2]

         cursuit = Y[3]

         curmatch = Y[4]

        

         if maincard <> curcard:

          if (mainnum - 1) == curnum and mainsuit == cursuit and curmatch <> 'NM':

                X[4] = curmatch

                CNT = CNT + 1

                break

          if mainnum + 1 == curnum and mainsuit == cursuit:

                Y[4] = 'MSEQ'+str(I)

                X[4] = 'MSEQ'+str(I)

                CNT = CNT + 1

                break

 

 

## After the following section is executed, PLIST will have all the cards that belong to a sequence and that do not belong to a sequence or a set   

    for I, X in enumerate(PLIST):

       maincard = X[1]

       mainmatch = X[4]

 

       if mainmatch <> 'NM':

        KLIST=PLIST

        CNT = 1

        for J, Y in enumerate(KLIST):

           curcard = Y[1]

           curmatch = Y[4]

           if (maincard <> curcard) and (mainmatch == curmatch):

               CNT = CNT + 1

        if CNT < 3:

           for K, Z in enumerate(KLIST):

               if Z[4] == mainmatch:

                   Z[4] = 'NM'

                   X[4] = 'NM'

        if CNT == 4:

           for K, Z in enumerate(KLIST):

               if Z[4] == mainmatch:

                   Z[4] = 'MSEQONE'

                   X[4] = 'MSEQONE'

 

 

## After the following section is executed, SEQLIST will have all the cards that belong to a sequence. NOMATCHLIST will have all the cards that do not belong to a sequence or a set   

    SEQLIST=[]

    NOMATCHLIST=[]

    for X in (PLIST):

       if X[4] == 'NM':

          NOMATCHLIST.append([X[0], X[1], X[2], X[3], X[4]])

       if X[4] <> 'NM':

          SEQLIST.append([X[0], X[1], X[2], X[3], X[4]])

 

 

 

## After the following section is executed, NOMATCHSTRING contains the cards that do not belong to a set or sequence in the order defined for output

    NOMATCHLIST.sort()

    for X in (NOMATCHLIST):

         X[1] = ''

         if X[3] == 'S': X[3] = 'R4'

         if X[3] == 'H': X[3] = 'R3'

         if X[3] == 'C': X[3] = 'R2'

         if X[3] == 'D': X[3] = 'R1'

    NOMATCHLIST.sort()

 

    NOMATCHSTRING = ''

    for X in (NOMATCHLIST):

         if X[3] == 'R4': ST = 'S'

         if X[3] == 'R3': ST = 'H'

         if X[3] == 'R2': ST = 'C'

         if X[3] == 'R1': ST = 'D'

         if X[2] == 1:  SN = 'A'

         if X[2] == 10: SN = 'T'

         if X[2] == 11: SN = 'J'

         if X[2] == 12: SN = 'Q'

         if X[2] == 13: SN = 'K'

         if X[2] <> 1 and X[2] <> 10 and X[2] <> 11 and X[2] <> 12 and X[2] <> 13:

            SN = X[2]

         if NOMATCHSTRING == '':   

           NOMATCHSTRING = str(SN)+ST

         else:

           NOMATCHSTRING = str(SN)+ST+", "+NOMATCHSTRING

 

 

## After the following section is executed, SETSTRING4 contains 4 card sets; SETSTRING contains 3 card sets in the order defined for output

    SETLIST.sort()

    for X in (SETLIST):

         X[1] = ''

         if X[3] == 'S': X[3] = 'R1'

         if X[3] == 'H': X[3] = 'R2'

         if X[3] == 'C': X[3] = 'R3'

         if X[3] == 'D': X[3] = 'R4'

 

    SETLIST.sort()

      

    SETSTRING = ''

    SETSTRING4 = ''

    for X in (SETLIST):

         if X[3] == 'R1': ST = 'S'

         if X[3] == 'R2': ST = 'H'

         if X[3] == 'R3': ST = 'C'

         if X[3] == 'R4': ST = 'D'

         if X[2] == 1:  SN = 'A'

         if X[2] == 10: SN = 'T'

         if X[2] == 11: SN = 'J'

         if X[2] == 12: SN = 'Q'

         if X[2] == 13: SN = 'K'

         if X[2] <> 1 and X[2] <> 10 and X[2] <> 11 and X[2] <> 12 and X[2] <> 13:

            SN = X[2]

 

         if SETSTRING4 == '' and X[4] == 'MNUMONE':

            SETSTRING4 = str(SN) + ST

         elif SETSTRING4 <> '' and X[4] == 'MNUMONE':

            SETSTRING4 = SETSTRING4+", "+str(SN)+ST

         elif SETSTRING == '' and X[4] <> 'MNUMONE':   

           SETSTRING = str(SN)+ST

         elif SETSTRING <> '' and X[4] <> 'MNUMONE':

           SETSTRING = SETSTRING+", "+str(SN)+ST

 

 

## After the following section is executed, SEQSTRING4 contains 4 card sequences; SEQSTRING contains 3 card sequences in the order defined for output

    SEQLIST.sort()

    for X in (SEQLIST):

         X[1] = ''

         if X[3] == 'S': X[3] = 'R1'

         if X[3] == 'H': X[3] = 'R2'

         if X[3] == 'C': X[3] = 'R3'

         if X[3] == 'D': X[3] = 'R4'

    SEQLIST.sort()

      

    SEQSTRING = ''

    SEQSTRING4 = ''

    for X in (SEQLIST):

         if X[3] == 'R1': ST = 'S'

         if X[3] == 'R2': ST = 'H'

         if X[3] == 'R3': ST = 'C'

         if X[3] == 'R4': ST = 'D'

         if X[2] == 1:  SN = 'A'

         if X[2] == 10: SN = 'T'

         if X[2] == 11: SN = 'J'

         if X[2] == 12: SN = 'Q'

         if X[2] == 13: SN = 'K'

         if X[2] <> 1 and X[2] <> 10 and X[2] <> 11 and X[2] <> 12 and X[2] <> 13:

            SN = X[2]

 

         if SEQSTRING4 == '' and X[4] == 'MSEQONE':

            SEQSTRING4 = str(SN) + ST

         elif SEQSTRING4 <> '' and X[4] == 'MSEQONE':

            SEQSTRING4 = SEQSTRING4+", "+str(SN)+ST

         elif SEQSTRING == '' and X[4] <> 'MSEQONE':   

           SEQSTRING = str(SN)+ST

         elif SEQSTRING <> '' and X[4] <> 'MSEQONE':

           SEQSTRING = SEQSTRING+", "+str(SN)+ST

   

   

## The following section combines SETSTRING4, SEQSTRING4, SEQSTRING, SETSTRING and NOMATCHSTRNG for displaying

    OP = '';

    if SETSTRING4 <> '' and OP == '':

       OP = SETSTRING4

    elif SEQSTRING4 <> '' and OP == '':

       OP = SEQSTRING4

      

    if SETSTRING <> '' and OP == '':

       OP = SETSTRING      

    elif SETSTRING <> '' and OP <> '':

       OP = OP + ', '+ SETSTRING

 

    if SEQSTRING <> '' and OP == '':

       OP = SEQSTRING      

    elif SEQSTRING <> '' and OP <> '':

       OP = OP + ', '+ SEQSTRING

 

    if NOMATCHSTRING <> '' and OP == '':  

      OP = NOMATCHSTRING

    elif NOMATCHSTRING <> '' and OP <> '':

       OP = OP + ', '+ NOMATCHSTRING

 

 

    OLIST.append((str(INP)+". "+INPUT_LINE).ljust(50, ' ') + str(INP)+". "+str(OP)) #adds the input and output to the output list

    OLIST.append(" ") #adds an empty line in the output list

 

#following section prints the output list containing the results

for item in OLIST:

    print item

 

raw_input()