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

Sign up
Appearance settings

Commit 5c8f0d1

Browse files
committed
更新排序单元测试
1 parent 26b8bfd commit 5c8f0d1

File tree

9 files changed

+117
-155
lines changed

9 files changed

+117
-155
lines changed

‎.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,5 @@ ENV/
203203
.idea
204204

205205
.vscode
206-
.pytest_cache
206+
.pytest_cache
207+
.env

‎Pipfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ format = "columns"
99
pylint = "*"
1010
"autopep8" = "*"
1111
pytest = "*"
12+
jedi = "*"
1213

1314
[requires]
1415
python_version = "3.6"

‎Pipfile.lock‎

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎algorithm/search/test_search.py‎

Lines changed: 0 additions & 51 deletions
This file was deleted.

‎algorithm/sort/__init__.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3+
4+
from algorithm.sort.bubble_sort import bubble_sort, bubble_sort_flag
5+
from algorithm.sort.count_sort import count_sort
6+
from algorithm.sort.heap_sort import heap_sort
7+
from algorithm.sort.insert_sort import insert_sort
8+
from algorithm.sort.marge_sort import merge_sort
9+
from algorithm.sort.quick_sort import quick_sort, quick_sort_cookbook
10+
from algorithm.sort.selection_sort import selection_sort
11+
from algorithm.sort.shell_sort import shell_sort

‎algorithm/sort/test_sort.py‎

Lines changed: 0 additions & 102 deletions
This file was deleted.

‎algorithm/test/__init__.py‎

Whitespace-only changes.

‎algorithm/test/test_sort.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import random
2+
from algorithm.sort import *
3+
4+
class TestSort:
5+
def gene_list(self):
6+
for i in range(11):
7+
yield [random.randint(0, 2**i) for _ in range(2**i)]
8+
9+
def sort_t(self, sort_func):
10+
g = self.gene_list()
11+
res = []
12+
for l in g:
13+
lt = l[:]
14+
lt.sort()
15+
res.append(lt == sort_func(l))
16+
17+
return all(res)
18+
19+
def test_bubble_sort(self):
20+
assert self.sort_t(bubble_sort)
21+
22+
def test_bubble_sort_flag(self):
23+
assert self.sort_t(bubble_sort_flag)
24+
25+
def test_count_sort(self):
26+
assert self.sort_t(count_sort)
27+
28+
def test_shell_sort(self):
29+
assert self.sort_t(shell_sort)
30+
31+
def test_seletion_sort(self):
32+
assert self.sort_t(selection_sort)
33+
34+
def test_marge_sort(self):
35+
assert self.sort_t(merge_sort)
36+
37+
def test_quick_sort(self):
38+
assert self.sort_t(quick_sort)
39+
40+
41+
def test_quick_sort_cookbook(self):
42+
assert self.sort_t(quick_sort_cookbook)
43+
44+
def test_insert_sort(self):
45+
assert self.sort_t(insert_sort)
46+
47+
48+
def test_heap_sort(self):
49+
assert self.sort_t(heap_sort)
50+

‎data_structure/tree/tree.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,48 @@ def postorder_traversal_while(self):
7878

7979
return res[::-1]
8080

81+
def preorder_traversal_recursion(self):
82+
"""
83+
树的前序遍历
84+
:return: list of tree node value
85+
"""
86+
res = []
87+
if not self.root:
88+
return res
89+
90+
def _inner(root):
91+
inner_res = []
92+
if root.value:
93+
inner_res.append(root.value)
94+
for sub_node in root.children:
95+
inner_res += _inner(sub_node)
96+
return inner_res
97+
return _inner(self.root)
98+
99+
def postorder_traversal_recursion(self):
100+
"""
101+
树的后序遍历
102+
:return: list of tree node value
103+
"""
104+
res = []
105+
if not self.root:
106+
return res
107+
108+
def _inner(root):
109+
inner_res = []
110+
if root.value:
111+
for sub_node in root.children:
112+
inner_res += _inner(sub_node)
113+
inner_res.append(root.value)
114+
return inner_res
115+
116+
return _inner(self.root)
117+
81118

82119
if __name__ == '__main__':
83120
t = Tree(
84121
TreeNode(1, [TreeNode(2, [TreeNode(4), TreeNode(5), TreeNode(6)]), TreeNode(3, [TreeNode(7), TreeNode(8)])]))
85122
print(t.preorder_traversal_while())
86123
print(t.postorder_traversal_while())
124+
print(t.preorder_traversal_recursion())
125+
print(t.postorder_traversal_recursion())

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /