【每日一题20220218】电话号码

:woman_mage:给定一个包含10个数字的列表,每个数字都在0~9之间,请编写一个函数,按照指定的电话号码格式,生成最终的字符串。注意括号后的空格。

格式: (xxx) xxx-xxxx

示例:
输入:[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],输出:“(123) 456-7890”。

题目难度:简单
题目来源:CodeWars:Create Phone Number

def solution(nums: list) -> str:
    # your code hrere

assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == "(123) 456-7890"
assert solution([0, 2, 3, 0, 5, 6, 0, 8, 9, 0]) == "(023) 056-0890"
assert solution([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) == "(000) 000-0000"
    return ''.join("("+str(nums[0])+str(nums[1])+str(nums[2])+")"+" "+str(nums[3])+str(nums[4])+str(nums[5])+"-"+str(nums[6])+str(nums[7])+str(nums[8])+str(nums[9]))

    return "({}{}{}) {}{}{}-{}{}{}{}".format(*nums)
def solution(nums: list) -> str:
    # your code hrere
    return '({}) {}-{}'.format(''.join(str(i) for i in nums[:3]), ''.join(str(i) for i in nums[3:6]),
                               ''.join(str(i) for i in nums[6:10]))


assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == "(123) 456-7890"
assert solution([0, 2, 3, 0, 5, 6, 0, 8, 9, 0]) == "(023) 056-0890"
assert solution([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) == "(000) 000-0000"
def solution(phone: list) -> str:
    phone1 = [str(i) for i in phone[:3]]
    phone2 = [str(i) for i in phone[3:6]]
    phone3 = [str(i) for i in phone[6:]]

    return f"({''.join(phone1)}) {''.join(phone2)}-{''.join(phone3)}"
def solution(nums: list) -> str:
    nums.insert(0, '(')
    nums.insert(4, ') ')
    nums.insert(8, '-')
    return ''.join([str(i) if type(i) == int else i for i in nums])


assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == "(123) 456-7890"
assert solution([0, 2, 3, 0, 5, 6, 0, 8, 9, 0]) == "(023) 056-0890"
assert solution([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) == "(000) 000-0000"
1 Like
def solution(nums: list) -> str:
    return "(" + ''.join([str(i) for i in nums[:3]]) + ") " + \
           ''.join([str(i) for i in nums[3:6]]) + "-" + \
           ''.join([str(i) for i in nums[6:]])


assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == "(123) 456-7890"
assert solution([0, 2, 3, 0, 5, 6, 0, 8, 9, 0]) == "(023) 056-0890"
assert solution([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) == "(000) 000-0000"
def solution(nums: list) -> str:
    # your code hrere
    nums_str = ""
    nums.insert(0, "(")
    nums.insert(4, ")")
    nums.insert(5, " ")
    nums.insert(9, "-")
    for i in nums:
        nums_str += nums_str.join(str(i))
    return nums_str


assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == "(123) 456-7890"
assert solution([0, 2, 3, 0, 5, 6, 0, 8, 9, 0]) == "(023) 056-0890"
assert solution([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) == "(000) 000-0000"
def solution(nums: list) -> str:
    #return "("+''.join(str(i) for i in nums[0:3])+") "+ ''.join(str(i) for i in nums[3:6])+"-" + ''.join(str(i) for i in nums[6:10])
    return f"({''.join(str(i) for i in nums[0:3])}) {''.join(str(i) for i in nums[3:6])}-{''.join(str(i) for i in nums[6:10])}"

assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == "(123) 456-7890"
assert solution([0, 2, 3, 0, 5, 6, 0, 8, 9, 0]) == "(023) 056-0890"
assert solution([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) == "(000) 000-0000"

暴力解

def solution(nums: list) -> str:
    return f'({nums[0]}{nums[1]}{nums[2]}) ' \
           f'{nums[3]}{nums[4]}{nums[5]}-' \
           f'{nums[6]}{nums[7]}{nums[8]}{nums[9]}'
    # your code hrere

assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == "(123) 456-7890"
assert solution([0, 2, 3, 0, 5, 6, 0, 8, 9, 0]) == "(023) 056-0890"
assert solution([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) == "(000) 000-0000"
public static String solution(Integer[] nums){
        return String.format("(%s%s%s) %s%s%s-%s%s%s%s",(Object[])nums);
    }
    public static void main(String[] args) {
        assert solution(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}).equals("(123) 456-7890");
        assert solution(new Integer[]{0, 2, 3, 0, 5, 6, 0, 8, 9, 0}).equals("(023) 056-0890");
        assert solution(new Integer[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}).equals("(000) 000-0000");
    }
def solution(nums: list) -> str:
    # 0218 电话号码
    res = '('+''.join(str(i) for i in nums[:3])+') '+''.join(str(i) for i in nums[3:6])+'-'+''.join(str(i) for i in nums[6:10])
    print(res)
    return res

assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == "(123) 456-7890"
assert solution([0, 2, 3, 0, 5, 6, 0, 8, 9, 0]) == "(023) 056-0890"
assert solution([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) == "(000) 000-0000"
    def solution(nums: list) -> str:
        s = ""
        for i in nums:
            s += str(i)
        return f"({s[0:3]}) {s[3:6]}-{s[6:]}"
def solution(nums) -> str:
    nums = [str(x) for x in nums]
    return f"({''.join(nums[:3])}) {''.join(nums[3:6])}-{''.join(nums[6:])}"
def solution(nums: list) -> str:
    str_nums = ''.join([str(nums[i]) for i in range(len(nums))])
    return "(" + str_nums[:3] + ") " + str_nums[3:6] + "-" + str_nums[6:]


assert solution([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) == "(123) 456-7890"
assert solution([0, 2, 3, 0, 5, 6, 0, 8, 9, 0]) == "(023) 056-0890"
assert solution([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) == "(000) 000-0000"