-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdicRefPython.py
More file actions
122 lines (92 loc) · 2.54 KB
/
Copy pathdicRefPython.py
File metadata and controls
122 lines (92 loc) · 2.54 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
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
class Foo:
def __init__(self, name):
self.name = name
"""
Testing whether an object gets stored as a reference or value in a python dictionary
"""
test_dic = {}
test_obj = Foo("Bar")
test_dic[1] = test_obj
test_dic[2] = test_obj
print(test_dic[1].name)
print(test_dic[2].name)
test_dic[2].name = "Bar2"
print(test_dic[2].name)
print(test_dic[1].name)
thing_sizes = [1, 2, 3, 4]
def get_iters(sizes, hand_size):
for size_x in range(sizes[1]):
for size_y in range(sizes[2]):
for size_z in range(sizes[3]):
if size_x+size_y+size_z != hand_size:
continue
yield size_x, size_y, size_z
for x, y, z in get_iters(thing_sizes, 4):
print(x, y, z)
# This ^ Outputs this:
# 0 1 3
# 0 2 2
# 1 0 3
# 1 1 2
# 1 2 1
""" We can use this as a start to dynamically determine the valid ways
one user can take from the subsets available to them.
"""
def recursive_iter(sizes, hand_size, idx, res):
if idx >= len(sizes) and hand_size != 0:
return []
if idx >= len(sizes) or hand_size == 0:
yield res
return res
for i in range(sizes[idx] + 1):
results = recursive_iter(sizes, hand_size - i, idx+1, res + [i])
if results:
yield from results
for res in recursive_iter(thing_sizes, 4, 0, []):
print(res)
"""This returns this:
[0, 0, 0, 4]
[0, 0, 1, 3]
[0, 0, 2, 2]
[0, 0, 3, 1]
[0, 1, 0, 3]
[0, 1, 1, 2]
[0, 1, 2, 1]
[0, 1, 3]
[0, 2, 0, 2]
[0, 2, 1, 1]
[0, 2, 2]
[1, 0, 0, 3]
[1, 0, 1, 2]
[1, 0, 2, 1]
[1, 0, 3]
[1, 1, 0, 2]
[1, 1, 1, 1]
[1, 1, 2]
[1, 2, 0, 1]
[1, 2, 1]
So for the list [1,2,3,4]
if the person has to pick 4, they can pick
0 from the first 3 and 4 from the last,
0 from the first two, 1 from the third and 3 from the last... etc.
This will help us generate all of the combinations for however many subsets.
In our dominos case, we are only interested in the second half, where
the person takes 1 from the first, because that's the only way everyone
else's subpartitons will be able to work.
[1, 0, 0, 3]
[1, 0, 1, 2]
[1, 0, 2, 1]
[1, 0, 3]
[1, 1, 0, 2]
[1, 1, 1, 1]
[1, 1, 2]
[1, 2, 0, 1]
[1, 2, 1]
So we want to update the code above to make it nicer/cleaner, but also only return
these. Also, want to make sure they're all the same size, so adding 0 to the three of size 3.
Having this will let us work with any number of players instead of a fixed 3.
"""
print("======== 8 person game")
eight_person_game = [5, 4, 3, 2, 3, 2, 4, 3, 2, 4, 3, 2, 4, 5]
for res in recursive_iter(eight_person_game, 7, 0, []):
print(res)