-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_sorted_list.py
71 lines (58 loc) · 2.1 KB
/
abstract_sorted_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
71
"""
SortedList ADT.
Defines a generic abstract sorted 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 SortedList(ABC, Generic[T]):
""" Abstract class for a generic Set. """
def __init__(self) -> None:
""" Basic SortedList 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, value: T) -> None:
""" Magic method. Insert the item at a given position,
if possible (!). Shift the following elements to the right.
"""
pass
def __len__(self) -> int:
""" Return the size of the list. """
return self.length
def __str__(self) -> str:
""" 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
@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 remove(self, item: T) -> None:
""" Remove an item from the list. """
index = self.index(item)
self.delete_at_index(index)
def is_empty(self) -> bool:
""" Check if the list of empty. """
return len(self) == 0
def clear(self) -> None:
""" Clear the list. """
self.length = 0
@abstractmethod
def add(self, item: T) -> None:
""" Add new element to the list. """
pass