-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdominos.py
More file actions
72 lines (53 loc) · 2.01 KB
/
dominos.py
File metadata and controls
72 lines (53 loc) · 2.01 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
__author__ = 'kathan'
class Domino(object):
MAX_DOT = 3
def __init__(self, first_half, second_half):
self.first_half = first_half
self.second_half = second_half
class DominoBag(object):
def __init__(self):
self.domino_list = []
def add(self, domino):
self.domino_list.append(domino)
# There should be dominos like [0 | 1] [1 | 2] [2 | 3] ...... [MAX_DOT - 1 | MAX_DOT]
def all_unique(self):
unique_domino_list = [False for i in range(Domino.MAX_DOT)]
for domino in self.domino_list:
if domino.first_half + 1 == domino.second_half or domino.second_half + 1 == domino.first_half:
unique_domino_list[min(domino.first_half, domino.second_half)] = True
if all(unique_domino_list):
return True
return False
# check if there is a loop in domino like [0 | 1] [1 | 2] [2 | 0]
def is_loop(self, base, current_list, parent=None):
if base in current_list:
return True
for domino in self.domino_list:
if domino == parent:
continue
if domino.first_half == base or domino.second_half == base:
other = domino.first_half if domino.second_half == base else domino.second_half
current_list.append(base)
result = self.is_loop(other, current_list, domino)
if result:
return result
return False
def is_loop_without_base(self):
return self.is_loop(self.domino_list[len(self.domino_list) - 1].first_half, [])
domino1 = Domino(0,1)
domino2 = Domino(2,3)
domino3 = Domino(1,2)
domino_bag = DominoBag()
domino_bag.add(domino1)
domino_bag.add(domino2)
domino_bag.add(domino3)
print domino_bag.all_unique()
domino4 = Domino(0,1)
domino5 = Domino(1,2)
domino6 = Domino(2,0)
domino_bag1 = DominoBag()
domino_bag1.add(domino4)
domino_bag1.add(domino5)
domino_bag1.add(domino6)
print domino_bag1.is_loop(0, [])
print domino_bag1.is_loop_without_base()