Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
68 lines (54 loc) · 2.03 KB

File metadata and controls

68 lines (54 loc) · 2.03 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from typing import Optional
# coding = utf-8
'''
typing模块的作用:
1 类型检查,防止运行时出现参数和返回值类型不符合
2 作为开发文档附加说明,方便使用者调用时传入和返回参数类型
3 该模块加入后并不会影响程序的运行,不会报正式的错误,只有提醒
'''
# 1) Insertion, deletion, and random access of array
# 2) Assumes int for element type
class MyArray:
def __init__(self, cacpcity: int):
self._data = []
self._count = 0
self._cacpcity = cacpcity
def __getitem__(self, position: int) -> int:
return self._data[position]
# 对于给定的键返回指定的值
def find(self, index: int) -> Optional[int]:
if index >= self._count or index <= -self._count: return None
return self._data[index]
def delete(self, index: int) -> bool:
if index >= self._count or index <= -self._count: return False
self._data[index: -1] = self[index+1:]
self._count -= 1
# -1表示从后往前第二个
def insert(self, index: int, value: int) -> bool:
if index >= self._count or index <= -self._count: return False
if self._cacpcity == self._count: return False
self._data.insert(index, value)
self._count += 1
return True
def insert_to_tail(self, value: int) -> bool:
if self._count == self._cacpcity: return False
self._data.append(value)
self._count += 1
return True
def __repr__(self):
return " ".join(str(num) for num in self._data[:self._count])
# 返回一个字符串对象, 表示每个数字的长度打印出来
def print_all(self):
for num in self._data[:self._count]:
print(f"{num}", end=" ")
# end表示在结尾加上指定字符串
print("\n", flush=True)
# flush表示是否立刻显示
if __name__ == "__main__":
a = MyArray(6)
for i in range(6):
a.insert_to_tail(i)
a.delete(2)
a.print_all()
a.__repr__()
print(a)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.