-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdomain.py
More file actions
71 lines (59 loc) · 1.88 KB
/
domain.py
File metadata and controls
71 lines (59 loc) · 1.88 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
class Domain:
def __init__(self, value_or_values):
"""
Represents the domain of a variable, i.e. the possible values that each
variable may assign.
"""
self.values = []
if type(value_or_values) is int:
self.values.append(value_or_values)
else: # type list
self.values = value_or_values
self.modified = False
######### Constructors/Modified Method #########
def copy(self, values):
self.values = values
def add(self, num):
self.values.append(num)
def remove(self, num):
if num in self.values:
self.modified = True
self.values.remove(num)
return True
else:
return False
######### Accessors Method #########
def contains(self, v):
"""
Checks if a value exists within the domain
@param value to check
@return true if <tt>value</tt> exists within the domain, false otherwise.
"""
return v in self.values
def size(self):
return len(self.values)
def isEmpty(self):
""" return true if no values are contained in the domain. """
return not self.values
def isModified(self):
"""
Returns whether or not the domain has been modified.
return true if the domain has been modified.
"""
return self.modified
######### String Representation #########
def __str__(self):
"""
String Representation method to print Domain values encapsulated
inside {}
"""
output = "{"
for i in range(len(self.values) - 1):
output += str(self.values[i]) + ", "
try:
output += str(self.values[-1])
except:
pass
output += "}"
return output
# return str(self.values)