Discuss / Python / map/reduce 作业

map/reduce 作业

Topic source

第一题

def normalize(name):
    name = name.capitalize()
    return name

第二题

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

第三题

def str2float(s): 
    S1,s2 = s.split(".")
    S2 = s2[::-1] + '0'

    def char2num(i):
        digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
        return digits[i]

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

    def g(x,y):
        return x * 0.1 + y

    Int = reduce(f,map(char2num,S1))
    Float = reduce(g,map(char2num,S2))

    return Int + Float

  • 1

Reply