【每日一题20220408】回文数字 #3.5

:mage:‍ 回文是指一段数字或者文本,正着读和倒着读都是相同的,例如2002,110011都是回文数字。 请编写一个函数,接收一个数字参数num,返回num中所有数字能够组成回文数字的列表,按升序排列,不允许重复。 个位数字和前后是0的数字(010和00)不被视为回文数字。如果不满足条件则返回空列表。

【示例】
输入:1221
输出:[22, 1221]
解释:数字1221中,1221整体是回文数字,22也是回文数字,因此按升序排列后为[22, 1221]。

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

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

assert solution(1221) == [22, 1221]
assert solution(34322122) == [22, 212, 343, 22122]
assert solution(1001331) == [33, 1001, 1331]
assert solution(13598) == []

image

def solution(num: int)->list:
    num= str(num)
    result = list()
    if len(num) < 2:
        return result
    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] and int(num[i]) != 0:
                    result.append(int(num[i:1+j]))
        return sorted(list(set(result)))

def solution(num: int) -> list:
    if len(str(num)) <= 1:
        return []
    else:
        list_num = sorted([int(str(num)[i:j]) 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'])
        return sorted(set(list_num), key=list_num.index)


assert solution(1221) == [22, 1221]
assert solution(34322122) == [22, 212, 343, 22122]
assert solution(1001331) == [33, 1001, 1331]
assert solution(13598) == []
def solution(num: int) -> list:
   #把num转为字符串
   nums = str(num)
   #新建一个空列表
   list_num = []
   #依次循环切片取出值,判断是否是回文数字
   for i in range(len(nums)):
      for j in range(i+1, len(nums)):
         if nums[i:j+1] == nums[i:j+1][::-1]:
            #如果切片的值不为0
            if nums[i:j+1]!= '0':
               #如果切片的值首尾都不等于0
               if nums[i:j+1][0] != '0' and nums[i:j+1][-1] != '0':
                  list_num.append(int(nums[i:j + 1]))
   #利用内置函数set去掉列表中的重复元素,然后使用内置函数sorted进行排序
   return sorted(set(list_num))

assert solution(1221) == [22, 1221]
assert solution(34322122) == [22, 212, 343, 22122]
assert solution(1001331) == [33, 1001, 1331]
assert solution(13598) == []
def solution(num: int)-> list:
    # your code here
    num_str = str(num)
    num_list = []
    for i in range(len(num_str)-1):
        for j in range(i + 2, len(num_str) + 1):
            num_str1 = num_str[i:j]
            if num_str1 == num_str1[::-1] and num_str1[0] != '0':
                if int(num_str[i:j]) not in num_list:
                    num_list.append(int(num_str[i:j]))
    num_list.sort()
    return num_list


assert solution(1221) == [22, 1221]
assert solution(34322122) == [22, 212, 343, 22122]
assert solution(1001331) == [33, 1001, 1331]
assert solution(13598) == []
def solution(num: int)-> list:
    # your code here
    num_str = str(num)
    num_list = [int(num_str[i:j+1]) for i in range(len(num_str)-1) for j in range(i + 1, len(num_str))
    if num_str[i] == num_str[j] and num_str[i] != '0' and num_str[i:j+1] == num_str[i:j+1][::-1]]
    return sorted(set(num_list))


assert solution(1221) == [22, 1221]
assert solution(34322122) == [22, 212, 343, 22122]
assert solution(1001331) == [33, 1001, 1331]
assert solution(13598) == []
assert solution(1) == []
def solution(num: int)-> list:
    # your code here
    num_len = len(str(num))
    res_list = []
    if num_len <= 1:
        return []
    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] and not str(num)[i:j].startswith("0"):
                res_list.append(str(num)[i:j])
    res_list = sorted(list(set(int(item) for item in res_list)))
    print(res_list)
    return res_list

assert solution(1221) == [22, 1221]
assert solution(34322122) == [22, 212, 343, 22122]
assert solution(1001331) == [33, 1001, 1331]
assert solution(13598) == []

@1402775654_8805 @jhf917 @ninixiaoye @joyoko @1208634117_0015

同学们好!

为了方便大家的切磋代码,建议贴代码的时候使用markdown格式提交源码哈。

操作很简单,只需要在代码前后加上一点标记:

```python
题解代码
```

:tada::tada::tada:

def solution(num)-> list:
    if not isinstance(num,int) or num < 0:
        return 'Not valid'

    nums = str(num)
    result = []
    for i in range(len(nums)):
        for j in range(i,len(nums)):
            res = nums[i:j+1]
            if str(int(res)) == res and len(res) >= 2 and res[::-1] == res:
                if int(res) not in result:
                    result.append(int(res))
    result.sort()
    return result

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


def solution(num: int) -> list:
    l = len(str(num))
    result = []
    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.append(int(str_num[i:j + 1]))
    result2 = list(set(result))
    result2.sort()
    return result2


assert solution(1221) == [22, 1221]
assert solution(34322122) == [22, 212, 343, 22122]
assert solution(1001331) == [33, 1001, 1331]
assert solution(13598) == []

solution(1221) == [22, 1221] #为什么2112不是回文数字
solution(34322122) == [22, 212, 343, 22122] #为什么33不是回文数字

是否题目少了一个条件:返回的结果中,必须是参数num中连续的数字

def solution(num: int)-> list:
    res_list=[]
    num_str=str(num)
    left_step=0
    while left_step<len(num_str):
        for right_step in range(left_step+2,len(num_str)+1):
            if num_str[left_step:right_step]==''.join(reversed(num_str[left_step:right_step])):
                if num_str[left_step:right_step][0]=='0' or num_str[left_step:right_step][-1]=='0':
                    continue
                else:
                    res_list.append(int(num_str[left_step:right_step]))
            else:
                continue
        left_step+=1
    res=list(set(res_list))
    res.sort()
    return res

assert solution(1221) == [22, 1221]
assert solution(34322122) == [22, 212, 343, 22122]
assert solution(1001331) == [33, 1001, 1331]
assert solution(13598) == []