基础知识
1 | 数据分析 7 绘图和可视化 |
###数据结构 –链表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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171class Node(object):
def __init__(self, data):
self.data = data
self.next = None
def set_data(self, data):
self.data = data
def set_next(self, node):
self.next = node
node1 = Node('apple')
node2 = Node('pear')
node3 = Node('watermelon')
node4 = Node('Strawberry')
node1.set_next(node2)
node2.set_next(node3)
node3.set_next(node4)
node = node1
#
# while True:
# print(node.data)
# if node.next:
# node = node.next
# continue
# break
# head = node1
# while True:
# print(head.data)
# if head.next is None:
# break
# head = head.next
class LinkedList(object):
def __init__(self):
self.head = None
def append_item(self, item):
temp = Node(item)
if self.head is None:
self.head = temp
return temp.data
current = self.head
# 遍历链表
while current.next is not None:
current = current.next
# 此时current是链表的最后一个元素
current.set_next(temp)
return temp.data
def remove_item(self, item):
current = self.head
pre = None
while current is not None:
if current.data == item:
if not pre:
self.head = current.next
else:
pre.set_next(current.next)
break
else:
pre = current
current = current.next
def remove_by_index(self, index):
if self.head is None or index > self.size() or index < 0:
print('LinkList is empty')
current = self.head
if index == 0:
p = self.head
self.head = p.next
current = self.head
else:
current = self.head
post = self.head
j = 0
while current.next is not None and j < index:
post = current
current = current.next
j += 1
if index == j:
post.next = current.next
current = self.head
while current is not None:
print(current.data)
current = current.next
def add_item(self, item, index):
if index <= 1:
temp = Node(item)
temp.set_next(self.head)
self.head = temp
elif index > self.size():
self.append_item(item)
else:
temp = Node(item)
count = 1
pre = None
current = self.head
while count < index:
count += 1
pre = current
current = current.next
pre.set_next(temp)
temp.set_next(current)
return temp.data
def size(self):
current = self.head
count = 0
while current is not None:
count += 1
current = current.next
return count
def get_item(self, index):
if self.head is None:
print('LinkList is empty')
i = 1
current = self.head
while current.next is not None and i < index:
current = current.next
i += 1
if i == index:
return current.data
else:
print('target is not exist')
def index(self, item):
if self.head is None:
print('LinkList is empty')
current = self.head
i = 0
while current.next is not None and not current.data == item:
current = current.next
i += 1
if current.data == item:
return i
else:
print('没有此值')
def __str__(self):
str1 = ''
current = self.head
str1 += str(current.data) + ','
while current.next:
current = current.next
str1 += str(current.data) + ','
return str1
link = LinkedList()
# a = link.append_item(5)
# print(a)
k = ['e', 2, 3, 'a', 'b', 'c', 'd']
list1 = []
for i in k:
list1.append(link.append_item(i))
print(list1)
print(link.size())
print(link.get_item(4))
print(link.add_item('apple', 4))
link.remove_by_index(4)
link.remove_item('apple')
print(link)
###数据结构 – 堆栈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
41class Stack(object):
def __init__(self, max_size):
self.container = []
self.max_size = max_size
def push(self, item):
# 判断栈是否已满
if self.is_full():
return 'stack is full'
self.container.append(item)
def pop(self, item):
# 判断栈是否为空
if not self.is_empty():
self.container.pop(item)
def size(self):
return len(self.container)
def is_empty(self):
return len(self.container) == 0
def is_full(self):
return len(self.container) == self.max_size
def index(self, item):
for i in range(len(self.container)):
if self.container[i] == item:
print(i)
stack = Stack(6)
for i in range(5):
stack.push(i)
print(stack.container)
stack.push('a')
print(stack.container)
# stack.pop(2)
# print(stack.container)
stack.index(4)
###单元测试1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22import unittest
class TestClass01(unittest.TestCase):
def setUp(self):
print('setup....')
self.l = []
self.l.append(3)
def test_case02(self):
self.assertEquals(len(self.l), 1)
my_pi = 3.14
self.assertFalse(isinstance(my_pi, int))
def test_case01(self):
my_str = "Carmack"
my_int = 999
self.assertTrue(isinstance(my_str, str))
self.assertTrue(isinstance(my_int, int))
if __name__ == '__main__':
unittest.main()