将给出字符串的问号转换成字符小写字母,使其满足回文要求 | python面试题笔试题 | python 技术论坛-江南app体育官方入口

请尝试在评论区里写下答案(如不能清楚表述,那么你可能没真正理解)。欢迎参与,为下一次求职做准备。

write a function solution that,given a string s of length n,returns any palindrome which can be obtained by replacing all of
the question marks in s by lowercase letters (‘a’-‘z).if no
palindrome can be obtained,the function should return the string
“no”.
a palindrome is a string that reads the same both forwards and
backwards.some examples of palindromes are:”kayak”,”radar”,
“mom”.
examples:
1.given s=”?ab??a”,the function should return “aabbaa”.
2.given s=”bab??a”,the function should return “no”.
3.given s=”?a?”,the function may return “aaa”.it may also
return “zaz”,among other possible answers.
tes
assume that:
·n is an integer within the range[1.1,000];
·string s consists only of lowercases letters(‘a’-
‘z)or’?.

讨论数量: 3

这是特斯拉面试题,我挂在这道题上了;发给小伙伴看看,如何大家之后碰到希望可以解决

1年前
s = input("please input the string:")
string_list = list(s)
n = len(string_list)
if n == 0:
    print("no")
mid = int(n/2)
for i in range(0, mid):
    if string_list[i] == string_list[n-1-i] or string_list[i] == "?" or string_list[n-1-i] == "?":
        continue
    else:
        print("no")
for m in range(0, n):
    if string_list[m] == "?":
        if string_list[n-1-m] == "?":
            string_list[m] = string_list[n-1-m] = 'z'
        else:
            string_list[m] = string_list[n-1-m]
print(''.join(string_list))

写的有点丑陋...我可能还没测全... 懒得改了 先午睡

1年前

这里人很少啊,只有一个题?

1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图