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 )
def __init__(self, rows, cols): self._rows = rows self._cols = cols # Store the rows. self._theRows = Array(self._rows) # Store the columns. self._theCols = Array(self._cols)
def radixSort(intList, numDigits): # Create an array 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 # Ones, tens, hundreds... # 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
def __init__(self, numAgents, numMinutes, betweenTime, serviceTime): # Parameters supplied by the user. self._arrivalProb = 1.0 / betweenTime self._serviceTime = serviceTime self._numMinutes = numMinutes # Simulation components. self._passengerQ = Queue() self._theAgents = Array(numAgents) for i in range(numAgents): self._theAgents[i] = TicketAgent(i + i) # Computed during the simulation. self._totalWaitTime = 0 self._numPassengers = 0
def _rehash( self ) : # Create a new larger table. origTable = self._hashTable newSize = len( self._hashTable ) * 2 + 1 self._hashTable = Array( newSize ) # Modify the size attributes. self._count = 0 self._loadFactor = newSize - newSize // 3 # Add the keys from the original array to the new table. for entry in origTable : if entry is not HashMap.UNUSED and entry is not HashMap.EMPTY : slot = self._findSlot( entry.key, True ) self._hashTable[ slot ] = entry self._count += 1
def append(self, item): # If the array is out of capacity, Create a new array, add the items in the old array # to the new one, add the "item" to the end of the new array and destroy the old array. # If the array is full, if len(self) >= self._theArrayCapacity: # Create a new one, newArray = Array(self._theArrayCapacity * 2) # Add the items in the old array to the new one. for element in range(len(self._theArray)): newArray[element] = self._theArray[element] # Destroy the old array. self._theArray = newArray # Update the array capacity. self._theArrayCapacity = len(self._theArray) # Then add the item to the end of the vector. self._theArray[len(self)] = item # If there is still space in the array, just add the item. else: self._theArray[len(self)] = item
def __init__( self, root, size ) : # Creates the array and fills it with the keys. self._theKeys = Array( size ) self._curItem = 0 # Keep track of the current location in the array. self._bstTraversal( root ) self._curItem = 0 # Reset the current item index.
def __init__(self, maxSize): self._elements = Array(maxSize) self._count = 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. return recMergeSort(theSeq, 0, n - 1, tmpArray)
def __init__( self ) : self._hashTable = Array( 7 ) self._count = 0 self._loadFactor = len( self._hashTable ) - len( self._hashTable ) // 3
def __init__(self, size=2): self._theArray = Array(size * 2) self._theArrayCapacity = len(self._theArray)
def __init__( self, numLevels ) : self._qSize = 0 self._qLevels = Array( numLevels ) for i in range( numLevels ) : self._qLevels[i] = Queue()
# The following simple program illustrates the creation and use of an array object # based on the Array ADT. # Fill a 1-D array with random values then print each value, one per line. from arrayADT import Array import random # The constructor is called to create the array. valueList = Array(100) # Fill the array with random floating-point values. for i in range(len(valueList)): valueList[i] = random.random() # Print the values one per line. for value in valueList: print(value)
def __init__( self, maxSize ) : self._theArray = Array( maxSize ) self._front = 0 self._back = maxSize - 1 self._count = 0