-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarray.py
executable file
·128 lines (104 loc) · 3.81 KB
/
marray.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
#!/usr/bin/python3
# -*- Coding: utf-8 -*-
# Author - Heera Hemanth Bylla < heerahemanth1@icloud.com >
import ctypes
class Array:
'''
Creates an array ADT with size elements
Array size is to be passed
'''
def __init__(self, size):
assert size>0, "Array size must be greater than 0."
self._size = size
# Creating the array in memory
PyArrayType = ctypes.py_object * size
self._elements = PyArrayType()
# Initialize each element
self.clear(None)
def __str__(self):
string = self.__class__.__name__ + ': ( '
for i in range(len(self)-1):
string += str(self._elements[i])+', '
string += str(self._elements[len(self)-1])+' )'
return string
# Returns the size of the array
def __len__(self):
return self._size
# Gets the contents of the index element
def __getitem__(self, index):
assert index>=0 and index<=self._size, "Array subscript out of range"
return self._elements[index]
# Puts the value in array at index position
def __setitem__(self, index, value):
assert index>=0 and index<=self._size, "Array subscript out of range"
self._elements[index] = value
# Clears the array by setting all elements to a given value
def clear(self, value):
for i in range(len(self)):
self._elements[i] = value
# Returns the array's iterator for traversing the elements
def __iter__(self):
return _ArrayIterator(self._elements)
# Iterator for the Array ADT
class _ArrayIterator:
'''
Iterator for the Array ADT
Array of elements passed as theArray
'''
def __init__(self, theArray):
self._arrayRef = theArray
self._curNdx = 0
def __iter__(self):
return self
def __next__(self):
if self._curNdx < len(self._arrayRef):
entry = self._arrayRef[self._curNdx]
self._curNdx += 1
return entry
else:
raise StopIteration
# Implementation of Array2D ADT using an array of arrays
class Array2D:
'''
2D Array implemented using Array of Arrays
'''
# Creates a 2D Array of size numRows x numCols
def __init__(self, numRows, numCols):
# Creates a 1D Array of length numRows
self._theRows = Array(numRows)
# Creates a 1D Array for each row of the 2D Array
for i in range(numRows):
self._theRows[i] = Array(numCols)
def __str__(self):
string = self.__class__.__name__+':\n'
for i in range(self.numRows()):
for j in range(self.numCols()-1):
string += str(self._theRows[i][j]) + ', '
string += str(self._theRows[i][self.numCols()-1]) + '\n'
return string
# Returns the number of rows
def numRows(self):
return len(self._theRows)
# Returns the number of columns
def numCols(self):
return len(self._theRows[0])
# Clears the Array by setting every element to a given value
def clear(self, value):
for i in range(self.numRows()):
self._theRows[i].clear(value)
# Gets the contents of the element at position [i, j]
def __getitem__(self, ndxTuple):
assert len(ndxTuple) == 2, "Invalid number of Array subscripts"
row, col = ndxTuple
return self._theRows[row][col]
# Sets the contents of the element at position [i, j] to value
def __setitem__(self, ndxTuple, value):
assert len(ndxTuple) == 2, "Invalid number of Array subscripts"
row, col = ndxTuple
assert row >= 0 and row <= self.numRows() and col >= 0 and col <= self.numCols(), \
"Array subscripts out of range"
self._theRows[row][col] = value
return
if __name__ == '__main__':
a = Array2D(3,2)
print(a.numCols())