0024-编程打卡:高级版定制回文联
本帖最后由 不二如是 于 2022-9-22 16:29 编辑**** Hidden Message *****
一星答案:
def Input_mix(shuru1,shuru2):
shuchu = ''
for k in range(len(shuru1)):
if shuru1 == shuru2[:k+1]:
j = (len(shuru1)-1-k)
k += 1
shuchu = shuru1[:j] + shuru2
return shuchu
def makePalindrome(shuru):
shuru1 = ''
for i in range(len(shuru)):
shuru1+= (shuru)
a2 = Input_mix(shuru1,shuru)
a1 = Input_mix(shuru,shuru1)
print("输出字符为:'",end = '')
if len(a1) > len(a2):
print(a2,end = '')
else:
print(a1,end = '')
print("'")
while 1:
Input = input("请输入字符:")
makePalindrome(Input)
二星答案:
def makePalindrome(s):
i = 0
while 1:
if s == s[::-1]:
axis = i
break
elif s[:-i] == s[:-i][::-1] and s[:i]:
axis = -i
break
i += 1
# 哪边短就往哪边加切片
out = s+s[:i][::-1] if axis>=0 else s[-i:][::-1]+s
return out
三星答案:
def makePalindrome(string):
for i in range(len(string), 1, -1):
if string[:i] == string:
return string[: i - 1: -1] + string
for i in range(-len(string), -1):
if string == string[:i - 1:-1]:
return string + string
return string + string[-2::-1]
基础语法:
https://www.bilibili.com/video/BV1c4411e77t
算法讲解:
https://www.bilibili.com/video/BV1HT4y1K7DY
Python3好好玩 {:10_270:} 本帖最后由 hveagle 于 2022-9-23 19:43 编辑
{:10_270:}{:10_294:}
正 0
正
正
正
正
正
正
正
正
正
一
51 {:5_108:}
页:
[1]