Discuss / Python / 第一次交代码

第一次交代码

Topic source

<pre> def normalize(name): namenew = '' if ord(name[0]) > 96: namenew += chr(ord(name[0]) - 32) else: namenew += name[0] name = name[1:] while name: if ord(name[0]) < 97: namenew += chr(ord(name[0]) + 32) else: namenew += name[0] name = name[1:] return namenew L1 = ['adam', 'SALLY', 'daisY'] L2 = list(map(normalize, L1)) print(L2) </pre>

这个东西吃乘号啊……

<pre> 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, '.': 10} def char2sum(s): return DIGITS[s] def str2float(s): ind = 0 while DIGITS[s[ind]] < 10: ind += 1 left = reduce(lambda x, y: x 10 + y, map(char2sum, s[:ind])) right = reduce(lambda x, y: x 10 + y, map(char2sum, s[ind + 1:])) return left + right 10 * (ind + 1 - len(s)) print(str2float('123.456')) </pre>

from functools import reduce

def prod(L): return reduce(lambda x, y: x * y, L) print(prod([3,5,7,9]))


  • 1

Reply