Advertisement
makispaiktis

Superpermutations Full Solution

Apr 13th, 2019 (edited)
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 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. # 1) Beginning: Result = 1st element of Pemrutations
  8. result = ""
  9. flag = False
  10.  
  11. #print(str(Permutations[0][0]))
  12. for i in range(n):                                          # Scanning n times
  13.     result += str(Permutations[0][i])                       # Result = "123" for example
  14. del Permutations[0]
  15. print("1st step: ")
  16. print("Result: " + result)
  17. print("List: " + str(Permutations))
  18.  
  19.  
  20. # 2) Continue
  21. depth = n-1                                                  # In which depth are we searching for same strings
  22. # Loop: Ends when depth = 0 (when Permutations has no elements)
  23. while len(Permutations) > 0:
  24.  
  25.     if flag:
  26.         depth -= 1
  27.     else:
  28.         depth = n-1
  29.  
  30.     flag = True
  31.  
  32.  
  33.     for i in range (len(Permutations)):                  # Sarwnei olon ton pinaka (n! fores osa kai ta diafora tuples = permutations)
  34.         eystoxies = 0
  35.         for j in range (depth):                   # Sarwnei tin apantisi mou = result
  36.             if result[len(result)-1-j] == str(Permutations[i][depth-1-j]):   # !!!! (AAAA)
  37.                 eystoxies += 1
  38.  
  39.         if eystoxies == depth:                   # An oses fores epsaksa (depth), toses fores eixa epityxia se "taytisi string"
  40.             for k in range(n - depth):            # Iterations = n - depth: depth epityxies ---> prepei na balw n-depth grammata akoma
  41.                 result += str(Permutations[i][depth+k])       # !!!! (AAAA)
  42.  
  43.             del Permutations[i]
  44.             flag = False
  45.             break
  46.  
  47. print("Result: " + result)
  48. print("Length: " + str(len(result)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement