马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 不二如是 于 2022-9-22 16:29 编辑
一星答案:
def Input_mix(shuru1,shuru2):
shuchu = ''
for k in range(len(shuru1)):
if shuru1[len(shuru1)-1-k:] == 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[len(shuru)-1-i:len(shuru)-i])
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[i:] == s[i:][::-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[i - 1::-1]:
return string[: i - 1: -1] + string
for i in range(-len(string), -1):
if string[i:] == string[:i - 1:-1]:
return string + string[i - 1::-1]
return string + string[-2::-1]
基础语法:
算法讲解:
|