Skip to content

Commit fb39e73

Browse files
committed
update
1 parent b2d420e commit fb39e73

10 files changed

Lines changed: 87 additions & 26 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
## 作者的话
1010

1111
书中经典的算法示例使用python3语言实现,并且配有详细的算法原理说明。
12-
整个部分分为两个部分,第一个部分是基于`算法》第4版` + `算法导论》第3版`这两本书中的所有例子写的。作为基础算法部分。
12+
整个部分分为两个部分,第一个部分是基于`《算法导论》第3版`这两本书中的所有例子写的。作为基础算法部分。
1313
第二部分是LeeCode算法题库,选取其中最具代表性的100个算法题来演示。
1414

1515
从2013年就开始写这个系列,写到动态规划后就停了,期间工作太忙根本抽不出时间来写。

algorithms/__init__.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
#!/usr/bin/env python
22
# -*- encoding: utf-8 -*-
33
"""
4-
《算法》第4版 + 《算法导论》第3版
5-
使用python3语言实现书中经典的算法示例
4+
《算法导论》第3版,使用python3语言实现书中经典的算法示例
65
"""
76

8-
s = {}
9-
s['1'] = None
10-
s['2'] = None
11-
print(s['1'])
12-
137

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
# -*- encoding: utf-8 -*-
22
"""常见的数据结构
3-
Some of description...
43
"""
4+
5+
6+
class Node:
7+
"""
8+
节点信息
9+
"""
10+
def __init__(self, item_, next_=None):
11+
self.item = item_
12+
self.next = next_
13+
14+
def __str__(self):
15+
return str(self.item)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- encoding: utf-8 -*-
2+
"""基于环形数组实现的一个简单队列
3+
数组A[1..n]实现最多容纳n-1个元素的队列。
4+
"""
5+
6+
7+
class ArrayQueue:
8+
def __init__(self, size):
9+
self.head = 0 # 队头元素下标
10+
self.tail = 0 # 下一个插入位置
11+
self._arr = [None] * size # 实际存放数据的数组
12+
13+
def is_empty(self):
14+
return self.head == self.tail
15+
16+
def is_full(self):
17+
return self.size() == len(self._arr) - 1
18+
19+
def size(self):
20+
return (len(self._arr) + self.tail - self.head) % len(self._arr)
21+
22+
def enqueue(self, item):
23+
# 队列满了则抛出异常
24+
if self.is_full():
25+
raise LookupError('Queue is full')
26+
self._arr[self.tail] = item
27+
self.tail = (self.tail + 1) % len(self._arr)
28+
29+
def dequeue(self):
30+
# 队列空则抛出异常
31+
if self.is_empty():
32+
raise LookupError('Queue underflow')
33+
item = self._arr[self.head]
34+
self._arr[self.head] = None
35+
self.head = (self.head + 1) % len(self._arr)
36+
return item
37+
38+
def __iter__(self):
39+
while not self.is_empty():
40+
yield self.dequeue()
41+
42+
43+
if __name__ == '__main__':
44+
q = ArrayQueue(6)
45+
q.enqueue(1)
46+
q.enqueue(2)
47+
q.enqueue(3)
48+
q.enqueue(4)
49+
q.enqueue(5)
50+
print(q.dequeue())
51+
print(q.dequeue())
52+
print(q.dequeue())
53+
print(q.dequeue())
54+
print(q.dequeue())
55+
q.enqueue(1)
56+
q.enqueue(2)
57+
q.enqueue(3)
58+
q.enqueue(4)
59+
q.enqueue(5)
60+
print(q.dequeue())
61+
print(q.dequeue())
62+
print(q.dequeue())
63+
print(q.dequeue())
64+
print(q.dequeue())
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# -*- encoding: utf-8 -*-
2-
"""自己实现的一个简单队列
2+
"""基于链表实现的一个简单队列
33
队尾插入元素,队头取元素。就跟在菜市场排队买菜原理是一样的
44
"""
5-
from algorithms.ch00structure.stack import Node
5+
from algorithms.ch01structure import Node
66

77

8-
class Queue:
8+
class LinkedQueue:
99
def __init__(self):
1010
self.first = None # beginning of queue
1111
self.last = None # end of queue

algorithms/ch01structure/prior_queue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55
from functools import total_ordering
66

7-
from algorithms.ch00structure.stack import Stack
7+
from algorithms.ch01structure.simple_stack import Stack
88

99

1010
class MaxPriorQueue:
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
另一方面,迭代器应该一直可以迭代。迭代器的 `__iter__` 方法应该返回自身。
55
一般可使用生成器函数实现更符合python风格的可迭代对象。
66
"""
7+
from algorithms.ch01structure import Node
78

89

910
class Stack:
@@ -38,11 +39,3 @@ def __iter__(self):
3839
while self.n > 0:
3940
yield self.pop()
4041

41-
42-
class Node:
43-
def __init__(self, item_, next_=None):
44-
self.item = item_
45-
self.next = next_
46-
47-
def __str__(self):
48-
return str(self.item)

algorithms/ch02sort/base/template.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def less(self, val1, val2):
2525
def show(self, vals):
2626
for val in vals:
2727
print("{}".format(val), end=' ')
28-
print()
2928

3029
def is_sorted(self, vals):
3130
for i in range(1, len(vals)):

algorithms/ch09sample/m06_hornerpoly.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
__author__ = 'Xiong Neng'
1010

1111

12-
def hornerPoly(coefficientArr, x):
12+
def horner_poly(coefficient_arr, x):
1313
res = 0
14-
for i in range(len(coefficientArr))[-1::-1]:
15-
res = coefficientArr[i] + x * res
14+
for i in range(len(coefficient_arr))[-1::-1]:
15+
res = coefficient_arr[i] + x * res
1616
return res
1717

1818

1919
if __name__ == '__main__':
20-
print(hornerPoly((1, 2, 3), 2))
20+
print(horner_poly((1, 2, 3), 2))

0 commit comments

Comments
 (0)