Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import permutations
- n = int(input("Enter n: "))
- Permutations = list(permutations(range(1,n+1))) # Permutations: list of tuples (each tuple contain a combination)
- print("Initial List: " + str(Permutations))
- # 1) Beginning: Result = 1st element of Pemrutations
- result = ""
- flag = False
- #print(str(Permutations[0][0]))
- for i in range(n): # Scanning n times
- result += str(Permutations[0][i]) # Result = "123" for example
- del Permutations[0]
- print("1st step: ")
- print("Result: " + result)
- print("List: " + str(Permutations))
- # 2) Continue
- depth = n-1 # In which depth are we searching for same strings
- # Loop: Ends when depth = 0 (when Permutations has no elements)
- while len(Permutations) > 0:
- if flag:
- depth -= 1
- else:
- depth = n-1
- flag = True
- for i in range (len(Permutations)): # Sarwnei olon ton pinaka (n! fores osa kai ta diafora tuples = permutations)
- eystoxies = 0
- for j in range (depth): # Sarwnei tin apantisi mou = result
- if result[len(result)-1-j] == str(Permutations[i][depth-1-j]): # !!!! (AAAA)
- eystoxies += 1
- if eystoxies == depth: # An oses fores epsaksa (depth), toses fores eixa epityxia se "taytisi string"
- for k in range(n - depth): # Iterations = n - depth: depth epityxies ---> prepei na balw n-depth grammata akoma
- result += str(Permutations[i][depth+k]) # !!!! (AAAA)
- del Permutations[i]
- flag = False
- break
- print("Result: " + result)
- print("Length: " + str(len(result)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement