Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 * * * *
- # Ask the user for input
- n = int(input("Enter number n: "))
- #Create the list of all permutations
- Permutations = list(permutations(range(1, n+1)))
- 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("*****************************************")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement