【每日一题20220407】回文数字 #3

:mage:‍ 回文是指一段数字或者文本,正着读和倒着读都是相同的,例如2002,110011都是回文数字。 请编写一个函数,接收一个数字参数num,判断num中所有数字能够组成回文数字的总个数。个位数字不被视为回文数字。

【示例】
输入:1221
输出:2
解释:数字1221中,1221整体是回文数字,22也是回文数字,因此总个数为2。

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

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

assert solution(5) == 0
assert solution(1221) == 2
assert solution(141221001) == 5
assert solution(1294) == 0
def solution(num: int)-> int:
    if not isinstance(num, int) or num < 0:
        return 'Not valid'

    nums = str(num)
    count = 0
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            if nums[i: j + 1] == nums[i: j + 1][::-1]:
                count += 1
    return count
def solution(num: int)-> int:
    # your code here
    num_len = len(str(num))
    res_list = []
    if num_len <= 1:
        return 0
    for i in range(num_len):
        for j in range(i + 2, num_len + 1):
            if str(num)[i:j] == str(num)[i:j][::-1]:
                res_list.append(str(num)[i:j])
    print(res_list)
    return len(res_list)

assert solution(5) == 0
assert solution(1221) == 2
assert solution(141221001) == 5
assert solution(1294) == 0
def solution(num: int) -> int:
    if len(str(num)) <= 1:
        return 0
    else:

        len_num = len([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]])
        if len_num >= 1:
            return len_num
        else:
            return 0


assert solution(5) == 0
assert solution(1221) == 2
assert solution(141221001) == 5
assert solution(1294) == 0

def solution(num: int) -> bool:
    num_s = str(num)
    num_len = len(num_s)
    cnt=0
    if num > 10:
        for i in range(0, num_len):
            min_len = min(i, num_len - i)
            for j in range(0, min_len + 1):
                if num_s[i - j - 1:i] == num_s[i:i + j + 1][::-1] or num_s[i - j - 1:i] == num_s[i + 1:i + j + 2][::-1]:
                    cnt+=1

    return cnt



def solution2(num: int)-> bool:
    num_len = len(str(num))
    if num_len <=1:
        return 0
    else:
        s=[True for i in range(num_len) for j in range(i+2,num_len+1)  if str(num)[i:j]== str(num)[i:j][::-1]  ]
       
        return len(s)

def solution(num: int)->int:
    num= str(num)
    count = 0
    if len(num) < 2:
        return count
    else:
        for i in range(len(num)):
            for j in range(i+1,len(num)):
                if num[i:1+j] == num[i:1+j][::-1]:
                    count += 1
        return count 

              

def solution_2(num:int)->int:
    s = str(num)
    n =0
    if num >10:
        for i in range(len(s)):
            for j in range(i+2,len(s)+1):
                s1 = s[i:j]
                if s1 == s1[::-1]:
                    n = n+1
    return n

def solution(num: int) -> bool:
    result = []
    length = len(str(num))
    for i in range(length):
        for j in range(length - i):
            ele = str(num)[j:j + i + 1]
            if len(ele) <= 1:
                pass
                # print(f"个位数字{str(num)[j:j + i + 1]}不被视为回文数字。")
            elif ele == ele[::-1]:
                result.append(ele)
    return len(result)


assert solution(5) == 0
assert solution(1221) == 2
assert solution(141221001) == 5
assert solution(1294) == 0
def fun(num: str) -> bool:
    if num == ''.join(reversed(num)) and len(num) > 1:
        return True
    else:
        return False


def solution(num: int) -> int:
    l = len(str(num))
    result = 0
    str_num = str(num)
    for i in range(l - 1):
        for j in range(i + 1, l):
            if fun(str_num[i:j + 1]):
                result += 1
    return result


assert solution(5) == 0
assert solution(1221) == 2
assert solution(141221001) == 5
assert solution(1294) == 0
def solution(num: int) -> int:
    # your code here
    num_str = str(num)
    count = 0
    for i in range(len(num_str) - 1):
        # print(i)
        for j in range(i+1, len(num_str)):
            # print(j)
            if num_str[i:j+1] == num_str[i:j+1][::-1]:
                count += 1
    return count


assert solution(5) == 0
assert solution(1221) == 2
assert solution(141221001) == 5
assert solution(1294) == 0

image

def solution(num: int) -> int:
    if len(str(num)) == 1:
        return 0
    else:
        n = 0
        for i in range(len(str(num))):
            for j in range(i + 1, len(str(num))):
                if str(num)[i:j + 1] == str(num)[i:j + 1][::-1]:
                    n += 1
        return n


assert solution(5) == 0
assert solution(1221) == 2
assert solution(141221001) == 5
assert solution(1294) == 0