【每日一题0722】 字符串格式化

:woman_mage:哈利波特需要一个解决方案,使它接收一个输入整数 n ,返回一个每3位数字后面用逗号进行分隔的字符串。

示例:
输入:10000,输出:‘10,000’
输入:35235235,输出:‘35,235,235’

题目难度:简单
题目来源:codewars

def group_by_commas(n):
    pass
    
assert group_by_commas(100) == '100'
assert group_by_commas(1000000) == '1,000,000'
assert group_by_commas(35235235) == '35,235,235'

没有看上去那么容易,有简单解法吗?

def group_by_commas(n:int):
    s=list(str(n))
    length=len(s)
    nums=len(s)//3
    if length%3==0:
        nums=nums-1
    for i in range(nums):
        s.insert(-(3*(i+1)+i),',')
    return ''.join(s)

assert group_by_commas(100) == '100'
assert group_by_commas(1000000) == '1,000,000'
assert group_by_commas(35235235) == '35,235,235'
def group_by_commas(n):
    if len(str(n)) <= 3:
        return str(n)
    n = str(n)[::-1]
    res = [n[0]]
    for i in range(1, len(n)):
        if i % 3 == 0:
            res.append(",")
        res.append(n[i])
    return "".join(res[::-1])


assert group_by_commas(100) == '100'
assert group_by_commas(1000000) == '1,000,000'
assert group_by_commas(35235235) == '35,235,235'
def group_by_commas(num):
    result =str(format(num, ","))
    return result
assert group_by_commas(100) == '100'
assert group_by_commas(1000000) == '1,000,000'
assert group_by_commas(35235235) == '35,235,235'
def group_by_commas(n):
    n_list = list(str(n))
    length = len(n_list)
    if length < 4:
        return str(n)
    else:
        times = length // 3
        for i in range(1, times+1):
            n_list.insert(-(3 * i + i-1), ',')
        return ''.join(n_list)

#以逗号分隔数字(千位分隔符),网上抄的

def group_by_commas(n):
    str = '{:,}'.format(n)
    return str

assert group_by_commas(100) == '100'
assert group_by_commas(1000000) == '1,000,000'
assert group_by_commas(35235235) == '35,235,235'
def group_by_commas(n):
    """ 0722 字符串格式化 """
    length = len(str(n))
    li = [str(n)[i] for i in range(length)]
    li.reverse()
    re = [li[0]]
    for i in range(1,length):
        if i%3 == 0:
            re.append(',')
        re.append(li[i])

    return ''.join(re[::-1])

看到你们用format直接输出,我流下了没有技术的眼泪 :sob:

@439379726_6330 :+1: :+1: :+1:优秀!
@fwj 参考这个解法~

思路1:

def group_by_commas(n):
    return '{:,}'.format(n)
    
assert group_by_commas(100) == '100'
assert group_by_commas(1000000) == '1,000,000'
assert group_by_commas(35235235) == '35,235,235'

思路2

def group_by_commas(n):
    return f'{n:,}'
    
assert group_by_commas(100) == '100'
assert group_by_commas(1000000) == '1,000,000'
assert group_by_commas(35235235) == '35,235,235'

思路3

def group_by_commas(n):
    return format(n,',')
    
assert group_by_commas(100) == '100'
assert group_by_commas(1000000) == '1,000,000'
assert group_by_commas(35235235) == '35,235,235'

@fwj 挑一个你喜欢的~~
:partying_face: :partying_face: :partying_face: :partying_face: :partying_face: :partying_face:

def group_by_commas(n):
    return "{:,}".format(n)

assert group_by_commas(100) == '100'
assert group_by_commas(1000000) == '1,000,000'
assert group_by_commas(35235235) == '35,235,235'

一个笨方法

def group_by_commas(n):
    return ','.join([str(n)[len(str(n)) - i - 3:len(str(n)) - i] for i in range(0, len(str(n)), 3)][::-1]) if len(
        str(n)) % 3 == 0 else str(n)[:len(str(n)) % 3] + ','.join(
        [str(n)[len(str(n)) - i - 3:len(str(n)) - i] for i in range(0, len(str(n)), 3)][::-1])


assert group_by_commas(100) == '100'
assert group_by_commas(1000000) == '1,000,000'
assert group_by_commas(35235235) == '35,235,235'
def group_by_commas(n):
    n_list = list(reversed(str(n)))
    result = []
    for i in range(0, len(n_list)):
        if i > 1 and (i + 1) % 3 == 0 and i + 1 < len(n_list):
            result.append(n_list[i])
            result.append(',')
        else:
            result.append(n_list[i])
    return ''.join(reversed(result))

assert group_by_commas(100) == '100'
assert group_by_commas(1000000) == '1,000,000'
assert group_by_commas(35235235) == '35,235,235'