Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
array.py 5.83 KB
Copy Edit Raw Blame History
4.9527 authored 2019年12月04日 10:41 +08:00 . read chapter 9
# Implements the Array ADT using array capabilities of the ctypes module.
import ctypes
class Array :
# Creates an array with size elements.
def __init__( self, size ):
assert size > 0, "Array size must be > 0"
self._size = size
# Create the array structure using the ctypes module.
PyArrayType = ctypes.py_object * size
self._elements = PyArrayType()
# Initialize each element.
self.clear( None )
# 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 < len(self), "Array subscript out of range"
return self._elements[ index ]
# Puts the value in the array element at index position.
def __setitem__( self, index, value ):
assert index >= 0 and index < len(self), "Array subscript out of range"
self._elements[ index ] = value
# Clears the array by setting each element to the 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 )
# An iterator for the Array ADT.
class _ArrayIterator :
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 the Array2D ADT using an array of arrays.
class Array2D :
# Creates a 2-D array of size numRows x numCols.
def __init__( self, numRows, numCols ):
# Create a 1-D array to store an array reference for each row.
self._theRows = Array( numRows )
# Create the 1-D arrays for each row of the 2-D array.
for i in range( numRows ) :
self._theRows[i] = Array( numCols )
# Returns the number of rows in the 2-D array.
def numRows( self ):
return len( self._theRows )
# Returns the number of columns in the 2-D array.
def numCols( self ):
return len( self._theRows[0] )
# Clears the array by setting every element to the given value.
def clear( self, value ):
for row in range( self.numRows() ):
row.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 = ndxTuple[0]
col = ndxTuple[1]
assert row >= 0 and row < self.numRows() \
and col >= 0 and col < self.numCols(), \
"Array subscript out of range."
the1dArray = self._theRows[row]
return the1dArray[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 = ndxTuple[0]
col = ndxTuple[1]
assert row >= 0 and row < self.numRows() \
and col >= 0 and col < self.numCols(), \
"Array subscript out of range."
the1dArray = self._theRows[row]
the1dArray[col] = value
# Implementation of the MultiArray ADT using a 1-D array.
class MultiArray :
# Creates a multi-dimensional array.
def __init__( self, *dimensions ):
assert len(dimensions) > 1, "The array must have 2 or more dimensions."
# The variable argument tuple contains the dim sizes.
self._dims = dimensions
# Compute the total number of elements in the array.
size = 1
for d in dimensions :
assert d > 0, "Dimensions must be > 0."
size *= d
# Create the 1-D array_type to store the elements.
self._elements = Array( size )
# Create a 1-D array to store the equation factors.
self._factors = Array( len(dimensions) )
self._computeFactors()
# Returns the number of dimensions in the array.
def numDims( self ):
return len(self._dims)
# Returns the length of the given dimension.
def length( self, dim ):
assert dim >= 1 and dim < len(self._dims),\
"Dimension component out of range."
return self._dims[dim - 1]
# Clears the array by setting all elements to the given value.
def clear( self, value ):
self._elements.clear( value )
# Returns the contents of element (i_1, i_2, ..., i_n).
def __getitem__( self, ndxTuple ):
assert len(ndxTuple) == self.numDims(), "Invalid # of array subscripts."
index = self._computeIndex( ndxTuple )
assert index is not None, "Array subscript out of range."
return self._elements[index]
# Sets the contents of element (i_1, i_2, ..., i_n).
def __setitem__( self, ndxTuple, value ):
assert len(ndxTuple) == self.numDims(), "Invalid # of array subscripts."
index = self._computeIndex( ndxTuple )
assert index is not None, "Array subscript out of range."
self._elements[index] = value
# Computes the 1-D array offset for element (i_1, i_2, ... i_n)
# using the equation i_1 * f_1 + i_2 * f_2 + ... + i_n * f_n
def _computeIndex( self, idx ):
offset = 0
for j in range( len(idx) ):
# Make sure the index components are within the legal range.
if idx[j] < 0 || idx[j] >= self._dims[j] :
return None
else : # sum the product of i_j * f_j.
offset += idx[j] * self._factors[j]
return offset
# Computes the factor values used in the index equation.
def _computeFactors( self ):
pass
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/nicolas4d/Data-Structures-and-Algorithms-Using-Python.git
git@gitee.com:nicolas4d/Data-Structures-and-Algorithms-Using-Python.git
nicolas4d
Data-Structures-and-Algorithms-Using-Python
Data-Structures-and-Algorithms-Using-Python
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /