Discuss / Python / 作业

作业

Topic source

Super-String

#1 Created at ... [Delete] [Delete and Lock User]

第一题:

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

第二题:

from functools import reduce


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

第三题:

from functools import reduce


def str2float(s):
    # 将字符串按小数点分割成整数部分和小数部分
    parts = s.split('.')
    # 将整数部分和小数部分分别转换为数字
    integer = reduce(lambda x, y: x * 10 + y, map(int, parts[0]))
    decimal = reduce(lambda x, y: x * 10 + y, map(int, parts[1])) / (10 ** len(parts[1]))
    # 返回整数部分和小数部分的和
    return integer + decimal

浅俗

#2 Created at ... [Delete] [Delete and Lock User]

都用到int()了为何不直接使用float()?

def str2float(s):
    return float(s)

Chosen1

#3 Created at ... [Delete] [Delete and Lock User]

强啊,我弄了那么长的一串,其实一个强转就行,牛逼啊,我怎么没想到呢


  • 1

Reply