-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabstractlist.py
45 lines (37 loc) · 1.39 KB
/
abstractlist.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
from abstractcollection import AbstractCollection
class AbstractList(AbstractCollection):
"""An abstract list implementation."""
# Constructor
def __init__(self, sourceCollection=None):
"""Maintains a count of modifications to the list."""
self._modCount = 0
AbstractCollection.__init__(self, sourceCollection)
# Accessor methods
def getModCount(self):
"""Returns the count of modifications to the list."""
return self._modCount
def index(self, item):
"""Precondition: item is in the list.
Returns the postition of item.
Raises: ValueError if the item is not in the list."""
position = 0
for data in self:
if data == item:
return position
else:
position += 1
if position == len(self):
raise ValueError(str(item) + " not in list.")
# Mutator methods
def incModCount(self):
"""Increases the count of modifications to the list."""
self._modCount += 1
def add(self, item):
"""Adds the item to the end of the list."""
self.insert(len(str), item)
def remove(self, item):
"""Precondition: item is in the list.
Raises: ValueError if item is not in the list.
Postcondition: item is removed from the list."""
position = index(item)
self.pop(position)