Discuss / Python / 作业

作业

Topic source

AlexMou54

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

第一题 normalize() ```pyyhon

def normalize(name): name = name.lower() return name[0].upper()+name[1:]


>第二题 prod()
```python
def prod(L):
    def sum(x,y):
        return x*y
    return reduce(sum,L)

第三题 str2float()

#① 借鉴了下面老哥的做法,使用split()函数分割list
    DIGITAL={'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':'.'}
    def str2num(S):
        return DIGITAL[S]

    def fn(x,y):
        return x*10+y

    heads,lasts=s.split('.')
    return reduce(fn,map(str2num,heads))+reduce(fn,map(str2num,lasts))*pow(10,-len(lasts))
# ②利用index()函数求出'.'的位置

    DIGITAL={'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':'.'}
    def str2num(S):
        return DIGITAL[S]

    def fn(x,y):
        return x*10+y

    n1 = s.index('.')

    return reduce(fn,map(str2num,s[:n1]))+reduce(fn,map(str2num,s[n1+1:]))*pow(10,-(len(s)-len(s[n1:])))

  • 1

Reply