Advertisement
makispaiktis

Superpermutations until n=3

Apr 10th, 2019 (edited)
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. from itertools import permutations
  2.  
  3. # 1st function (Paragontiko)
  4. def factorial(x):
  5.     if x== 0:
  6.         return 1;
  7.     else:
  8.         return x*factorial(x-1);
  9.  
  10. # 2nd function (Synartisi pou antigrafei meros enos string/arithmou)
  11. def copyPartOfTuple(myTuple):                    # Argument myTuple = Tuple like one of each unique combinations in Permutations
  12.     newList = list(myTuple)                      # Make typecast to the tuple ---> becomes list
  13.     size = len(newList)
  14.     newList += newList
  15.     del newList[2*size-1]                       # Delete to stoixeio se index = 2*size-1
  16.     return newList
  17.  
  18. # 3rd funtion (Synartisi pou sou anapodogyrizei to string kai sto kollaei sto arxiko ---> Symmetria)
  19. def reverseAndExtendList(list):
  20.     initialList = list.copy()                   # Copy the list to variable initialList
  21.     list.reverse()                              # EINAI VOID !!!! Den epistrefei lista h synartisi list.reverse() ---> Exei allaxei pleon h list kai egine anapodi
  22.     initialList.extend(list)                    # Enwnei tin arxiki list (initialList) me tin allagmeni/reversed lista (list)
  23.     return initialList
  24.  
  25.  
  26. # * * * * M A I N * * * *
  27. # Ask the user for input
  28. n = int(input("Enter number n: "))
  29. #Create the list of all permutations
  30. Permutations = list(permutations(range(1, n+1)))
  31. print("All the permutations from 1 to " + str(n) + " is the following list: ")
  32. print(Permutations);                                                                 # Swsto ws edw
  33. print();
  34.  
  35.  
  36. # CASE 1
  37. if n ==1:
  38.     print("The shortest permutation has length " + str(factorial(n)))
  39.     print("This permutation is: 1");
  40. # CASE 2
  41. elif n == 2:
  42.     print("The shortest permutation has length " + str(factorial(n) + factorial(n-1)))
  43.     print("These permutations is 121 or 212")
  44. # CASE 3
  45. elif n == 3:
  46.     print("**** All the solutions for n = 3 are the following: :****")
  47.     for i in range(len(Permutations)):
  48.         print(str(i+1) + ") When tuple is: " + str(Permutations[i]) + ", solution is: ");
  49.         plusList = copyPartOfTuple(Permutations[i])          # [1,2,3] ---> [1,2,3,1,2]
  50.         result = reverseAndExtendList(plusList)              # To exw kanei symmetriko (tin plusList)
  51.         del result[ int(len(result)/2) ]                     # TELOS DIADIKASIAS ---> To result einai LISTA
  52.         print(result)
  53.         print()
  54.     print("*****************************************")
  55.     print("*****************************************")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement