【每日一题20220401】回文数字 #1

:mage:‍ 回文是指一段数字或者文本,正着读和倒着读都是相同的,例如2002,110011都是回文数字。给定一个数字num,请编写一个函数,判断它是不是一个回文数字。

【示例】
输入:1221
输出:True
解释:1221倒过来依旧是1221,因此它是回文数字

题目难度:中等
题目来源:CodeWars-Numerical Palindrome #1

def solution(num: int)-> bool:
    # your code here

assert solution(1221) is True
assert solution(123322) is False
assert solution(2022) is False
def solution(num: int)-> bool:
    # your code here
    if num == int(str(num)[::-1]):
        return True
    else:
        return False

assert solution(1221) is True
assert solution(123322) is False
assert solution(2022) is False
assert solution(12321) is True
def solution(num: int) -> bool:
    return str(num) == str(num)[::-1]
    if str(num)==str(num)[::-1]:
        return True
    else:
        return False


assert solution(1221) is True
assert solution(123322) is False
assert solution(2022) is False

#通过字符串倒序求解
def solution(num: int)-> bool:
  num_r = int(str(num)[::-1])
  return num == num_r

#通过列表倒序求解
def solution(num: int)-> bool:
  lst =[i for i in str(num)][::-1]
  num_r = int(’’.join(lst))
  return num == num_r

def solution(num: int)-> bool:
    return str(num)[::-1]==str(num)


def solution(num):
result = True if str(num) == str(num)[::-1] else False
return result

assert solution(1221) is True
assert solution(123322) is False
assert solution(2022) is False
assert solution(“ahha”) is True
assert solution(“ahsdsha”) is False

#list方式
def solution(num: int)-> bool:
# your code here
num_list = [str(x) for x in str(num)]
num_list.reverse()
revnum = int("".join(num_list))

if num == revnum:
    return True
else:
    return False

assert solution(1221) is True
assert solution(123322) is False
assert solution(2022) is False


def solution(num: int)-> bool:
    a=int(str(num)[::-1])
    return True if a ==num else False
def palindrome(num):
    return str(num)[::-1] == str(num) if isinstance(num, int) and num>=0  else 'Not valid'
    def solution(num: int) -> bool:
        str_num = str(num)
        for i in range(len(str_num)//2):
            if str_num[i] != str_num[-i-1]:
                return False
        return  True
def solution(num: int) -> bool:
    nums_str = str(num)
    if nums_str == ''.join(reversed(nums_str)):
        return True
    else:
        return False

assert solution(1221) is True
assert solution(123322) is False
assert solution(2022) is False

image

def solution(num: int) -> bool:
    if num == int(str(num)[::-1]):
        return True
    else:
        return False


关闭