Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
140 Kevin 1
def perms_insert(perms, char_prev, char_next):
2
	prev_exist = False
3
	prev_index = 0
4
	next_exist = False
5
	next_index = 0
6
 
7
	# First check if either characters are already in a permutation
8
	for perm in perms:
9
		# Check if the prev char exists in the permutation set
10
		if perm.count(char_prev) != 0:
11
			prev_exist = True
12
			prev_index = perms.index(perm)
13
		# Check if the next char exists in the permutation set
14
		if perm.count(char_next) != 0:
15
			next_exist = True
16
			next_index = perms.index(perm)
17
		# Break out of the loop once both are found
18
		if prev_exist and next_exist:
19
			break
20
 
21
	# Neither exists
22
	if not prev_exist and not next_exist:
23
		# If both chars are the same, insert the character into the permutation set by itself
24
		if char_prev == char_next:
25
			perms.append(char_next)
26
		# Otherwise append the two characters together and insert into the permutation set
27
		else:
28
			perms.append(char_prev+char_next)
29
 
30
	# Only the prev char exists so append the next char to the end of the permutation with the prev char
31
	if prev_exist and not next_exist:
32
		new_perm = perms[prev_index] + char_next
33
		perms.remove(perms[prev_index])
34
		perms.append(new_perm)
35
 
36
	# Only the next char exists so append the prev char to the start of the permutation with the next char
37
	if not prev_exist and next_exist:
38
		new_perm = char_prev + perms[next_index]
39
		perms.remove(perms[next_index])
40
		perms.append(new_perm)
41
 
42
	# Both chars exist
43
	if prev_exist and next_exist:
44
		# Check if they are part of the same permutation, combine permutations if they are not
45
		if prev_index != next_index:
46
			str1 = perms[prev_index]
47
			str2 = perms[next_index]
48
			new_perm = str1 + str2
49
			perms.remove(str1)
50
			perms.remove(str2)
51
			perms.append(new_perm)
52
 
53
	return perms
54
 
55
if __name__ == '__main__':
56
	input_text = 'GAWGST OTJPNA VBPLKM YPSYVU CZSXGU HTNINR PCLSOI WMGREH \
57
				YPSYVU CZSXGU HIZIMP PCLSOI WMGREH YPSYVU CZSXGU IOQHFZ \
58
				QNHDPB XPRCVD YAMYSL CQOXQQ JJHBJB QNHDPB XPRCVD YAMYSL \
59
				DXYMTS KPVOVY RJXNJK XPRCVD ZSTKHE DXYMTS LHXQRK RJXNJK \
60
				XPRCVD ZSTKHE DUOMAQ MVHFYB SGCADV XLICWW ZGGKDH DECMIV \
61
				NTUZNJ SGCADV YEDYIC AYKTCN EVIUYW NTUZNJ TDJWZA YEDYIC \
62
				BOOVFQ EVIUYW NDBZZO TDJWZA YEFYIC BOOVFQ FFEEXG OQMPQL \
63
				TRAWLF YEDYIC BWFVBX FKLEUI OQMPQL UHAJRF YEDYIC BWFVBX \
64
				FYPECM'
65
	# Split the input into an array of 6 letter strings
66
	input_split = input_text.split();
67
 
68
	# Iterate and process through D*A, E*B, F*C
69
	for offset in range(3):
70
		perms = []
71
		# Go through each letter set and insert into permutation list
72
		for entry in input_split:
73
			perms = perms_insert(perms, entry[offset], entry[offset+3])
74
		print offset, offset+3, perms