-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_list.py
70 lines (56 loc) · 2.06 KB
/
abstract_list.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
""" List ADT. Defines a generic abstract list with the standard methods. """
from abc import ABC, abstractmethod
from typing import TypeVar, Generic
T = TypeVar('T')
__author__ = 'Maria Garcia de la Banda and Brendon Taylor. Modified by Alexey Ignatiev'
__docformat__ = 'reStructuredText'
class List(ABC, Generic[T]):
""" Abstract class for a generic List. """
def __init__(self) -> None:
""" Basic List object initialiser. """
self.length = 0
@abstractmethod
def __getitem__(self, index: int) -> T:
""" Magic method. Return the element at a given position. """
pass
@abstractmethod
def __setitem__(self, index: int, item: T) -> None:
""" Magic method. Insert the item at a given position. """
pass
def __len__(self) -> int:
""" Return the size of the list. """
return self.length
def __str__(self):
""" Magic method constructing a string representation of the list object. """
result = '['
for i in range(len(self)):
if i > 0:
result += ', '
result += str(self[i]) if type(self[i]) != str else "'{0}'".format(self[i])
result += ']'
return result
def append(self, item: T) -> None:
""" Append a new item to the end of the list. """
self.insert(len(self), item)
@abstractmethod
def insert(self, index: int, item: T) -> None:
""" Insert an item at a given position. """
pass
def remove(self, item: T) -> None:
""" Remove an item from the list. """
index = self.index(item)
self.delete_at_index(index)
@abstractmethod
def delete_at_index(self, index: int) -> T:
""" Delete item at a given position. """
pass
@abstractmethod
def index(self, item: T) -> int:
""" Find the position of a given item in the list. """
pass
def is_empty(self) -> bool:
""" Check if the list of empty. """
return len(self) == 0
def clear(self):
""" Clear the list. """
self.length = 0