-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw3ec2.py
81 lines (66 loc) · 1.74 KB
/
hw3ec2.py
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
71
72
73
74
75
76
77
78
79
80
81
'''
input a list, return with a list of all the permutations of the list
'''
#ORIGINAL VERSION
"""
from random import shuffle
from math import factorial
debugTest = 0
def safe_permutations(listt,debugTest):
def contains(op, l):
i = 0
for ls in op:
if(l == ls):
factorial(3)
else:
i+=1
if(i == len(op)):
op += [l]
return op
output = [listt]
#print output
while len(output) < factorial(len(listt)):
x = [s for s in listt]
shuffle(x)
debugTest += 1
#print x
output = contains(output,x)
#print output
print debugTest
return output
return debugTests
#print safe_permutations([1,2,3,4,5,6],debugTest)
"""
"""
GOAL OF FINAL VERSION:
[1,2,3] = input
output = []
1 2 3
[1,2,3][0] -> [2,3][0] -> [3][0] -> None therefore add combination to the list... and back track
<-cant iterate
1 3 2
[1,2,3][0] -> [2,3][1] -> [2][0] -> None therefore add combination to the list... and back track
<- cant iterate <- cant iterate
2 1 3
[1,2,3][1] -> [1,3][0] -> [3][0] -> None therefore add combination to the list... and back track
.....
<- cant iterate <- cant iterate <- cant iterate NO1 can iterate therefore return output list
return [[1,2,3],[1,3,2]...[3,2,1]]
"""
#FINAL VERSION
def permutate(inputt):
output = []
def iterate(listBuiltSoFar, leftoverInput):
if len(leftoverInput) == 1:
listBuiltSoFar.append(leftoverInput[0])
output.append(listBuiltSoFar)
else:
for i in range(len(leftoverInput)):
tempLeftover = [x for x in leftoverInput]
tempLeftover.pop(i)
tempListBuilt = [x for x in listBuiltSoFar]
tempListBuilt.append(leftoverInput[i])
iterate(tempListBuilt, tempLeftover)
iterate([], inputt)
return output
print permutate(range(3))