Discuss / Python / 课后作业

课后作业

Topic source

Zflyee

#1 Created at ... [Delete] [Delete and Lock User]
  • 第一题
    # -*- coding: utf-8 -*-
    def normalize(name):
      return name[0].upper() + name[1:].lower()
    
  • 第二题
    # -*- coding: utf-8 -*-
    from functools import reduce
    def prod(L):
      result = reduce(lambda x,y: x*y, L)
      return result
    
  • 第三题
# -*- coding: utf-8 -*-
from functools import reduce

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5,
          '6': 6, '7': 7, '8': 8, '9': 9}

def str2float(s):
    temp = s.split('.')
    s_L = temp[0]
    s_R = temp[1]

    def fn(x, y):
        return x * 10 + y
    def char2num(s):
        return DIGITS[s]
    x1 = reduce(fn,map(char2num,s_L))
    x2 = reduce(fn,map(char2num,s_R))/(10**len(s_R))
    return x1 + x2

  • 1

Reply