Advertisement
makispaiktis

Superpermutations Paradotea

Apr 13th, 2019 (edited)
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.22 KB | None | 0 0
  1. from itertools import permutations
  2.  
  3. n = int(input("Enter n: "))
  4. Permutations = list(permutations(range(1,n+1)))             # Permutations: list of tuples (each tuple contain a combination)
  5. #print("Initial List: " + str(Permutations))
  6.  
  7.  
  8. from itertools import permutations
  9.  
  10. # 1st function (Paragontiko)
  11. def factorial(x):
  12.     if x== 0:
  13.         return 1;
  14.     else:
  15.         return x*factorial(x-1);
  16.  
  17. # 2nd function (Synartisi pou antigrafei olo to string/arithmo ektos tou teleutaiou grammatos)
  18. def copyPartOfTuple(myTuple):                    # Argument myTuple = Tuple like one of each unique combinations in Permutations
  19.     newList = list(myTuple)                      # Make typecast to the tuple ---> becomes list
  20.     size = len(newList)
  21.     newList += newList
  22.     del newList[2*size-1]                       # Delete to stoixeio se index = 2*size-1
  23.     return newList
  24.  
  25. # 3rd funtion (Synartisi pou sou anapodogyrizei to string kai sto kollaei sto arxiko ---> Symmetria)
  26. def reverseAndExtendList(list):
  27.     initialList = list.copy()                   # Copy the list to variable initialList
  28.     list.reverse()                              # EINAI VOID !!!! Den epistrefei lista h synartisi list.reverse() ---> Exei allaxei pleon h list kai egine anapodi
  29.     initialList.extend(list)                    # Enwnei tin arxiki list (initialList) me tin allagmeni/reversed lista (list)
  30.     return initialList
  31.  
  32.  
  33. '''
  34. # 4th funtion (Kanei n=4 swsta)
  35. def makeNCorrectChoices(n, list):          # Synithws nCorrect = 4
  36.    newList = []
  37.    for i in range(nCorrect):                                       # Kanw n loops
  38.        for j in range(nCorrect-1):                                 # Gia kathe loop koitw ta n-1 teleytaia grammata
  39.            newList.append(list[(len)list -1 -j])                   # kai ta vazw mesa se 1 alli lista
  40.        # H newList exei n-1=3 grammata k psaxnw poio leipei
  41. '''
  42.  
  43.  
  44.  
  45.  
  46. # * * * * M A I N * * * *
  47. print("All the permutations from 1 to " + str(n) + " is the following list: ")
  48. print(Permutations);                                                                 # Swsto ws edw
  49. print();
  50.  
  51.  
  52. # CASE 1
  53. if n ==1:
  54.     print("The shortest permutation has length " + str(factorial(n)))
  55.     print("This permutation is: 1");
  56.  
  57. # CASE 2
  58. elif n == 2:
  59.     print("The shortest permutation has length " + str(factorial(n) + factorial(n-1)))
  60.     print("Found 2 permutations/solutions is: 121 or 212")
  61.  
  62. # CASE 3
  63. elif n == 3:
  64.     print("**** All the solutions for n = 3 are the following: :****")
  65.     size = 0
  66.     for i in range(len(Permutations)):
  67.         print(str(i+1) + ") When tuple is: " + str(Permutations[i]) + ", solution is: ");
  68.         plusList = copyPartOfTuple(Permutations[i])          # [1,2,3] ---> [1,2,3,1,2]
  69.         result = reverseAndExtendList(plusList)              # To exw kanei symmetriko (tin plusList)
  70.         del result[ int(len(result)/2) ]                     # TELOS DIADIKASIAS ---> To result einai LISTA
  71.         size = len(result)
  72.         print(''.join(map(str, result)))
  73.         print()
  74.  
  75.     print("So,all of these results (found 6 solutions) have the minimum length which is: " + str(size))
  76.     print("*****************************************")
  77.     print("*****************************************")
  78.  
  79. # CASE 4
  80. elif n==4:
  81.     print("**** All the solutions for n = 4 are the following: :****")
  82.     for i in range(len(Permutations)):
  83.         print(str(i + 1) + ") When tuple is: " + str(Permutations[i]) + ", solution is: ");
  84.         size = 0
  85.         myList = copyPartOfTuple(Permutations[i])  # Make 4 correct
  86.         myList.append(Permutations[i][0])  # Stin for tha allaksei mono to 1o kai tha ginei i to 2o 0 meneiiiii
  87.         myList.append(myList[len(myList) - (n + 1)])
  88.         myList.append(myList[len(myList) - n])
  89.         myList.append(myList[len(myList) - n])
  90.         myList.append(myList[len(myList) - n])
  91.         myList.append(Permutations[i][1])
  92.         myList.append(myList[len(myList) - (n + 1)])
  93.         myList.append(myList[len(myList) - n])
  94.         myList.append(myList[len(myList) - n])
  95.         myList.append(myList[len(myList) - n])  # Swsto to miso akrivws string
  96.         result = reverseAndExtendList(myList)  # To exw kanei symmetriko (tin myList)
  97.         del result[int(len(result) / 2)]  # TELOS DIADIKASIAS ---> To result einai LISTA
  98.         size = len(result)
  99.         print(''.join(map(str, result)))
  100.         print()
  101.  
  102.     print("So,all of these results (found 24 solutions) have the minimum length which is: " + str(size))
  103.     print("*****************************************")
  104.     print("*****************************************")
  105.  
  106.  
  107. else:
  108.  
  109.     # 1) Beginning: Result = 1st element of Pemrutations
  110.     result = ""
  111.     flag = False
  112.  
  113.     #print(str(Permutations[0][0]))
  114.     for i in range(n):                                          # Scanning n times
  115.         result += str(Permutations[0][i])                       # Result = "123" for example
  116.     del Permutations[0]
  117.     print("Initial List: " + str(Permutations))
  118.  
  119.  
  120.     # 2) Continue
  121.     depth = n-1                                                  # In which depth are we searching for same strings
  122.     # Loop: Ends when depth = 0 (when Permutations has no elements)
  123.     while len(Permutations) > 0:
  124.  
  125.         if flag:
  126.             depth -= 1
  127.         else:
  128.             depth = n-1
  129.  
  130.         flag = True
  131.  
  132.  
  133.         for i in range (len(Permutations)):                  # Sarwnei olon ton pinaka (n! fores osa kai ta diafora tuples = permutations)
  134.             eystoxies = 0
  135.             for j in range (depth):                   # Sarwnei tin apantisi mou = result
  136.                 if result[len(result)-1-j] == str(Permutations[i][depth-1-j]):   # !!!! (AAAA)
  137.                     eystoxies += 1
  138.  
  139.             if eystoxies == depth:                   # An oses fores epsaksa (depth), toses fores eixa epityxia se "taytisi string"
  140.                 for k in range(n - depth):            # Iterations = n - depth: depth epityxies ---> prepei na balw n-depth grammata akoma
  141.                     result += str(Permutations[i][depth+k])       # !!!! (AAAA)
  142.  
  143.                 del Permutations[i]
  144.                 flag = False
  145.                 break
  146.  
  147.     print("Result: " + result)
  148.     print("Length: " + str(len(result)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement