Skip to content

Commit 37346cc

Browse files
Merge pull request apachecn#21 from cclauss/Python3
谢谢小哥哥 | print() is a function in Python 3
2 parents 37d5830 + 65fa953 commit 37346cc

File tree

11 files changed

+32
-21
lines changed

11 files changed

+32
-21
lines changed

docs/Algorithm/Sort/BubbleSort.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
# 冒泡排序
5+
from __future__ import print_function
56
def bubble_sort(l):
67
length = len(l)
78
# 外层循环 length遍,内层循环少一遍
@@ -16,4 +17,4 @@ def bubble_sort(l):
1617
if __name__ == "__main__":
1718
l = [5, 1, 9, 3, 2, 7]
1819
bubble_sort(l)
19-
print l
20+
print(l)

docs/Algorithm/Sort/InsertSort.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
插入排序的前提是:左边是有序的数列
66
而冒泡排序:相邻的值进行交换,一共进行n次交换
77
"""
8+
from __future__ import print_function
89

910

1011
def insert_sort(l):
1112
# 循环 除第一个数字组成的有序数组 以外的数字
1213
for i in range(1, len(l)):
1314
# 每一个数字,依次和有序数组进行比较
14-
print l[:i]
15+
print(l[:i])
1516
for j in range(len(l[:i])):
1617
if l[i] < l[j]:
1718
l[i], l[j] = l[j], l[i]
1819

1920

2021
if __name__ == "__main__":
2122
l = [5, 1, 9, 3, 2, 7]
22-
print l
23+
print(l)
2324
insert_sort(l)
2425
print("result: " + str(l))

docs/Algorithm/Sort/MergeSort.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# coding: utf-8
22

33

4+
from __future__ import print_function
45
def MergeSort(lists):
56
if len(lists) <= 1:
67
return lists
78
num = int(len(lists) / 2)
89
# 从中间,进行数据的拆分, 递归的返回数据进行迭代排序
910
left = MergeSort(lists[:num])
1011
right = MergeSort(lists[num:])
11-
print left
12-
print "*" * 20
13-
print right
14-
print "_" * 20
12+
print(left)
13+
print("*" * 20)
14+
print(right)
15+
print("_" * 20)
1516
return Merge(left, right)
1617

1718

@@ -27,9 +28,9 @@ def Merge(left, right):
2728
r += 1
2829
result += right[r:]
2930
result += left[l:]
30-
print 'result:', result
31+
print('result:', result)
3132
return result
3233

3334

3435
if __name__ == "__main__":
35-
print MergeSort([1, 2, 3, 4, 5, 6, 7, 90, 21, 23, 45])
36+
print(MergeSort([1, 2, 3, 4, 5, 6, 7, 90, 21, 23, 45]))

docs/Algorithm/Sort/QuickSort.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# coding:utf8
33

44

5+
from __future__ import print_function
56
def quick_sort(l, start, end):
67
i = start
78
j = end
@@ -14,12 +15,12 @@ def quick_sort(l, start, end):
1415
while i < j:
1516
# 和最右边的比较,如果>=key,然后j-1,慢慢的和前一个值比较;如果值<key,那么就交换位置
1617
while i < j and key <= l[j]:
17-
print key, l[j], '*' * 30
18+
print(key, l[j], '*' * 30)
1819
j -= 1
1920
l[i] = l[j]
2021
# 交换位置后,然后在和最左边的值开始比较,如果<=key,然后i+1,慢慢的和后一个值比较;如果值>key,那么就交换位置
2122
while i < j and key >= l[i]:
22-
print key, l[i], '*' * 30
23+
print(key, l[i], '*' * 30)
2324
i += 1
2425
l[j] = l[i]
2526
l[i] = key
@@ -32,4 +33,4 @@ def quick_sort(l, start, end):
3233
if __name__ == "__main__":
3334
l = [5, 1, 9, 3, 2, 7]
3435
quick_sort(l, 0, len(l) - 1)
35-
print l
36+
print(l)

docs/Algorithm/Sort/SelectionSort.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
选择排序的前提是:找到最大值的位置,最后才进行1次交换
66
而冒泡排序:相邻的值进行交换,一共进行n次交换
77
"""
8+
from __future__ import print_function
89

910

1011
def selection_sort(l):
@@ -17,12 +18,12 @@ def selection_sort(l):
1718
if l[j] > l[index]:
1819
index = j
1920
l[length], l[index] = l[index], l[length]
20-
print len(l) - length, l
21+
print(len(l) - length, l)
2122
length -= 1
2223

2324

2425
if __name__ == "__main__":
2526
l = [5, 1, 9, 3, 2, 7]
26-
print l
27+
print(l)
2728
selection_sort(l)
2829
print("result: " + str(l))

docs/Algorithm/Sort/ShellSort.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# coding: utf8
22

3+
from __future__ import print_function
34
def insert_sort(l, start, increment):
45
for i in range(start+increment, len(l), increment):
56
for j in range(start, len(l[:i]), increment):
67
if l[i] < l[j]:
78
l[i], l[j] = l[j], l[i]
8-
print increment, '--',l
9+
print(increment, '--',l)
910
return l
1011

1112
def shell_sort(l, increment):
@@ -20,6 +21,6 @@ def shell_sort(l, increment):
2021
if __name__ == "__main__":
2122
l = [5, 2, 9, 8, 1, 10, 3, 4, 7]
2223
increment = len(l)/3+1 if len(l)%3 else len(l)/3
23-
print "开始", l
24+
print("开始", l)
2425
l = shell_sort(l, increment)
25-
print "结束", l
26+
print("结束", l)

docs/Leetcode_Solutions/Summarization/Python刷题技巧笔记.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
# python有无数奇技淫巧和许多人不知道的秘密,这里用简洁的语言一条条表述出来,不断更新, 大家一起贡献!
23

34
# 1. python 排序

src/py2.x/TreeRecursionIterator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding:utf8
22

3+
from __future__ import print_function
34
class Node():
45
def __init__(self, value, left=None, right=None):
56
self.value = value
@@ -10,7 +11,7 @@ def midRecusion(node):
1011
if node is None:
1112
return
1213
midRecusion(node.left)
13-
print node.value,
14+
print(node.value, end=' ')
1415
midRecusion(node.right)
1516

1617
def midIterator(node):
@@ -21,7 +22,7 @@ def midIterator(node):
2122
node = node.left
2223
else:
2324
node = stack.pop(-1)
24-
print node.value,
25+
print(node.value, end=' ')
2526
node = node.right
2627

2728
if __name__ == "__main__":

src/py2.x/list2iteration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
迭代使用的是循环结构。
55
递归使用的是选择结构。
66
'''
7+
from __future__ import print_function
78

89
# 递归求解
910
def calculate(l):

src/py3.x/TreeRecursionIterator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding:utf8
22

3+
from __future__ import print_function
34
class Node():
45
def __init__(self, value, left=None, right=None):
56
self.value = value
@@ -10,7 +11,7 @@ def midRecusion(node):
1011
if node is None:
1112
return
1213
midRecusion(node.left)
13-
print node.value,
14+
print(node.value, end=' ')
1415
midRecusion(node.right)
1516

1617
def midIterator(node):
@@ -21,7 +22,7 @@ def midIterator(node):
2122
node = node.left
2223
else:
2324
node = stack.pop(-1)
24-
print node.value,
25+
print(node.value, end=' ')
2526
node = node.right
2627

2728
if __name__ == "__main__":

0 commit comments

Comments
 (0)