Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import permutations
- n = int(input("Enter n: "))
- Permutations = list(permutations(range(1,n+1))) # Permutations: list of tuples (each tuple contain a combination)
- #print("Initial List: " + str(Permutations))
- from itertools import permutations
- # 1st function (Paragontiko)
- def factorial(x):
- if x== 0:
- return 1;
- else:
- return x*factorial(x-1);
- # 2nd function (Synartisi pou antigrafei olo to string/arithmo ektos tou teleutaiou grammatos)
- def copyPartOfTuple(myTuple): # Argument myTuple = Tuple like one of each unique combinations in Permutations
- newList = list(myTuple) # Make typecast to the tuple ---> becomes list
- size = len(newList)
- newList += newList
- del newList[2*size-1] # Delete to stoixeio se index = 2*size-1
- return newList
- # 3rd funtion (Synartisi pou sou anapodogyrizei to string kai sto kollaei sto arxiko ---> Symmetria)
- def reverseAndExtendList(list):
- initialList = list.copy() # Copy the list to variable initialList
- list.reverse() # EINAI VOID !!!! Den epistrefei lista h synartisi list.reverse() ---> Exei allaxei pleon h list kai egine anapodi
- initialList.extend(list) # Enwnei tin arxiki list (initialList) me tin allagmeni/reversed lista (list)
- return initialList
- '''
- # 4th funtion (Kanei n=4 swsta)
- def makeNCorrectChoices(n, list): # Synithws nCorrect = 4
- newList = []
- for i in range(nCorrect): # Kanw n loops
- for j in range(nCorrect-1): # Gia kathe loop koitw ta n-1 teleytaia grammata
- newList.append(list[(len)list -1 -j]) # kai ta vazw mesa se 1 alli lista
- # H newList exei n-1=3 grammata k psaxnw poio leipei
- '''
- # * * * * M A I N * * * *
- print("All the permutations from 1 to " + str(n) + " is the following list: ")
- print(Permutations); # Swsto ws edw
- print();
- # CASE 1
- if n ==1:
- print("The shortest permutation has length " + str(factorial(n)))
- print("This permutation is: 1");
- # CASE 2
- elif n == 2:
- print("The shortest permutation has length " + str(factorial(n) + factorial(n-1)))
- print("Found 2 permutations/solutions is: 121 or 212")
- # CASE 3
- elif n == 3:
- print("**** All the solutions for n = 3 are the following: :****")
- size = 0
- for i in range(len(Permutations)):
- print(str(i+1) + ") When tuple is: " + str(Permutations[i]) + ", solution is: ");
- plusList = copyPartOfTuple(Permutations[i]) # [1,2,3] ---> [1,2,3,1,2]
- result = reverseAndExtendList(plusList) # To exw kanei symmetriko (tin plusList)
- del result[ int(len(result)/2) ] # TELOS DIADIKASIAS ---> To result einai LISTA
- size = len(result)
- print(''.join(map(str, result)))
- print()
- print("So,all of these results (found 6 solutions) have the minimum length which is: " + str(size))
- print("*****************************************")
- print("*****************************************")
- # CASE 4
- elif n==4:
- print("**** All the solutions for n = 4 are the following: :****")
- for i in range(len(Permutations)):
- print(str(i + 1) + ") When tuple is: " + str(Permutations[i]) + ", solution is: ");
- size = 0
- myList = copyPartOfTuple(Permutations[i]) # Make 4 correct
- myList.append(Permutations[i][0]) # Stin for tha allaksei mono to 1o kai tha ginei i to 2o 0 meneiiiii
- myList.append(myList[len(myList) - (n + 1)])
- myList.append(myList[len(myList) - n])
- myList.append(myList[len(myList) - n])
- myList.append(myList[len(myList) - n])
- myList.append(Permutations[i][1])
- myList.append(myList[len(myList) - (n + 1)])
- myList.append(myList[len(myList) - n])
- myList.append(myList[len(myList) - n])
- myList.append(myList[len(myList) - n]) # Swsto to miso akrivws string
- result = reverseAndExtendList(myList) # To exw kanei symmetriko (tin myList)
- del result[int(len(result) / 2)] # TELOS DIADIKASIAS ---> To result einai LISTA
- size = len(result)
- print(''.join(map(str, result)))
- print()
- print("So,all of these results (found 24 solutions) have the minimum length which is: " + str(size))
- print("*****************************************")
- print("*****************************************")
- else:
- # 1) Beginning: Result = 1st element of Pemrutations
- result = ""
- flag = False
- #print(str(Permutations[0][0]))
- for i in range(n): # Scanning n times
- result += str(Permutations[0][i]) # Result = "123" for example
- del Permutations[0]
- print("Initial List: " + str(Permutations))
- # 2) Continue
- depth = n-1 # In which depth are we searching for same strings
- # Loop: Ends when depth = 0 (when Permutations has no elements)
- while len(Permutations) > 0:
- if flag:
- depth -= 1
- else:
- depth = n-1
- flag = True
- for i in range (len(Permutations)): # Sarwnei olon ton pinaka (n! fores osa kai ta diafora tuples = permutations)
- eystoxies = 0
- for j in range (depth): # Sarwnei tin apantisi mou = result
- if result[len(result)-1-j] == str(Permutations[i][depth-1-j]): # !!!! (AAAA)
- eystoxies += 1
- if eystoxies == depth: # An oses fores epsaksa (depth), toses fores eixa epityxia se "taytisi string"
- for k in range(n - depth): # Iterations = n - depth: depth epityxies ---> prepei na balw n-depth grammata akoma
- result += str(Permutations[i][depth+k]) # !!!! (AAAA)
- del Permutations[i]
- flag = False
- break
- print("Result: " + result)
- print("Length: " + str(len(result)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement