Discuss / Python / 第三题懵逼中,看了评论还是不太懂

第三题懵逼中,看了评论还是不太懂

Topic source

singcao

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

第一题取了个巧,直接用了下capitalize函数

def normalize(name):
       return name.capitalize()
L1 = ['adam','LISA','barT']
L2 = list(map(normalize,L1))
print(L2)

第二题:

# -*- coding: utf-8 -*-
from functools import reduce

def prod(L):
    def ji(x,y):
        return x*y
    return reduce(ji,L)

第三题蒙圈。。

singcao

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

参考了下别人的

第三题:

from functools import reduce

def str2float(s):
    def fn(x,y):
        return x*10+y
    def char2num(s):
        digits = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
        return digits[s]
    #通过index函数获取.的位置
    n = s.index('.')
    #根据之前获取的.的位置信息,分别输出小数点前后的数字
    s1 = list(map(int,(x for x in s[: n])))
    s2 = list(map(int,(x for x in s[n + 1 :])))
    #最后利用fn函数将数字整合出来
    return reduce(fn,s1) + reduce(fn,s2)/10 ** len(s2)

蝎尾蛇嚴

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

#给你改简单了点。。。删了点东西

from functools import reduce

def str2float(s):

    def fn(x,y):

        return x*10+y

    #通过index函数获取.的位置

    n = s.index('.')

    #根据之前获取的.的位置信息,分别输出小数点前后的数字

    s1 = map(int,(x for x in s[: n]))

    s2 = map(int,(x for x in s[n + 1 :]))

    #最后利用fn函数将数字整合出来

    return reduce(fn,s1) + reduce(fn,s2)/10 ** (len(s)-n-1)


  • 1

Reply