-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOMBINATIONS(PRODUCT METHOD).py
185 lines (88 loc) · 4.25 KB
/
COMBINATIONS(PRODUCT METHOD).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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
def product(iterables,r, repeat=1):
r = len(iterables) + 1 - r
# product('ABCD', 'xy') → Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) → 000 001 010 011 100 101 110 111
M = [ [list(iterables[0+rr : r+rr])][0] for rr in range( len(l)-r+1 ) ] #### COMB**
#M = [ [iterables[i] for i in range(len(iterables) ) ] for _ in range( r ) ] #### COMB** WITH REPLACEMENT, PERMUT**
#pools = [tuple(pool) for pool in iterables] * repeat
pools = [tuple(pool) for pool in M] * repeat
result = [[]]
#vis = []
q=[]
for pool in pools :
result = [ x+[y] for x in result for y in pool if y not in x and x+[y] not in result ] # #### COMB**
q = []
for i in result:
if set(i) not in q :
q.append(set(i))
return q
#return result
l = [1,2,3,4,5,6,7,8,9]
r = 7 # 5 + 1 - r
# 5 1
#10 5
#10 10
#5 10
#1 5
#print( list(product( l , r) ) , len(list(product( l , r) ) ))
from itertools import combinations_with_replacement
from itertools import combinations
#print( list(combinations_with_replacement(l, r)), len(list(combinations_with_replacement(l, r))) )
print( list(product( l , r) ) , len(list(product( l , r) ) ) )
print()
print( list(combinations(l, r)), len(list(combinations(l, r))) )
#r = 4
#print()
#print( [ [list(range(0+rr, r+rr))][0] for rr in range( len(l)-r+1 ) ] )
#print([ [i for i in range(len(l) ) ] for _ in range( r ) ] )
#print(set([1,1,1,2,2,2,3,3,3]))
#print( list(combinations_with_replacement(l, r)), len( list(combinations_with_replacement(l, r)) ) )
#print()
"""
print( list(product( l , r) ) , len(list(product( l , r) ) ) )
q = []
for nn in range(1, len(l)):
for m in list(combinations(l, nn)) :
q.append(m)
print()
print()
print()
print(q, len(q))
############################################################ >>>>>>>>>>>>>>>>>>>
def do(l, r):
#mm =[ [ l[i-j:i+1] for i in range(len(l)-r, len(l)) ] for j in range(r) ]
iterables = l
mm = [[ [list(iterables[0+rr : r+rr]) ][0][vvv:] for rr in range( len(l)-r+1 ) ] for vvv in range(r-1,-1,-1) ]
mm_copy = mm
row = r - 1
path2 = []
#print(mm, )
def dfs(row, col, mm ) :
q = [(row, col, row, col) ]
while q :
row_ , col_ , nrow , ncol = q.pop()
news = mm[row_][col_][:len(mm[row_][col_])- len( mm[nrow][ncol] ) ] + mm[nrow][ncol]
#if mm[row][col][:len(mm[row][col])- len( news) ] + news not in path2 :
path2.append( mm[row][col][:len(mm[row][col])- len( news) ] + news ) # ORIGINAL TO CUMMULATIVE NEWS {row_ , col_} == {row , col}
#path2.append( mm[row][col][:len(mm[row][col])- len( mm[row_][col_] ) ] + mm[row_][col_] )
#path2.append( mm[row][col][:len(mm[row][col])- len( mm[nrow][ncol] ) ] + mm[nrow][ncol] )
#if nrow == 0 or ncol == len(mm[0])-1:
#print( path,"ans")
#print()
#print( path2,"PATH2" )
for ROW in range(1, len(mm)):
for COL in range(1, len(mm[0])):
new_row = nrow - ROW
new_col = ncol + COL
if 0 <= new_row < len(mm) and 0 <= new_col < len(mm[0]) :
#q.append( (row_, col_, new_row, new_col) ) # 1ST JOURNEY
q.append( ( nrow , ncol , new_row, new_col) ) # <<<<<<< (nrow , ncol ) ORIGINAL to ALL 1ST {NON_CUMULATIVE} new options
for col in range(len(mm[0])):
#print(mm[row][col])
dfs(row, col, mm )
print( path2, len(path2))
#l = [1,2,3,4,5]
#r = 3
do(l, r)
############################################################ >>>>>>>>>>>>>>>>>>>
"""