## 1) Insertion, deletion and random access of array# 2) Assumes int for element type## Author: Wenru#class MyArray:"""A simple wrapper around List.You cannot have -1 in the array."""def __init__(self, capacity: int):self._data = []self._capacity = capacitydef __getitem__(self, position: int) -> object:return self._data[position]def __setitem__(self, index: int, value: object):self._data[index] = valuedef __len__(self) -> int:return len(self._data)def __iter__(self):for item in self._data:yield itemdef find(self, index: int) -> object:try:return self._data[index]except IndexError:return Nonedef delete(self, index: int) -> bool:try:self._data.pop(index)return Trueexcept IndexError:return Falsedef insert(self, index: int, value: int) -> bool:if len(self) >= self._capacity:return Falseelse:return self._data.insert(index, value)def print_all(self):for item in self:print(item)def test_myarray():array = MyArray(5)array.insert(0, 3)array.insert(0, 4)array.insert(1, 5)array.insert(3, 9)array.insert(3, 10)assert array.insert(0, 100) is Falseassert len(array) == 5assert array.find(1) == 5assert array.delete(4) is Truearray.print_all()if __name__ == "__main__":test_myarray()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型