Discuss / Python / 第三题

第三题

Topic source

王饮刀

#1 Created at ... [Delete] [Delete and Lock User]
import math
from functools import reduce
def str1num(x1,x2):
    if x2==0:
        return x1
    return x1*10+x2
def str2num(x):
    return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'0':0,'.':0}[x]
def change(L):
    m=len(L)-L.find('.')-1
    return (reduce(str1num,map(str2num,L)))/int(math.pow(10,m))
print(change('12342.56565'))

王饮刀

#2 Created at ... [Delete] [Delete and Lock User]

更正错误之后的

import math
from functools import reduce
def str1num(x1,x2):
    if x2==-1:
        return x1
    return x1*10+x2
def str2num(x):
    return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'0':0,'.':-1}[x]
def change(L):
    m=len(L)-L.find('.')-1
    if m>=len(L):
        return reduce(str1num,map(str2num,L))
    return (reduce(str1num,map(str2num,L)))/math.pow(10,m)
print(change('324.24089'))

  • 1

Reply