forked from lymin/python_interview_question
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path28.py
More file actions
37 lines (31 loc) · 682 Bytes
/
28.py
File metadata and controls
37 lines (31 loc) · 682 Bytes
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
# 利用str函数
def atoi(s):
num = 0
for i in s:
for j in range(10):
if i == str(j):
num = num*10 + j
return num
# 利用ord函数
def atoi1(s):
num = 0
for i in s:
num = num*10 + ord(i) - ord('0')
return num
# 利用eval函数
def atoi2(s):
num = 0
for i in s:
t = "%s * 1 " %i
n = eval(t)
num = num * 10 + n
return num
# 利用reduce函数
from functools import reduce
def atoi3(s):
return reduce(lambda num, i: num * 10 + ord(i) - ord('0'), s , 0)
if __name__ == '__main__':
s = "432"
#print(type(s))
#s = input ("please input: ")
print(atoi2(s))