#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/4/16 14:13
# @Author  : LiuZhi
# @Site    : 
# @File    : MapAndReduce.py
# @Software: PyCharm
def f(x):
    return x*x
r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
print(list(r))

L = []
for n in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
    L.append(f(n))
print(L)

print(list(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9])))

from functools import reduce
def add(x, y):
    return x+y

print(reduce(add, [1, 3, 5, 7, 9]))

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]

print(reduce(fn, map(char2num, '13579')))

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2int(s):
    def fn(x, y):
        return x*10+y
    def char2num(s):
        return DIGITS[s]
    return reduce(fn, map(char2num, s))


def char2num(s):
    return DIGITS[s]
def str2int(s):
    return reduce(lambda x, y: x*10+y, map(char2num, s))

#利用map()函数，把用户输入的不规范的英文名字，变为首字母大写，其他小写的规范名字。输入：['adam', 'LISA', 'barT']，输出：['Adam', 'Lisa', 'Bart']
def normalize(name):
    pass

#Python提供的sum()函数可以接受一个list并求和，请编写一个prod()函数，可以接受一个list并利用reduce()求积：
def prod(l):
    pass

#利用map和reduce编写一个str2float函数，把字符串'123.456'转换成浮点数123.456
def str2float(s):
    pass




