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 meros enos string/arithmou)
- 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
- # * * * * 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("These permutations is 121 or 212")
- # CASE 3
- elif n == 3:
- print("**** All the solutions for n = 3 are the following: :****")
- 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
- print(result)
- print()
- print("*****************************************")
- print("*****************************************")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement