def test_remove_element_in_arr(self): a = Ar(5) for i in range(5): a.add_element(i, i) a.remove_element(2) print(a._data) assert a._data == [0, 1, 3, 4, None]
def test_find_elements_index_in_arr(self): a = Ar(20) for i in range(20): a.add_element(i, i) res = a.find_elements_index_in_arr(19) assert res == 19
class ArrayStack(StackInterface): def __init__(self, capacity=0): # 动态数组对象, 容量默认为10,有10个空间的顺序栈, # 用户可自定义栈的大小 self.__data = Array(capacity) # self.__size = 0 不需要维护size,下面用不到 # 获取栈内元素个数 def getSize(self): return self.__data.getSize() # 判断栈是否为空 def isEmpty(self): return self.__data.isEmpty() # 入栈 def push(self, e): self.__data.addLast(e) # 出栈 def pop(self): return self.__data.deleteLast() # 取栈顶元素 def peek(self): return self.__data.getLast() def __str__(self): return "ArrayStack:{}, size = {}".format(self.__data, self.__data.getSize()) def __repr__(self): return self.__str__()
class ArrayQueue(QueueInterface): def __init__(self, capacity=0): # 动态数组对象, 容量默认为10,有10个空间的顺序队列, # 用户可自定义队列的大小 self.__data = Array(capacity) # 获取队列内元素个数 def getSize(self): return self.__data.getSize() # 判断队列是否为空 def isEmpty(self): return self.__data.isEmpty() # 入队 def enqueue(self, e): self.__data.addLast(e) #出队 def dequeue(self): return self.__data.deleteFirst() # 取队首元素 def getFront(self): return self.__data.getFirst() def __str__(self): return "ArrayQueue: {}".format(self.__data) def __repr__(self): return self.__str__()
def __init__(self, capacity=0): # 动态数组对象, 容量默认为10,有10个空间的顺序栈, # 用户可自定义栈的大小 self.__data = Array(capacity)
def generate_random_array(n, range_L, range_R): random.seed(time()) array = Array() for i in range(0, n, 1): array.addLast(randint(range_L, range_R)) return array
from Array.Array import Array i = 5 arr = Array(i) arr.__setitem__(0, 5) arr.__setitem__(1, 4) arr.__setitem__(2, 3) arr.__setitem__(3, 2) arr.__setitem__(4, 1) for i in range(len(arr)): print(arr[i], end=" ")
def test_print_arr(self): a = Ar(5) for i in range(5): a.add_element(i, i) print(a)
def test_remove_first_element(self): a = Ar(20) for i in range(20): a.add_element(i, i) a.remove_last_element() assert a.get_element(18) == 18
def test_remove_element_by_index_in_arr(self): a = Ar(20) for i in range(20): a.add_element(i, i) a.remove_element_by_index(5) assert a.get_element(5) == 6
def test_judge_element_in_arr(self): a = Ar(20) for i in range(20): a.add_element(i, i) res = a.judge_element_in_arr(5) assert res is True
def test_add_element_end(self): a = Ar(20) for i in range(10): a.add_element(i, i) a.add_last_element(9) assert a.get_element(a.get_size() - 1) == 9
def test_add_element_first(self): a = Ar(5) a.add_first_element(6) assert a.get_element(0) == 6
def test_add_element(self): a = Ar() for i in range(10): a.add_element(i, i) print(a._data) assert a._data == [i for i in range(10)]