【每日一题20220411】回文数字 #4

:mage:‍ 回文是指一段数字或者文本,正着读和倒着读都是相同的,例如2002110011都是回文数字。 请编写一个函数,接收一个数字参数num,返回最靠近num的一个回文数字,数值比num小一点或者大一点都可以。 如果存在多个可能的值,则返回其中较大的数字;如果数字本身就是回文数字,直接返回自身。 个位数字(0~9)不被视为回文数字。

【示例】
输入:1029
输出:1001
解释:数字1029附近的回文数字分别有10011111,但是1001更接近1029,因此返回1001

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

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

assert solution(8) == 11
assert solution(281) == 282
assert solution(1029) == 1001
assert solution(1221) == 1221
def solution(num: int)-> int:
    # your code here
    if num > 0 and num <= 10:
        return 11
    if str(num) == str(num)[::-1]:
        return num
    # 寻找小于num的回文数字
    res_list1 = []
    res_list2 = []
    close_minnum = None
    close_maxnum = None
    diff_num = None
    for i in range(11,num):
        if str(i) == str(i)[::-1]:
            res_list1.append(i)
    # 取小于num的回文数字中最大的一个数,并计算差值
    close_minnum = max(res_list1)
    diff_num = num - close_minnum
    # 寻找num + 差值之间是否还有回文,有则返回其最小值,没有则返回close_minnum
    for i in range(num+1,num+diff_num+1):
        if str(i) == str(i)[::-1]:
            res_list2.append(i)
    if res_list2 == []:
        return close_minnum
    else:
        close_maxnum = min(res_list2)
        return close_maxnum



assert solution(8) == 11
assert solution(281) == 282
assert solution(1029) == 1001
assert solution(1221) == 1221

def solution(n:int):
if n<11:
return 11
I=0
while n>10:
If str(n-I) ==str(n-I)[::-1]:
return n-I
elif str(n+1)==str(n+1)[::-1]:
return n+1
I+=1

def solution(num: int)-> int:
    # your code here
    if num < 10 :
        return 11
    else :
        num1 = num
        num2 = num
        result = [] 
        while not result:
            if str(num) == str(num)[::-1] :
                result.append(num)
            if str(num1) == str(num1)[::-1] :
                result.append(num1)
            if str(num2) == str(num2)[::-1] :
                result.append(num2)
            if result:
                return sorted(result)[-1]
            num1 += 1
            num2 -= 1


assert solution(8) == 11
assert solution(281) == 282
assert solution(1029) == 1001
assert solution(1221) == 1221
2 个赞

   def solution(num: int)-> int:
    # your code here
    num = str(num)
    if num == num[::-1] and int(num) > 10:
        return int(num)
    i = int(num) + 1
    while str(i) != str(i)[::-1] or i < 10:
        i = i + 1
    j = int(num) - 1
    while str(j) != str(j)[::-1]:
        j = j - 1
        if j < 10:
            break
    if i-int(num) > int(num)-j and j > 10:
        return j
    else:
        return i


assert solution(8) == 11
assert solution(281) == 282
assert solution(1029) == 1001
assert solution(1221) == 1221
def solution(num: int) -> int:
    # your code here
    if num < 10:
        return 11
    elif str(num) == str(num)[::-1]:
        return num
    else:
        result = []
        n = 1
        while n:
            if str(num + n) == str(num + n)[::-1]:
                result.append(num + n)
            if str(num - n) == str(num - n)[::-1]:
                result.append(num - n)
            if result:
                return sorted(result)[-1]
            n += 1


assert solution(8) == 11
assert solution(281) == 282
assert solution(1029) == 1001
assert solution(1221) == 1221

def solution(num: int) -> int:
    if num <= 16:
        return 11
    add = reduce = num
    while True:
        if str(add) == str(add)[::-1]:
            return add
        if reduce > 9 and str(reduce) == str(reduce)[::-1]:
            return reduce
        add += 1
        reduce -= 1

def solution(num: int) -> int:
    if 0 < num <= 10:
        return 11
    elif str(num) == str(num)[::-1]:
        return num
    else:
        close_num1 = max([n for n in range(11, num) if str(n) == str(n)[::-1]])
        dif_num = num - close_num1

        if [n for n in range(num, num + dif_num + 1) if str(n) == str(n)[::-1]] == []:
            return close_num1
        else:
            return min([n for n in range(num, num + dif_num + 1) if str(n) == str(n)[::-1]])


assert solution(8) == 11
assert solution(281) == 282
assert solution(1029) == 1001
assert solution(1221) == 1221
# Daily_20220411
def solution(num: int)-> int:
    # your code here
    if isinstance(num, int):
        if 0 < num <= 10:
            return 11
        elif str(num) == str(num)[::-1]:
            return num
        else:
            max_result = []
            min_result = []
            for i in range(11, num+1):
                if str(i) == str(i)[::-1]:
                    max_result.append(i)
            max_num = max(max_result)
            diff_max_num = num - max_num
            for i in range(num+1, diff_max_num+(num+1)):
                if str(i) == str(i)[::-1]:
                    min_result.append(i)
            if not min_result or min(min_result) - num > diff_max_num:
                return max_num
            else:
                return min(min_result)
    else:
        print('input error.')


assert solution(8) == 11
assert solution(281) == 282
assert solution(1029) == 1001
assert solution(1221) == 1221
def solution(num: int)-> int:
    maybe_value=[]
    count = 0
    while True:
        if len(str(num-count)) >1 and str(num-count) == str(num-count)[::-1]:
            maybe_value.append(num-count)
        if len(str(num+count)) >1 and  str(num+count) == str(num+count)[::-1]:
            maybe_value.append(num+count)
        if maybe_value:
            return sorted(maybe_value)[-1]
        count = count +1
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;


public class Palindrome {

    @Test
    void demoTest() {
        assertAll(
                ()-> assertEquals(palindrome(8),11),
                ()-> assertEquals(palindrome(281),282),
                ()-> assertEquals(palindrome(1029),1001),
                ()-> assertEquals(palindrome(10293456),10288201),
                ()-> assertEquals(palindrome(1221),1221)
                );
    }


    public int palindrome(int number) {
        String numberOf = number + "";
        String reverse = new StringBuilder(numberOf).reverse().toString();
        int reversed;
        if (number > 0 && number < 10) {
            return 11;
        } else if (reverse.equals(numberOf)) {
            return number;
        } else {
            int maxReverse =0;
            int minReverse =0;
            for (int i = number;i>10;i--){
                String ans1 = i + "";
                String reverse1= new StringBuffer(ans1).reverse().toString();
                if (ans1.equals(reverse1)){
                    minReverse=i;
                    break;
                }

            }
            for (int s = number;;s++){
                String ans2 = s + "";
                String reverse2 = new StringBuffer(ans2).reverse().toString();
                if (ans2.equals(reverse2)){
                    maxReverse =s;
                    break;
                }
            }
            if (maxReverse-number>number-minReverse){
                reversed=minReverse;
            }else {
                reversed=maxReverse;
            }
            return reversed;
        }
    }
}
def solution(num: int) -> int:
    if num <= 11:
        return 11

    step = 0
    while True:
        left = str(num - step)
        right = str(num + step)
        
        if right == right[::-1]:
            return int(right)
        if left == left[::-1] and int(left) > 9:
            return int(left)
        step += 1  # 齐步走~
def fun(num: str) -> bool:
    if num == ''.join(reversed(num)) and len(num) > 1:
        return True
    else:
        return False


def solution(num: int) -> int:
    if num < 10:
        return 11
    elif fun(str(num)):
        return num
    else:
        num_max = num + 1
        num_min = num - 1
        while True:
            if fun(str(num_max)):
                return num_max
            if fun(str(num_min)):
                return num_min
            num_max += 1
            num_min -= 1


assert solution(8) == 11
assert solution(281) == 282
assert solution(1029) == 1001
assert solution(1221) == 1221
def solution(num: int)-> int:
    if num<10:
        return 11
    else:
        min_num=0
        for i in range(num,10,-1):
            if str(i)==str(i)[::-1]:
                min_num=i
                break
        max_num=num
        while True:
            max_num+=1
            if str(max_num)==str(max_num)[::-1]:
                break
        num_list=[min_num,max_num]
        if num-num_list[0]>max_num-num:
            return max_num
        elif num-num_list[0]<max_num-num:
            return min_num


assert solution(8) == 11
assert solution(281) == 282
assert solution(1029) == 1001
assert solution(1221) == 1221