数字回文#4【难度:2级】:
答案1:
def palindrome(num
):
if type(num
) is not int or num
< 0:
return "Not valid"
else:
c
=0
for i
in range(num
,num
**2):
if is_pal
(i
):
return i
elif is_pal
(i
-c
):
return i
-c
else:
c
+=2
def is_pal(n
):
return n
> 10 and n
== int(str(n
)[::-1])
答案2:
is_palindrome
= lambda x
: x
> 10 and str(x
) == str(x
)[::-1]
def palindrome(num
):
if not isinstance(num
, int) or num
< 0:
return "Not valid"
smaller
, larger
= num
, num
while True:
checks
= is_palindrome
(smaller
), is_palindrome
(larger
)
if checks
== (True, False): return smaller
if checks
== (False, True): return larger
if checks
== (True, True): return larger
smaller
-= 1
larger
+= 1
答案3:
def is_palindrome(n
):
s
= str(n
)
return s
== s
[::-1]
def palindrome(num
):
if not isinstance(num
, int) or num
< 0:
return "Not valid"
if num
< 17:
return 11
inf
= next(n
for n
in range(num
, 0, -1) if is_palindrome
(n
))
sup
= next(n
for n
in range(num
, 2*num
) if is_palindrome
(n
))
mean
= (inf
+ sup
) / 2
return sup
if num
>= mean
else inf
答案4:
from itertools
import count
def palindrome(num
):
return next(c
for b
in count
(0) for c
in (num
+ b
, num
- b
) if c
> 9 and c
== int(str(c
)[::-1])) if isinstance(num
, int) and num
>= 0 else 'Not valid'
答案5:
def findR(num
):
while True:
if str(num
)==str(num
)[::-1]:
return num
num
+=1
def findL(num
):
while True:
if str(num
)==str(num
)[::-1]:
return num
num
-=1
def palindrome(num
):
if type(num
)!=int or num
<0:
return 'Not valid'
if num
<=10:
return 11
else:
if findR
(num
)-num
<=num
-findL
(num
):
return findR
(num
)
else:
return findL
(num
)
答案6:
def is_pali(num
):
return str(num
) == str(num
)[::-1] and num
> 9
def palindrome(num
):
if type(num
) != int or str(num
) != str(int(num
)) or int(num
) < 0:
return 'Not valid'
n
= 0
if num
<= 11:
return 11
while n
< num
:
test1
= num
- n
test2
= num
+ n
if is_pali
(test2
):
return num
+ n
elif is_pali
(test1
):
return num
- n
n
+= 1
答案7:
""" Solution without loops ! """
""" Define the first segment of what wwould be a "not far away palindrome" based on "num" :
num = 1258 -> midPart = 12 ( -> would give "1221" )
num = 459 -> midPart = 45 ( -> would give "454" )
Check for lower and upper values (in case num is near of change of decade, upperbond or lowerbond)
and define 3 possible parts of palindromes:
num = 1258 -> 12 -> lowPart = 11
midPart = 12
highPart = 13
num = 459 -> 45 -> lowPart = 44
midPart = 45
highPart = 46
Construct palindromes according to each part an the parity of the length of the original number:
num = 1258 -> lPal = 1111 ; mPal = 1221 ; hPal = 1331
num = 459 -> lPal = 444 ; mPal = 454 ; hPal = 464
Sort the result with tuples defined as: (abs(num-newPal), -newPal)
This way, the nearest new palindrome value are first, and if two values of "abs(num-newPal)"
are equal, the second element of the tuple ensure to find the highest one as first element of
the sorted list of tuples.
return the opposite of the second element of the first tuple, which is so "newPal".
Edge cases: num = 1999 -> lPal = 1881 ; mPal = 1991 ; hPal = 2002 => return 2002
num = 801 -> lPal = 797 ; mPal = 808 ; hPal = 818 => return 797
num = 1002 -> lPal = 99(!); mPal = 1001 ; hPal = 1111 => return 1001
"""
def nextPal(sPart
, ls
): return int( sPart
+ sPart
[:len(sPart
)-ls
%2][::-1] )
def palindrome(num
):
if type(num
) != int or num
<= 0: return "Not valid"
s
= str(num
)
midPart
= s
[:(len(s
)+1)//2]
lowPart
= str(int(midPart
)-1)
highPart
= str(int(midPart
)+1)
lPal
, mPal
, hPal
= nextPal
(lowPart
, len(s
)), nextPal
(midPart
, len(s
)), nextPal
(highPart
, len(s
))
return 11 if num
<= 16 else -sorted( (abs(num
-pal
), -pal
) for pal
in [lPal
, mPal
, hPal
] )[0][1]
答案8:
def is_palindrome(num
):
if num
< 10:
return False
s
= str(num
)
return s
[0:len(s
)//2] == s
[len(s
)-1:len(s
)-len(s
)//2-1:-1]
def palindrome(num
):
if type(num
) is not int or num
< 0:
return 'Not valid'
if is_palindrome
(num
):
return num
n
= 1
while True:
if is_palindrome
(num
+ n
):
return num
+ n
elif is_palindrome
(num
- n
):
return num
- n
n
+= 1
答案9:
def find_palindrome(num
, step
= -1):
while num
> 10:
if str(num
) == str(num
)[::-1]:
return num
num
+= step
return -1
def palindrome(num
):
if not isinstance(num
, int) or num
<= 0:
return 'Not valid'
if num
< 10:
return 11
s
= str(num
)
if s
== s
[::-1]:
return num
top
= find_palindrome
(num
, 1)
down
= find_palindrome
(num
, -1)
if top
- num
<= num
- down
:
return top
return down
答案10:
def is_pal(s
): return s
==s
[::-1] and len(s
)>1
def palindrome(n
):
if not isinstance(n
,int) or n
<0: return "Not valid"
if is_pal
(str(n
)): return n
i
=1
while True:
if is_pal
(str(n
+i
)): return n
+i
if is_pal
(str(n
-i
)): return n
-i
i
+=1
景越Python基础训练营QQ群
欢迎各位同学加群讨论,一起学习,共同成长!
转载请注明原文地址: https://mac.8miu.com/read-512206.html