Advertisement
makispaiktis

Superpermutations until n=4

Apr 10th, 2019 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.50 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 olo to string/arithmo ektos tou teleutaiou grammatos)
  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. '''
  27. # 4th funtion (Kanei n=4 swsta)
  28. def makeNCorrectChoices(n, list):          # Synithws nCorrect = 4
  29.    newList = []
  30.    for i in range(nCorrect):                                       # Kanw n loops
  31.        for j in range(nCorrect-1):                                 # Gia kathe loop koitw ta n-1 teleytaia grammata
  32.            newList.append(list[(len)list -1 -j])                   # kai ta vazw mesa se 1 alli lista
  33.        # H newList exei n-1=3 grammata k psaxnw poio leipei
  34. '''
  35.  
  36.  
  37.  
  38.  
  39. # * * * * M A I N * * * *
  40. # Ask the user for input
  41. n = int(input("Enter number n: "))
  42. #Create the list of all permutations
  43. Permutations = list(permutations(range(1, n+1)))
  44. print("All the permutations from 1 to " + str(n) + " is the following list: ")
  45. print(Permutations);                                                                 # Swsto ws edw
  46. print();
  47.  
  48.  
  49. # CASE 1
  50. if n ==1:
  51.     print("The shortest permutation has length " + str(factorial(n)))
  52.     print("This permutation is: 1");
  53.  
  54. # CASE 2
  55. elif n == 2:
  56.     print("The shortest permutation has length " + str(factorial(n) + factorial(n-1)))
  57.     print("Found 2 permutations/solutions is: 121 or 212")
  58.  
  59. # CASE 3
  60. elif n == 3:
  61.     print("**** All the solutions for n = 3 are the following: :****")
  62.     size = 0
  63.     for i in range(len(Permutations)):
  64.         print(str(i+1) + ") When tuple is: " + str(Permutations[i]) + ", solution is: ");
  65.         plusList = copyPartOfTuple(Permutations[i])          # [1,2,3] ---> [1,2,3,1,2]
  66.         result = reverseAndExtendList(plusList)              # To exw kanei symmetriko (tin plusList)
  67.         del result[ int(len(result)/2) ]                     # TELOS DIADIKASIAS ---> To result einai LISTA
  68.         size = len(result)
  69.         print(''.join(map(str, result)))
  70.         print()
  71.  
  72.     print("So,all of these results (found 6 solutions) have the minimum length which is: " + str(size))
  73.     print("*****************************************")
  74.     print("*****************************************")
  75.  
  76. # CASE 4
  77. elif n==4:
  78.     print("**** All the solutions for n = 4 are the following: :****")
  79.     for i in range(len(Permutations)):
  80.         print(str(i + 1) + ") When tuple is: " + str(Permutations[i]) + ", solution is: ");
  81.         size = 0
  82.         myList = copyPartOfTuple(Permutations[i])  # Make 4 correct
  83.         myList.append(Permutations[i][0])  # Stin for tha allaksei mono to 1o kai tha ginei i to 2o 0 meneiiiii
  84.         myList.append(myList[len(myList) - (n + 1)])
  85.         myList.append(myList[len(myList) - n])
  86.         myList.append(myList[len(myList) - n])
  87.         myList.append(myList[len(myList) - n])
  88.         myList.append(Permutations[i][1])
  89.         myList.append(myList[len(myList) - (n + 1)])
  90.         myList.append(myList[len(myList) - n])
  91.         myList.append(myList[len(myList) - n])
  92.         myList.append(myList[len(myList) - n])  # Swsto to miso akrivws string
  93.         result = reverseAndExtendList(myList)  # To exw kanei symmetriko (tin myList)
  94.         del result[int(len(result) / 2)]  # TELOS DIADIKASIAS ---> To result einai LISTA
  95.         size = len(result)
  96.         print(''.join(map(str, result)))
  97.         print()
  98.  
  99.     print("So,all of these results (found 24 solutions) have the minimum length which is: " + str(size))
  100.     print("*****************************************")
  101.     print("*****************************************")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement