【每日一题20220412】回文数字 #5

:mage:‍ 回文是指一段数字或者文本,正着读和倒着读都是相同的,例如2002110011都是回文数字。 请编写一个函数,接收一个数字参数num,判断该数字经过重新组合后是否可以变成一个回文数字。 个位数字(0~9)不被视为回文数字。

【示例】
输入:2121
输出:True
解释:数字2121经过重组成1221,它是一个回文数字,因此返回True

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

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

assert solution(1331) is True
assert solution(2121) is True
assert solution(3357665) is True
assert solution(13245) is False
solution(num: int)-> bool:
    s = str(num)

    if not isinstance(num, int) or num < 0:
        return "Not valid"

    if num > 9 and sum(s.count(x) % 2 for x in set(s)) < 2:
        return True
    else:
        return False
1 个赞

def solution(n):
if n<11:
return False
s=str(n)
times=0
for i in list(set(s)):
If s.count(i)!=0:
times+=1
if times >1:
return False
return True

def solution(num: int)-> bool:
    # your code here
    if num < 10:
        return False
    else:
        l = []
        for i in set(str(num)):
            if str(num).count(i) % 2 != 0 and len(str(num)) % 2 == 0:    
                return False
            if str(num).count(i) % 2 != 0 and len(str(num)) % 2 == 1:
                l.append(i)
                if len(l) > 1:
                    return False
        else:
            return True


assert solution(1331) is True
assert solution(2121) is True
assert solution(3357665) is True
assert solution(13245) is False
def solution(num: int) -> bool:
    if len(str(num)) <= 1:
        return False
    else:
        list_true = [True for i in range(len(str(num))) for j in range(i + 2, len(str(num)) + 1) if
                     str(num)[i:j] == str(num)[i:j][::-1] and str(num)[i:j][0] != '0' and str(num)[i:j][
                         -1] != '0']
        if list_true != []:
            return True
        else:
            return False


assert solution(1331) is True
assert solution(2121) is True
assert solution(3357665) is True
assert solution(13245) is False
# Daily_20220412
def solution(num: int)-> bool:
    # your code here
    if isinstance(num, int) and num > 0:
        if num <= 10:
            return False
        else:
            num_list = list(str(num))
            repeat_num = set()
            for i in range(0, len(num_list)):
                if num_list[i] in num_list[i + 1:]:
                    repeat_num.add(num_list[i])
            if len(set(num_list).difference(repeat_num)) <= 1:
                return True
            else:
                return False
    else:
        return "Not valid"


assert solution(1331) is True
assert solution(2121) is True
assert solution(3357665) is True
assert solution(13245) is False
assert solution(15577666) is False

这种方式还存在bug,晚点重写下。

import itertools
def solution(num: int)-> bool:
    if num < 10:
        return False
    else:
        for i in set(''.join(i) for i in itertools.permutations(str(num))):
            if i == i[::-1]:
                return True
        return False

做此题的时,竟感到一丝凄凉

def solution(num: int) -> bool:
    if not isinstance(num, int) or num < 10:
        return False

    # 抓单身🐶啦 T_T,多于一个就报警
    count_list = [(str(num).count(n)) % 2 for n in set(str(num))]
    return True if count_list.count(1) <= 1 else False
1 个赞
def solution(num: int)-> bool:
    # your code here
    num = str(num)
    a = 0
    b = 0
    if len(num) == 1:
        return False
    if int(num) == int(num[::-1]):
        return True
    for i in set(num):
        if num.count(i) % 2 == 0:
            a = a + 2
        elif num.count(i) % 2 == 1:
            b = b + 1
    if a != 0 and b == 0:
        return True
    elif a == 0 and b != 0:
        return False
    elif a % 2 == 0 and b % 2 ==1:
        return True
    else:
        return False


assert solution(1331) is True
assert solution(2121) is True
assert solution(3357665) is True
assert solution(13245) is False

from itertools import permutations


def fun(num: str) -> bool:
    if num == ''.join(reversed(num)) and len(num) > 1:
        return True
    else:
        return False


def solution(num: int) -> bool:
    num_list = list(str(num))
    perm = permutations(num_list)
    for x in perm:
        if fun(''.join(x)):
            return True
    return False


assert solution(1331) is True
assert solution(2121) is True
assert solution(3357665) is True
assert solution(13245) is False
def solution(num: int) -> bool:
    num_list = list(str(num))
    num_set = set(num_list)
    n = 0
    for i in num_set:
        if num_list.count(i) % 2 != 0:
            n += 1
    if n > 1:
        return False
    else:
        return True


assert solution(1331) is True
assert solution(2121) is True
assert solution(3357665) is True
assert solution(13245) is False