Blame | Last modification | View Log | Download | RSS feed
def perms_insert(perms, char_prev, char_next):prev_exist = Falseprev_index = 0next_exist = Falsenext_index = 0# First check if either characters are already in a permutationfor perm in perms:# Check if the prev char exists in the permutation setif perm.count(char_prev) != 0:prev_exist = Trueprev_index = perms.index(perm)# Check if the next char exists in the permutation setif perm.count(char_next) != 0:next_exist = Truenext_index = perms.index(perm)# Break out of the loop once both are foundif prev_exist and next_exist:break# Neither existsif not prev_exist and not next_exist:# If both chars are the same, insert the character into the permutation set by itselfif char_prev == char_next:perms.append(char_next)# Otherwise append the two characters together and insert into the permutation setelse:perms.append(char_prev+char_next)# Only the prev char exists so append the next char to the end of the permutation with the prev charif prev_exist and not next_exist:new_perm = perms[prev_index] + char_nextperms.remove(perms[prev_index])perms.append(new_perm)# Only the next char exists so append the prev char to the start of the permutation with the next charif not prev_exist and next_exist:new_perm = char_prev + perms[next_index]perms.remove(perms[next_index])perms.append(new_perm)# Both chars existif prev_exist and next_exist:# Check if they are part of the same permutation, combine permutations if they are notif prev_index != next_index:str1 = perms[prev_index]str2 = perms[next_index]new_perm = str1 + str2perms.remove(str1)perms.remove(str2)perms.append(new_perm)return permsif __name__ == '__main__':input_text = 'GAWGST OTJPNA VBPLKM YPSYVU CZSXGU HTNINR PCLSOI WMGREH \YPSYVU CZSXGU HIZIMP PCLSOI WMGREH YPSYVU CZSXGU IOQHFZ \QNHDPB XPRCVD YAMYSL CQOXQQ JJHBJB QNHDPB XPRCVD YAMYSL \DXYMTS KPVOVY RJXNJK XPRCVD ZSTKHE DXYMTS LHXQRK RJXNJK \XPRCVD ZSTKHE DUOMAQ MVHFYB SGCADV XLICWW ZGGKDH DECMIV \NTUZNJ SGCADV YEDYIC AYKTCN EVIUYW NTUZNJ TDJWZA YEDYIC \BOOVFQ EVIUYW NDBZZO TDJWZA YEFYIC BOOVFQ FFEEXG OQMPQL \TRAWLF YEDYIC BWFVBX FKLEUI OQMPQL UHAJRF YEDYIC BWFVBX \FYPECM'# Split the input into an array of 6 letter stringsinput_split = input_text.split();# Iterate and process through D*A, E*B, F*Cfor offset in range(3):perms = []# Go through each letter set and insert into permutation listfor entry in input_split:perms = perms_insert(perms, entry[offset], entry[offset+3])print offset, offset+3, perms