python习题

廖雪峰python教程上的习题

map/reduce

1
2
3
4
def (name):
b = name[0].upper()
b += name[1:].lower()
return b
1
2
3
4
def prod(L):
def a(x ,y):
return x * y
return reduce(a,L)

第三题

1

1
2
3
4
5
6
7
8
9
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.':'.'}
i = 0
while s[i]!='.':
i += 1
def change(s):
return DIGITS[s]
def sum(x,y):
return x*10 + y
return reduce(sum,map(change,s[:i])) + reduce(sum,map(change,s[i+1:]))/pow(10,(len(s)-i-1))

2

1
return reduce(lambda x,y: x*10 + y,map(int,s.split('.')[0])) + reduce(lambda x,y: x/10 + y,list(map(int,s.split('.')[1]))[::-1])/10