Python程序对介于!之间的字符进行计数!和2 ##

问题描述 投票:0回答:1

我对此问题有疑问:您有一个包含!,#和小字母a-z的字符串。您必须转义后面跟着的每个字符!并位于2#符号之间。 [将奇数#as(甚至#as视为)],您必须打印出将转义多少个字母。

输入格式

输入将是包含字符串序列的一行

约束

n = len(string_seq)1

输出格式

输出将是一个整数

Sample Input 0

#!a!b#c!d
Sample Output 0

2

Sample Input 1

##bc!a#
Sample Output 1

0

现在,此代码清除了9个测试用例中的6个,但是我无法理解如何使我尝试过许多方法的所有测试用例都变得清晰。我的怀疑是测试凯瑟斯可能是错的。有人可以帮我告诉我问题是错误的还是测试用例是错误的还是我的代码是错误的,如果我的代码是错误的,请帮助我进行纠正。谢谢。

def escape(str1):
counter=0
if(str1.count('!')%2==1):
    return '0'
for a,b in enumerate(str1):
    if(b=='#'):
        if(97<=ord(str1[a+1])<=122):
            if(str1.index('#')<a+1<len(str1)-str1[::-1].index('#')):
                counter+=1
return counter

str1 = input()打印(转义(str1))

python-3.x
1个回答
0
投票

如果此代码适用于您的所有情况,请告诉我,以便使我的解决方案变得更好。。

case='#h!a!h#gh#!d##ghj#'
cnt=0
temp=''
flag=False
for c in case:
    if c=='#':
        if flag:
            #print(temp)
            tl=temp.split('!')
            if len(tl)>1:
                #print(tl)
                #tl=list(filter(('').__ne__, tl))
                #print(tl)
                cnt+=len(tl)-1

            temp=''
            flag=False
        else:
            flag=True
    else:
        temp+=c
print(cnt)
© www.soinside.com 2019 - 2024. All rights reserved.