Skip to content

Commit d70810b

Browse files
committed
py3
1 parent 050178a commit d70810b

19 files changed

+161
-140
lines changed

2d_list.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@
3737
3838
score_list = [[random.randint(0,100) for i in range(student_num)] for j in range(len(course_list))]
3939
for i in range(len(course_list)):
40-
print "score of every one in %s:"%course_list[i]
41-
print score_list[i]
40+
print("score of every one in %s:"%course_list[i])
41+
print(score_list[i])
4242

4343
every_score,every_total,ave_one_course = score(score_list,course_list,student_num)
44-
print "\n"
45-
print "NEXT IS EVERY ONE SCORE IN EVERY COURSE:"
44+
print("\n")
45+
print("NEXT IS EVERY ONE SCORE IN EVERY COURSE:")
4646
for name in course_list:
47-
print name,
48-
print "\t"
49-
print every_score
50-
print "\n"
51-
print "every one all score:\t",every_total
52-
print "every course of average score:\t",ave_one_course
47+
print(name,)
48+
print("\t")
49+
print(every_score)
50+
print("\n")
51+
print("every one all score:\t",every_total)
52+
print("every course of average score:\t",ave_one_course)
5353

5454
##qiwsir#gmail.com (# to @)
5555

2d_list.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ def score(score_list,course_list,student_num):
3131

3232
score_list = [[random.randint(0,100) for i in range(student_num)] for j in range(len(course_list))]
3333
for i in range(len(course_list)):
34-
print "score of every one in %s:"%course_list[i]
35-
print score_list[i]
34+
print("score of every one in %s:"%course_list[i])
35+
print(score_list[i])
3636

3737
every_score,every_total,ave_one_course = score(score_list,course_list,student_num)
38-
print "\n"
39-
print "NEXT IS EVERY ONE SCORE IN EVERY COURSE:"
38+
print("\n")
39+
print("NEXT IS EVERY ONE SCORE IN EVERY COURSE:")
4040
for name in course_list:
41-
print name,
42-
print "\t"
43-
print every_score
44-
print "\n"
45-
print "every one all score:\t",every_total
46-
print "every course of average score:\t",ave_one_course
41+
print(name,)
42+
print("\t")
43+
print(every_score)
44+
print("\n")
45+
print("every one all score:\t",every_total)
46+
print("every course of average score:\t",ave_one_course)

ahead_one_step.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
return a
1818

1919
if __name__ =="__main__":
20-
print ahead_one()
20+
print(ahead_one())
2121

2222
#解决(racket 5.2.1)
2323

ahead_one_step.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
即,a[0]=a[1],a[1]=a[2],…最后一个元素的值是原来第一个元素的值,然后输出这个数组。
1010
"""
1111

12-
def ahead_one():
13-
a = [i for i in range(1, 11)]
12+
def ahead_one(a):
1413
b = a.pop(0)
1514
a.append(b)
1615
return a
1716

1817
if __name__ =="__main__":
19-
print ahead_one()
18+
a = [i for i in range(1, 11)]
19+
ahead_one(a)
20+
print(ahead_one(a))

average_score.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@
2727

2828
if __name__=="__main__":
2929
score = make_score(40)
30-
print "the number of less average is:",less_average(score)
31-
print "the every socre is[from big to small]:",sorted(score,reverse=True)
30+
print("the number of less average is:",less_average(score))
31+
print("the every socre is[from big to small]:",sorted(score,reverse=True))

average_score.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ def less_average(score):
2525
if __name__=="__main__":
2626
score = make_score(40)
2727
average_num,less_num = less_average(score)
28-
print 'the score of average is:',average_num
29-
print "the number of less average is:",less_num
30-
print "the every score is[from big to small]:",sorted(score,reverse=True)
28+
print('the score of average is:', average_num)
29+
print("the number of less average is:", less_num)
30+
print("the every score is[from big to small]:", sorted(score,reverse=True))

big_int.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#问题
1+
# 问题
22

33
大整数相乘
44

5-
#思路说明
5+
## 思路说明
66

77
对于大整数计算,一般都要用某种方法转化,否则会溢出。但是python无此担忧了。
88

@@ -24,8 +24,8 @@ Python支持**“无限精度”的整数,**一般情况下不用考虑整数
2424

2525
两个整数相乘:**阿拉伯乘法**。关于这个乘法的详细描述,请看:[http://ualr.edu/lasmoller/medievalmult.html](http://ualr.edu/lasmoller/medievalmult.html)
2626

27-
#解决(Python)
28-
27+
## 解决(Python)
28+
```python
2929
#!/usr/bin/env python
3030
#coding:utf-8
3131

@@ -73,7 +73,6 @@ Python支持**“无限精度”的整数,**一般情况下不用考虑整数
7373

7474

7575
#计算阿拉伯乘法表格左侧开始的各项之和
76-
7776
def summ_left(lst):
7877
summ = []
7978
x = [i for i in range(len(lst))]
@@ -93,7 +92,6 @@ Python支持**“无限精度”的整数,**一般情况下不用考虑整数
9392
return summ
9493

9594

96-
9795
#计算阿拉伯乘法表格底部开始的各项之和
9896

9997
def summ_end(lst):
@@ -135,9 +133,9 @@ Python支持**“无限精度”的整数,**一般情况下不用考虑整数
135133

136134
if __name__=="__main__":
137135
arabic_multiplication(469,37)
136+
```
137+
## 联系方法
138138

139-
#联系方法
140139
- qiwsir#gmail.com
141-
- https://qiwsir.github.io
142-
- http://weibo.com/qiwsir
143-
140+
- <https://qiwsir.github.io>
141+
- <http://weibo.com/qiwsir>

big_int.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ def arabic_multiplication(num1,num2):
2626
int_martix = [[i*j for i in num_lst1] for j in num_lst2]
2727

2828
#将上述元素为数字的list转化为元素类型是str,主要是将9-->'09'
29-
str_martix = [map(convert_to_str,int_martix[i]) for i in range(len(int_martix))]
30-
29+
str_martix = [list(map(convert_to_str,int_martix[i])) for i in range(len(int_martix))]
30+
print(str_martix)
3131
#将上述各个list中的两位数字分开:['01','29','03']-->[0,2,0],[1,9,3]
3232
martix = [[int(str_martix[i][j][z]) for j in range(len(str_martix[i]))] for i in range(len(str_martix)) for z in range(2)]
33-
33+
print(martix)
3434
#计算阿拉伯乘法表的左侧开始各项和
3535
sum_left = summ_left(martix)
3636

@@ -47,8 +47,8 @@ def arabic_multiplication(num1,num2):
4747
#翻转结果并合并为一个结果字符串数值
4848
result.reverse()
4949
int_result = "".join(result)
50-
print "%d*%d="%(num1,num2)
51-
print int_result
50+
print("%d*%d="%(num1,num2))
51+
print(int_result)
5252

5353

5454
#将int类型转化为str类型,9-->'09'
@@ -65,8 +65,11 @@ def convert_to_str(num):
6565
def summ_left(lst):
6666
summ = []
6767
x = [i for i in range(len(lst))]
68+
print("x:", x)
6869
y = [j for j in range(len(lst[0]))]
70+
print("y:", y)
6971
sx = [i for i in x if i%2==0]
72+
print("sx:", sx)
7073
for i in sx:
7174
s=0
7275
j=0
@@ -115,11 +118,12 @@ def take_digit(lst):
115118
tmp = 0
116119
digit_list.append(str(lstm))
117120
else:
118-
tmp = lstm/10
119-
mm = lstm-tmp*10
121+
tmp = lstm // 10
122+
mm = lstm - tmp * 10
120123
digit_list.append(str(mm))
121124
return digit_list
122125

123126

124127
if __name__=="__main__":
125-
arabic_multiplication(469,37)
128+
# arabic_multiplication(469,37)
129+
arabic_multiplication(1234, 567)

bin_search.md

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#问题
1+
# 问题
22

33
二分查找
44

55
list.index()无法应对大规模数据的查询,需要用其它方法解决,这里谈的就是二分查找
66

7-
#思路说明
7+
## 思路说明
88

99
在查找方面,python中有list.index()的方法。例如:
1010

@@ -13,7 +13,7 @@ list.index()无法应对大规模数据的查询,需要用其它方法解决
1313
1
1414
>>> a.index(5) #如果没有该值,则报错
1515
Traceback (most recent call last):
16-
File "<stdin>", line 1, in <module>
16+
File "<stdin>", line 1, in <module>
1717
ValueError: 5 is not in list
1818

1919
这是python中基本的查找方法,虽然简单,但是,如果由于其时间复杂度为O(n),对于大规模的查询恐怕是不足以胜任的。二分查找就是一种替代方法。
@@ -28,37 +28,37 @@ list.index()无法应对大规模数据的查询,需要用其它方法解决
2828

2929
这种搜索算法每一次比较都使搜索范围缩小一半。时间复杂度:O(logn)
3030

31-
#解决(Python)
32-
33-
def binarySearch(lst, value,low,high): #low,high是lst的查找范围
34-
if high < low:
35-
return -1
36-
mid = (low + high)/2
37-
if lst[mid] > value:
38-
return binarySearch(lst, value, low, mid-1)
39-
elif lst[mid] < value:
40-
return binarySearch(lst, value, mid+1, high)
41-
else:
42-
return mid
43-
44-
#也可以不用递归方法,而采用循环,如下:
45-
46-
def bsearch(l, value):
47-
lo, hi = 0, len(l)-1
48-
while lo <= hi:
49-
mid = (lo + hi) / 2
50-
if l[mid] < value:
51-
lo = mid + 1
52-
elif value < l[mid]:
53-
hi = mid - 1
54-
else:
55-
return mid
56-
return -1
57-
58-
if __name__ == '__main__':
59-
l = range(50)
60-
print binarySearch(l,10,0,49)
61-
print bsearch(l,10)
31+
## 解决(Python)
32+
33+
def binarySearch(lst, value,low,high): #low,high是lst的查找范围
34+
if high < low:
35+
return -1
36+
mid = (low + high)/2
37+
if lst[mid] > value:
38+
return binarySearch(lst, value, low, mid-1)
39+
elif lst[mid] < value:
40+
return binarySearch(lst, value, mid+1, high)
41+
else:
42+
return mid
43+
44+
#也可以不用递归方法,而采用循环,如下:
45+
46+
def bsearch(l, value):
47+
lo, hi = 0, len(l)-1
48+
while lo <= hi:
49+
mid = (lo + hi) / 2
50+
if l[mid] < value:
51+
lo = mid + 1
52+
elif value < l[mid]:
53+
hi = mid - 1
54+
else:
55+
return mid
56+
return -1
57+
58+
if __name__ == '__main__':
59+
l = range(50)
60+
print binarySearch(l,10,0,49)
61+
print bsearch(l,10)
6262

6363
对于python,不能忽视其强大的标准库。经查阅,发现标准库中就有一个模块,名为:bisect。其文档中有这样一句话:
6464

@@ -77,15 +77,15 @@ list.index()无法应对大规模数据的查询,需要用其它方法解决
7777
- 关于bisect模块的更多内容,可以参看[官方文档](https://docs.python.org/2/library/bisect.html)
7878

7979
下面演示这个模块的一个函数
80-
81-
from bisect import *
82-
83-
def bisectSearch(lst, x):
84-
i = bisect_left(lst, x) #bisect_left(lst,x),得到x在已经排序的lst中的位置
85-
if i != len(lst) and lst[i] == x:
86-
return i
87-
raise ValueError
88-
89-
if __name__=="__main__":
90-
lst = sorted([2,5,3,8])
91-
print bisectSearch(lst,5)
80+
81+
from bisect import *
82+
83+
def bisectSearch(lst, x):
84+
i = bisect_left(lst, x) #bisect_left(lst,x),得到x在已经排序的lst中的位置
85+
if i != len(lst) and lst[i] == x:
86+
return i
87+
raise ValueError
88+
89+
if __name__=="__main__":
90+
lst = sorted([2,5,3,8])
91+
print bisectSearch(lst,5)

change_coin.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ def change_coin(money):
2020
change = {}
2121

2222
for one in coin:
23-
num_coin = money//one #除法,取整,得到该单位硬币的个数
23+
# num_coin = money//one #除法,取整,得到该单位硬币的个数
24+
num_coin, num_remain = divmod(money, one)
2425
if num_coin>0:
2526
change[one]=num_coin
26-
num_remain = money%one #取余数,得到剩下的钱数
27+
# num_remain = money%one #取余数,得到剩下的钱数
2728
if num_remain==0:
2829
break
2930
else:
@@ -48,23 +49,22 @@ def coinChange(centsNeeded, coinValues):
4849
coin = [1,2,5,10,20,50,100] #1分,2分,5分,1角,2角,5角,1元
4950
num_coin = change_coin(money)
5051
result = [(key,num_coin[key]) for key in sorted(num_coin.keys())]
51-
print "You have %s RMB"%money
52-
print "I had to change you:"
53-
print " Coin Number"
52+
print("You have %s RMB"%money)
53+
print("I had to change you:")
54+
print(" Coin Number")
5455
for i in result:
5556
if i[0]==100:
56-
print "Yuan %d %d"%(i[0]/100,i[1])
57+
print("Yuan %d %d"%(i[0]/100,i[1]))
5758
elif i[0]<10:
58-
print "Fen %d %d"%(i[0],i[1])
59+
print("Fen %d %d"%(i[0],i[1]))
5960
else:
60-
print "Jiao %d %d"%(i[0]/10,i[1])
61+
print("Jiao %d %d"%(i[0]/10,i[1]))
6162
num2 = coinChange(5,coin)
62-
print num2
63+
print(num2)
6364
#执行结果
6465
#You have 3.42 RMB
6566
#I had to change you:
6667
# Coin Number
6768
# Fen 2 1
6869
# Jiao 2 2
6970
# Yuan 1 3
70-

0 commit comments

Comments
 (0)