def _add(self, item): """如果数组是否已满, 就先扩容, 然后添加元素到数组末尾 """ if len(self) == len(self.items): self.items = Array.increase_capacity(self.items) self.items[len(self)] = item
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 UNUSED and entry is not EMPTY : slot = self._findSlot( key, True ) self._table[slot] = entry self._count += 1
def _add(self, item): if len(self) == len(self.items): # 判断数组满了没有, 满了就先扩容 self.items = Array.increase_capacity(self.items) if self.isEmpty() or item >= self.items[len(self) - 1]: super()._add(item) else: for i, _item in enumerate(self): if item <= _item: targetIndex = i for i in range(len(self), targetIndex, -1): self.items[i] = self.items[i - 1] self.items[targetIndex] = item
def remove(self, item): """移除数组中第一个匹配的元素, 移除后有必要就缩容 """ if item not in self: raise KeyError(f'{item} not in bag.') for i, _item in enumerate(self): if _item == item: targetIndex = i break # 将 targetIndex及其后的元素左移 for i in range(targetIndex, len(self) - 1): self.items[i] = self.items[i+1] self.size -= 1 if len(self) < len(self.items) // 2: self.items = Array.decrease_capacity(self.items)
def remove(self, item): if item not in self: raise KeyError(f'{item} not in bag.') frontNode, targetNode = None, None for _item in self: if _item.data == item: targetNode = _item break else: frontNode = _item if frontNode is None: self.items = self.items.next else: frontNode.next = targetNode.next self.size -= 1 if len(self) < len(self.items) // 2: #如果有必要就缩容 self.items = Array.decrease_capacity(self.items)
from Arrays import Array arch = open("junio.dat", "rt") empleado = arch.readlines() empleado = [e.replace('', '').strip().split(',') for e in empleado] lista = len(empleado) empleados = Array(lista) for x in range(1, lista): empleados.set_item(empleado[x], x) print("SUELDOS DE EMPLEADOS") for x in range(1, lista): extra = float(276.5) hora_extra = int(empleados.get_item(x)[4]) año_ingreso = int(empleados.get_item(x)[6]) sueldo = int(empleados.get_item(x)[5]) sueldo_extra = sueldo + (hora_extra * extra) sueldo_prestacion = (2020 - año_ingreso) * 0.03 sueldo_total = float((sueldo * sueldo_prestacion) + sueldo_extra) print( f" N. empleado:{empleado[x][0]} Nombre:{empleado[x][1]} {empleado[x][2]} {empleado[x][3]} sueldo total de ${sueldo_total}" ) print("\nEMPLEADOS CON MENOR ANTIGUEDAD") for x in range(1, lista): if int(empleados.get_item(x)[6]) == 2020: print( f" N. empleado:{empleado[x][0]} Nombre: {empleado[x][1]} {empleado[x][2]} {empleado[x][3]}, Año de ingreso:{empleado[x][6]}" )
from Arrays import Array def rotation(rotateBy, myArray): for i in range(0, rotateBy): rotateOne(myArray) return myArray def rotateOne(myArray): for i in range(len(myArray) - 1): myArray[i], myArray[i + 1] = myArray[i + 1], myArray[i] if __name__ == '__main__': myArray = Array(10) for i in range(len(myArray)): myArray.insert(i, i) print('Before Rotation:', myArray) print('After Rotation:', rotation(3, myArray)) # OUTPUT: # Before Rotation: 0 1 2 3 4 5 6 7 8 9 # After Rotation: 3 4 5 6 7 8 9 0 1 2
# Author: Levon Khachatryan from Arrays import Array def findMissing(myArray, n): n = n - 1 totalSum = (n * (n + 1)) // 2 for i in range(0, n): totalSum -= myArray[i] return totalSum if __name__ == '__main__': myArray = Array(10) for i in range(len(myArray)): myArray.insert(i, i) myArray.delete(4, 4) print('Original Array:',myArray) print('Missing Element:', findMissing(myArray, len(myArray))) # OUTPUT: # Original Array: 0 1 2 3 5 6 7 8 9 0 # Missing Element: 4
def __init__(self, sourceCollection=None): """初始化函数. `items` 属性储存了栈的元素, 它是一个 `Array`的实例, 其第一个元素是栈底元素. """ self.items = Array(self.__class__.DEFAULT_CAPACITY) super().__init__(sourceCollection)
def clear(self): self.size = 0 self.items = Array(self.__class__.DEFAULT_CAPACITY)
from Arrays import Array algo = Array(10) print(algo.get_item(63)) algo.set_item(555, 3) print(algo.get_item(3)) print(f"el arreglo tiene {algo.get_length()} elementos") algo.clear(777) print(algo.get_item(3)) for x in algo: print(x) print("-------------------------------prueba iterador") for x in range(algo.get_length()): print(f"{x} -- {algo.get_item(x)}")
def __init__(self): self._table = Array(7) self._count = 0 self._maxCount = len(self._table) - len(self._table) // 3
def __init__(self, sourceCollection=None): self.items = Array(self.__class__.DEFAULT_CAPACITY) super().__init__(sourceCollection)