-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_array.py
139 lines (110 loc) · 4.02 KB
/
static_array.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
# Course: CS261 - Data Structures
# Description: Data structure for assignments
class StaticArrayException(Exception):
"""
Custom exception for Static Array class
DO NOT CHANGE THIS CLASS IN ANY WAY
"""
pass
class StaticArray:
"""
Implementation of Static Array Data Structure
Implemented methods: get(), set(), length()
DO NOT CHANGE THIS CLASS IN ANY WAY
YOU ARE ALLOWED TO CREATE AND USE OBJECTS OF THIS CLASS IN YOUR SOLUTION
"""
def __init__(self, size=10) -> None:
"""
Create array of given size
Initialize all elements with values of None
If requested size is not a positive number, raise StaticArray Exception
"""
if size < 1:
raise StaticArrayException('Array size must be a positive integer')
# The underscore denotes this as a private variable and
# private variables should not be accessed directly.
# Use the length() method to get the size of a StaticArray.
self._size = size
# Remember, this is a built-in list and is used here
# because Python doesn't have a fixed-size array type.
# Don't initialize variables like this in your assignments!
self._data = [None] * size
def __iter__(self) -> None:
"""
Disable iterator capability for StaticArray class
This means loops and aggregate functions like
those shown below won't work:
arr = StaticArray()
for value in arr: # will not work
min(arr) # will not work
max(arr) # will not work
sort(arr) # will not work
"""
return None
def __str__(self) -> str:
"""
Return content of static array in human-readable form
"""
out = "STAT_ARR Size: "
out += str(self._size)
out += " " + str(self._data)
return out
def get(self, index: int) -> object:
"""
Return value from given index position
Invalid index raises StaticArrayException
"""
if index < 0 or index >= self.length():
raise StaticArrayException('Index out of bounds')
return self._data[index]
def set(self, index: int, value: object) -> None:
"""
Store value at given index in the array
Invalid index raises StaticArrayException
"""
if index < 0 or index >= self.length():
raise StaticArrayException('Index out of bounds')
self._data[index] = value
def __getitem__(self, index: int):
"""
Same functionality as get() method above, but called differently
These snippets of code are equivalent:
arr = StaticArray()
arr.set(0, 'hello')
print(arr.get(0))
arr = StaticArray()
arr[0] = 'hello'
print(arr[0])
"""
return self.get(index)
def __setitem__(self, index: int, value: object) -> None:
"""
Same functionality as set() method above, but called differently
These snippets of code are equivalent:
arr = StaticArray()
arr.set(0, 'hello')
print(arr.get(0))
arr = StaticArray()
arr[0] = 'hello'
print(arr[0])
"""
self.set(index, value)
def length(self) -> int:
"""Return length of the array (number of elements)."""
return self._size
if __name__ == "__main__":
# Using the Static Array #
# create a new StaticArray object to store 5 elements
arr = StaticArray(5)
# set the value of each element equal to its index multiplied by 10
for index in range(5):
arr[index] = index * 10
# print the values of all elements in reverse order
for index in range(4, -1, -1):
print(arr[index])
# print the number of elements stored in the array
print(arr.length())
# Special consideration below #
# Don't do this! This creates a built-in Python list and if you use
# one you'll lose points.
forbidden_list = [None] * 10