|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
假如我们的字符串是 'ABCaABCbABCcABCd' 的时候,按照答案的代码,不会打印 'b',代码如下:
- str1 = '''ABCaABCbABCcABCd'''
- c1 = 0
- c2 = 0
- c3 = 0
- lenth = len(str1)
- for i in range(lenth):
- if str1[i] == '\n':
- continue
- if str1[i].isupper():
- if c2 == 1:
- c3 += 1
- c1 = 0
- else:
- c1 += 1
- continue
- if str1[i].islower() and c1 == 3:
- c2 = 1
- c1 = 0
- t = i
- continue
- if str1[i].islower() and c3 == 3:
- print(str1[t],end='')
- c1 = 0
- c2 = 0
- c3 = 0
复制代码
i=0时,大写,此时c1=1,c2=0,c3=0
i=1时,大写,此时c1=2,c2=0,c3=0
i=2时,大写,此时c1=3,c2=0,c3=0
i=3时,小写,此时c1=0,c2=1,c3=0,t=3
i=4时,大写,此时c1=0,c2=1,c3=1
i=5时,大写,此时c1=0,c2=1,c3=2
i=6时,大写,此时c1=0,c2=1,c3=3
i=7时,小写,此时c1=0,c2=1,c3=3,所以打印a,然后重置c1=0,c2=0,c3=0
以此重复,当i=11时和i=7时的情况一样,理所当然会打印t=11时的字符,也就是'c',这样就跳过了t=7时的字符也就是'b'
有没有什么解决的办法,我想了好久没想明白怎么改代码
- str1 = '''ABCaABCbABCcABCd'''
- c1 = 0
- c2 = 0
- c3 = 0
- lenth = len(str1)
- for i in range(lenth):
- if str1[i] == '\n':
- continue
- if str1[i].isupper():
- if c2 == 1:
- c3 += 1
- c1 = 0
- else:
- c1 += 1
- continue
- if str1[i].islower() and (c1 == 3):
- c2 = 1
- c1 = 0
- t = i
- continue
- if str1[i].islower() and (c3 == 3):
- print(str1[t],end='')
- t = i #
- c1 = 0 #<---改这里
- c2 = 1 #
- c3 = 0 #
- else: #
- c1 = 0
- c2 = 0
- c3 = 0
复制代码
|
|