Repository navigation
Expand file tree
/
Copy pathperm_lib.py
More file actions
70 lines (57 loc) · 1.65 KB
/
Copy pathperm_lib.py
File metadata and controls
70 lines (57 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Python function to print permutations of a given list
# Ale: https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list/104436#104436
import itertools
# list(itertools.permutations([1, 2, 3])
def analyse(next_line):
willi = next_line.split(",")
print(willi)
return(willi)
def permutation(lst):
# If lst is empty then there are no permutations
if len(lst) == 0:
return []
# If there is only one element in lst then, only
# one permuatation is possible
if len(lst) == 1:
return [lst]
# Find the permutations for lst if there are
# more than 1 characters
l = [] # empty list that will store current permutation
# Iterate the input(lst) and calculate the permutation
for i in range(len(lst)):
m = lst[i]
# Extract lst[i] or m from the list. remLst is
# remaining list
remLst = lst[:i] + lst[i+1:]
# Generating all permutations where m is first
# element
for p in permutation(remLst):
l.append([m] + p)
return l
# Function to convert a list to string
def listToString(s):
# initialize an empty string
str1 = ""
# traverse in the string
for ele in s:
str1 += ele
# return string
return str1
'''
for lwords in range(len(data),1,-1):
print(lwords,"-letter words")
print(len(data),"-letter words")
all_permutations = set()
count = 0
for p in itertools.permutations(data):
#for p in permutation(data):
count +=1
strg=listToString(p)
all_permutations.update({strg[0:lwords]})
#print(all_permutations)
all_permutations = sorted(all_permutations)
for word in all_permutations:
print(word)
print(len(all_permutations)," of ",count)
print("Permutations: ",count)
'''