-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharrays.py
144 lines (119 loc) · 4.77 KB
/
arrays.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
File: arrays.py
An Array is a restricted list whose clients can use
only [], len, iter, and str.
To instantiate, use
<variable> = array(<capacity>, <optional fill value>)
The fill value is None by default.
"""
class Array(object):
"""Represents an array."""
def __init__(self, capacity, fillValue = None):
"""Capacity is the static size of the array.
fillValue is placed at each position."""
self._items = list()
for count in range(capacity):
self._items.append(fillValue)
def __len__(self):
"""-> The capacity of the array."""
return len(self._items)
def __str__(self):
"""-> The string representation of the array."""
return str(self._items)
def __iter__(self):
"""Supports iteration over a view of an array."""
return iter(self._items)
def __getitem__(self, index):
"""Subscript operator for access at index."""
return self._items[index]
def __setitem__(self, index, newItem):
"""Subscript operator for replacement at index."""
self._items[index] = newItem
class ArrayExpanded(object):
"""Represents an array."""
def __init__(self, capacity, fillValue = None):
"""Capacity is the static size of the array.
fillValue is placed at each position."""
self._items = list()
self._logicalSize = 0
# Track the capacity and fill value for adjustments later
self._capacity = capacity
self._fillValue = fillValue
for count in range(capacity):
self._items.append(fillValue)
def __len__(self):
"""-> The capacity of the array."""
return len(self._items)
def __str__(self):
"""-> The string representation of the array."""
return str(self._items)
def __iter__(self):
"""Supports traversal with a for loop."""
return iter(self._items)
def __getitem__(self, index):
"""Subscript operator for access at index.
Precondition: 0 <= index < size()"""
#if index < 0 or index >= self.size():
# raise IndexError("Array index out of bounds")
return self._items[index]
def __setitem__(self, index, newItem):
"""Subscript operator for replacement at index.
Precondition: 0 <= index < size()"""
#if index < 0 or index >= self.size():
# raise IndexError("Array index out of bounds")
self._items[index] = newItem
def size(self):
"""-> The number of items in the array."""
return self._logicalSize
def grow(self):
"""Increases the physical size of the array if necessary."""
# Double the physical size if no more room for items
# and add the fillValue to the new cells in the underlying list
for count in range(len(self)):
self._items.append(self._fillValue)
def shrink(self):
"""Decreases the physical size of the array if necessary."""
# Shrink the size by half but not below the default capacity
# and remove those garbage cells from the underlying list
newSize = max(self._capacity, len(self) // 2)
for count in range(len(self) - newSize):
self._items.pop()
def insert(self, index, newItem):
"""Inserts item at index in the array."""
if self.size() == len(self):
self.grow()
if index >= self.size():
self._items[self.size()] = newItem
else:
index = max(index, 0)
# Shift items down by one position
for i in range(self.size(), index, -1):
self._items[i] = self._items[i - 1]
# Add new item and increment logical size
self._items[index] = newItem
self._logicalSize += 1
def pop(self, index):
"""Removes and returns item at index in the array.
Precondition: 0 <= index < size()"""
if index < 0 or index >= self.size():
raise IndexError("Array index out of bounds")
itemToReturn = self._items[index]
# Shift items up by one position
for i in range(index, self.size() - 1):
self._items[i] = self._items[i + 1]
# Reset empty slot to fill value
self._items[self.size() - 1] = self._fillValue
self._logicalSize -= 1
if self.size() <= len(self) // 4 and len(self) > self._capacity:
self.shrink()
return itemToReturn
def __eq__(self, other):
"""Returns True if the two arrays are equal or
False otherwise."""
if self is other: return True
if type(self) != type(other): return False
if self.size() != other.size(): return False
for index in range(self.size()):
if self[index] != other[index]:
return False
return True