Discuss / Python / zuoye-0416

zuoye-0416

Topic source

纸质盒子

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

第一题

# -*- coding: utf-8 -*-

def normalize(name):
    return name.title()

第二题

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

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

第三题 明儿写...

纸质盒子

#2 Created at ... [Delete] [Delete and Lock User]
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':''}

def str2float(s):
    a=0
    def fn(x,y):
        return x*10+y
    def char2num(s):
        return DIGITS[s]
    for p in s:
        a=a+1
        if p=='.':
            s=s[:a-1]+s[a-len(s):]
            break
    if a==len(s):
        return reduce(fn,map(char2num,s))
    else:
        return reduce(fn,map(char2num,s))/(10**(len(s)+1-a))

妈耶,终于成功了。

s=s[:a-1]+s[a-len(s):] 其实就是想要漏掉 . 所在的位置 a-len(s) 就没必要了,可以直接写成 s=s[:a-1]+s[a:]


  • 1

Reply