简答题
偏难
#6490
对称字符串 对称字符串是指从左往右读和从右往左读是一样的字符串。小方同学学习了递归思想后,利用递归思想来解决对称问题。在一串包含大小写字符和数字等字符组成的字符…
对称字符串
对称字符串是指从左往右读和从右往左读是一样的字符串。小方同学学习了递归思想后,利用递归思想来解决对称问题。在一串包含大小写字符和数字等字符组成的字符串中,他首先筛选出只包含大小写字符组成的字符串,然后判断该字符串是否是对称字符串。他编写的代码如下,请完善划线处的代码。
def getzf(yw): #筛选出原始字符串yw中大写小写字符组成的字符串
rst = ''
for i in yw:
if 'z'>=i >='a' or 'Z'>=i >='A':
①
return rst
def nxzf(data): #生成字符串data的逆序字符串
if len(data) == 1:
return data[0]
return ②
yw =input('请输入字符串')
yw = getzf(yw)
nxzfc = nxzf(yw)
if ③ :
print('原字符串中存在对称字符串',yw)
else:
print('不存在对称字符串')
答案与解析
正确答案:<pre class="brush:python;toolbar:false">def getzf(yw): #筛选出原始字符串yw中大写小写字符组成的字符串
rst = ''
for i in yw:
if 'z'>=i >='a' or 'Z'>=i >='A':
rst+=i
return rst
def nxzf(data): #生成字符串data的逆序字符串
if len(data) == 1:
return data[0]
return nxzf(data[1:])+data[0]
yw =input('请输入字符串')
yw = getzf(yw)
nxzfc = nxzf(yw)
if nxzfc == yw:
print('原字符串中存在对称字符串',yw)
else:
print('不存在对称字符串')</pre>
解析
评分标准:
(1)rst += i 或 rst= rst +i 或等效答案;(2分)
(2)nxzf(data[1:])+data[0] 或等效答案;(3分)
(3)nxzfc == yw 或等效答案。(3分)