-
Notifications
You must be signed in to change notification settings - Fork 1
/
Interval.py
executable file
·70 lines (54 loc) · 1.4 KB
/
Interval.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/python
#
# Defines a closed interval on the real line
#
# 06/06/09 JTO - Created
#
class Interval:
#
# Create a new Interval object
#
# low - Lower bound
# high - Upper bound
#
def __init__(self, low, high):
self.low = low
self.high = high
self.count = 0
#
# Create a string representation of an Interval object
#
def __repr__(self):
s = '[' + str(self.low) + ', ' + str(self.high) + ']'
s = s + ' n = ' + str(self.count)
return s
__str__ = __repr__
#
# Determine if a data item falls inside the interval and, if so,
# increment the interval's counter
#
# item - The data item
#
def add(self, item):
if item != None and item >= self.low and item <= self.high:
self.count = self.count + 1
#
# Determine if a data item falls inside the interval and, if so,
# decrement the interval's counter
#
# item - The data item
#
def remove(self, item):
if item != None and item >= self.low and item <= self.high:
self.count = self.count - 1
#
# Reset the count of the number of items that fell into the interval
# to zero
#
def clear(self):
self.count = 0
#
# Return the current count of data items that fell inside the interval
#
def getCount(self):
return self.count