【每日一题1231】首音互换

:woman_mage: 给定一个包含两个正数的列表nums,请编写一个函数,将它们首音互换,并且计算他们能之间的差值的绝对值。

备注:首音互换是指将两个对象的第一个元素进行对换。例如 123456 ,互换后变成 423156

示例
输入: [123, 456],输出: 267。因为 423 - 156 = 267

题目难度:简单
题目来源:CodeWars: Noonerize Me

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

assert solution([12, 34]) == 18
assert solution([55, 63]) == 12
assert solution([357, 579]) == 178
assert solution([1, 1]) == 0
def solution(nums: list) -> int:
    return abs(int(str(nums[0])[0] + str(nums[1])[1:]) - int(str(nums[1])[0] + str(nums[0])[1:]))


assert solution([12, 34]) == 18
assert solution([55, 63]) == 12
assert solution([357, 579]) == 178
assert solution([1, 1]) == 0
1 Like
def solution(nums):
    num1 = int(str(nums[0])[0] + str(nums[1])[1:])
    num2 = int(str(nums[1])[0] + str(nums[0])[1:])
    if num1 <= num2:
        return num2 - num1
    else:
        return num1 - num2

assert solution([12, 34]) == 18
assert solution([55, 63]) == 12
assert solution([357, 579]) == 178
assert solution([1, 1]) == 0
def solution(nums: list) -> int:
    if len(nums) != 2: return -1
    sn1,sn2 = str(nums[0]),str(nums[1])
    return abs(int(sn2[0:1]+sn1[1:])-int(sn1[0:1]+sn2[1:]))
def solution(nums: list) -> int:
    nums = [int(str(nums[1])[0] + str(nums[0])[1:]), int(str(nums[0])[1] + str(nums[1])[1:])]
    return abs(nums[1] - nums[0])

    assert solution([12, 34]) == 18
    assert solution([55, 63]) == 12
    assert solution([357, 579]) == 178
    assert solution([1, 1]) == 0
def solution13(nums:list):
    nums_1=[i for i in str(nums[0])]
    nums_2=[i for i in str(nums[1])]
    nums_1[0],nums_2[0]=nums_2[0],nums_1[0]
    int1=''.join(nums_1)
    int2=''.join(nums_2)
    return abs((int(int1)-int(int2)))
assert solution13([12, 34]) == 18
assert solution13([55, 63]) == 12
assert solution13([357, 579]) == 178
assert solution13([1, 1]) == 0
def solution(nums: list) -> int:
    # your code

    i = abs(int(str(nums[0])[0] + str(nums[1])[1:]) - int(str(nums[1])[0] + str(nums[0])[1:]))
    return i


assert solution([12, 34]) == 18
assert solution([55, 63]) == 12
assert solution([357, 579]) == 178
assert solution([1, 1]) == 0