def __init__(self, *dimensions):
        assert len(dimensions) > 1, 'The array must have 2 or more dimensions.'
        # The variable argument tuple contaims the dim sizes.
        self._dims = dimensions
        # Compute the total number of elements in the array.
        size = 1
        for d in dimensions:
            assert d > 0, 'Demensions must be > 0.'
            size *= d

        # Create the 1-D array 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()
Beispiel #2
0
def radixSort(intList, numDigits):
	# Create an arrry of queues to represent the bins.
	binArray = Array(10)
	for k in range(10):
		binArray[k] = Queue()
		
	# The value of the current column.
	column = 1
	
	# Iterate over the number of digits in the largest value.
	for d in range(numDigits):
		
		# Distribute the keys across the 10 bins.
		for key in intList:
			digit = (key // column) % 10
			binArray[digit].enqueue(key)
			
		# Gather the keys from the bins and place them back in intList.
		i = 0
		for bin in binArray:
			while not bin.isEmpty():
				intList[i] = bin.dequeue()
				i += 1
				
		# Advance to the next column value.
		column *= 10
	return intList
Beispiel #3
0
def mergeSort(theSeq):
    n = len(theSeq)
    # Create a temporary array for use when merging subsequences.
    tmpArray = Array(n)
    # Call the private recursive merge sort function.
    recMergeSort(theSeq, 0, n - 1, tmpArray)
    return theSeq
	def __init__(self, *dimensions):
		assert len(dimensions) > 1, 'The array must have 2 or more dimensions.'
		# The variable argument tuple contaims the dim sizes.
		self._dims = dimensions
		# Compute the total number of elements in the array.
		size = 1
		for d in dimensions:
			assert d > 0, 'Demensions must be > 0.'
			size *= d
		
		# Create the 1-D array 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()
	def _rehash(self):
		# Create a new larger table.
		origTable = self._table
		newSize = len(self._table) * 2 + 1
		self._table = Array(newSize)
		
		# Modify the size attributes.
		self._count = 0
		self._maxCount = newSize - newSize // 3
		
		# Add the keys from the original array to the new table.
		for entry in origTable:
			if entry is not self.UNUSED and entry is not self.EMPTY:
				slot = self._findSlot(entry.key, True)
				self._table[slot] = entry
				self._count += 1
class MultiArray(object):
	# Creates a multi-dimentional array.
	def __init__(self, *dimensions):
		assert len(dimensions) > 1, 'The array must have 2 or more dimensions.'
		# The variable argument tuple contaims the dim sizes.
		self._dims = dimensions
		# Compute the total number of elements in the array.
		size = 1
		for d in dimensions:
			assert d > 0, 'Demensions must be > 0.'
			size *= d
		
		# Create the 1-D array 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 dimention.
	def length(self, dim):
		assert dim >=1 and dim <= len(self._dims), \
			'Dimention 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 imdex components are within the legal range.
			if idx[j] < 0 or 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):
		for j in range(len(self._factors)):
			self._factors[j] = 1
			if j != len(self._factors) - 1:
				for i in range(j + 1, len(self._factors)):
					self._factors[j] *= self._dims[i]
class MultiArray(object):
    # Creates a multi-dimentional array.
    def __init__(self, *dimensions):
        assert len(dimensions) > 1, 'The array must have 2 or more dimensions.'
        # The variable argument tuple contaims the dim sizes.
        self._dims = dimensions
        # Compute the total number of elements in the array.
        size = 1
        for d in dimensions:
            assert d > 0, 'Demensions must be > 0.'
            size *= d

        # Create the 1-D array 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 dimention.
    def length(self, dim):
        assert dim >=1 and dim <= len(self._dims), \
         'Dimention 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 imdex components are within the legal range.
            if idx[j] < 0 or 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):
        for j in range(len(self._factors)):
            self._factors[j] = 1
            if j != len(self._factors) - 1:
                for i in range(j + 1, len(self._factors)):
                    self._factors[j] *= self._dims[i]
 def __init__(self, numLevels):
     self._qSize = 0
     self._qLevels = Array(numLevels)
     for i in range(numLevels):
         self._qLevels[i] = Queue()
Beispiel #9
0
 def __init__(self, maxSize):
     self._elements = Array(maxSize)
     self._count = 0
Beispiel #10
0
	def __init__(self):
		self.UNUSED = None
		self.EMPTY = _MapEntry(None, None)
		self._table = Array(7)
		self._count = 0
		self._maxCount = len(self._table) - len(self._table) // 3
	def __init__(self, maxSize):
		self._count = 0
		self._front = 0
		self._back = maxSize - 1
		self._qArray = Array(maxSize)
Beispiel #12
0
	def __init__(self, numRows, numCols):
		self._numCols = numCols
		self._listOfRows = Array(numRows)
Beispiel #13
0
# Count the number of occurrences of each letter in a text file.

from my_own_array import Array

# Create an array for the counters and initialize each element to 0.
theCounters = Array(127)
theCounters.clear(0)

# Open the text file for reading and extract each line from the file
# and iterate over each character in the line.
with open('atextfile.txt', 'r') as theFile:
	content = theFile.read()
for letter in content:
	code = ord(letter)
	theCounters[code] += 1

# Print the result. The uppercase letters have ASCII values in the 
# range 65...90 and the lowercase letters are in the range 97...122.
for i in range(26):
	print('%c - %4d			%c - %4d' % \
		(chr(65+i), theCounters[65+i], chr(97+i), theCounters[97+i]))