-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCARTESIAN PROD.py
70 lines (42 loc) · 1.39 KB
/
CARTESIAN PROD.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
#!/usr/bin/env python3
def product(*iterables, repeat=3):
# product('ABCD', 'xy') → Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) → 000 001 010 011 100 101 110 111
if repeat < 0:
raise ValueError('repeat argument cannot be negative')
pools = [tuple(pool) for pool in iterables] * repeat
#print(pools) # [('A', 'B', 'C', 'D'), ('x', 'y'), ('A', 'B', 'C', 'D'), ('x', 'y'), ('A', 'B', 'C', 'D'), ('x', 'y')]
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
_1st = list( product('ABCD', 'xy') ) # python standard library form
print( _1st , len(_1st ) ) # 512 ---- 3 x 4 x 2 x 4 x 2 x 4 x 2
print()
print()
print()
print()
def product(*iterables, repeat=3):
pools = [tuple(pool) for pool in iterables] * repeat
#print(pools ,'here')
q = []
result = [[]]
cn= 0
for pool in pools:
q = []
for x in result :
if len( x ) == (repeat * len(iterables) ) : break
for y in pool :
cn += 1
#if len( x+[y] ) == (repeat * len(iterables) ) : print(x+[y] , cn) # 365 876
if len( x+[y] ) == (repeat * len(iterables) ) : print(x+[y] )
q.append( x+[y] )
result = q
return result # or q
_2nd = list( product('ABCD', 'xy') ) # my form
print( _2nd , len( _2nd ) )
"""
if one + len(pools[0]) == cn :
break
"""