-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence.py
37 lines (29 loc) · 1.24 KB
/
sequence.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
"""Demonstration of abstract classes via template method pattern."""
from abc import ABCMeta, abstractmethod
class Sequence(metaclass=ABCMeta):
"""Our own version of ``collections.Sequence`` abstract base class."""
@abstractmethod
def __len__(self):
"""Return the length of the sequence."""
@abstractmethod
def __getitem__(self, j):
"""Return the element at index j of the sequence."""
def __contains__(self, val):
"""Return ``True`` if val found in the sequence; ``False`` otherwise."""
for j in range(len(self)):
if self[j] == val: # Found match.
return True
return False
def index(self, val):
"""Return leftmost index at which val is found (or rasie ``ValueError``)."""
for j in range(len(self)):
if self[j] == val: # Leftmost match.
return j
raise ValueError("Value not in sequence") # Never found a match.
def count(self, val):
"""Return the number of elements equal to the given value."""
k = 0
for j in range(len(self)):
if self[j] == val:
k += 1 # Found a match.
return k