疑问
if i+1 != length and str1.isupper():这句,i+1!=length可以不写吗,这是不是防止小写字母是最后一个会出现countA=3,countC=3但是小写字母是最后一个的这种情况吗
str1 = '''ABSaDKSbRIHcRHGcdDIF'''
countA = 0# 统计前边的大写字母
countB = 0# 统计小写字母
countC = 0# 统计后边的大写字母
length = len(str1)
for i in range(length):
if str1 == '\n':
continue
"""
|如果str1是大写字母:
|-- 如果已经出现小写字母:
|-- -- 统计后边的大写字母
|-- 如果未出现小写字母:
|-- -- 清空后边大写字母的统计
|-- -- 统计前边的大写字母
"""
if str1.isupper():
if countB:
countC += 1
else:
countC = 0
countA += 1
"""
|如果str1是小写字母:
|-- 如果小写字母前边不是三个大写字母(不符合条件):
|-- -- 清空所有记录,重新统计
|-- 如果小写字母前边是三个大写字母(符合条件):
|-- -- 如果已经存在小写字母:
|-- -- -- 清空所有记录,重新统计(出现两个小写字母)
|-- -- 如果该小写字母是唯一的:
|-- -- -- countB记录出现小写字母,准备开始统计countC
"""
if str1.islower():
if countA != 3:
countA = 0
countB = 0
countC = 0
else:
if countB:
countA = 0
countB = 0
countC = 0
else:
countB = 1
countC = 0
target = i
"""
|如果前边和后边都是三个大写字母:
|-- 如果后边第四个字母也是大写字母(不符合条件):
|-- -- 清空记录B和C,重新统计
|-- 如果后边仅有三个大写字母(符合所有条件):
|-- -- 打印结果,并清空所有记录,进入下一轮统计
"""
if countA == 3 and countC == 3:
if i+1 != length and str1.isupper():
countB = 0
countC = 0
else:
print(str1, end='')
countA = 3
countB = 0
countC = 0 i+1 != length,意思是说不是最后一个字符,如果不加这句,最后一组符合(countA == 3 and countC == 3)时就会out of range了,现在的例子不会出问题,因为跳过这个bug了,你把str1改成 '''ABSaDKSbRIHcRHGcDIF'''就可以看到报错了。
另外,你的代码错了两个地方:
if str1.isupper():应该是if str1.isupper():
if str1.islower():应该是if str1.islower():
最后建议以后发代码用代码模式发,点击编辑界面的一排按钮中的尖括号“<>”,把代码复制在窗口里确认就可以了。 txxcat 发表于 2020-4-23 12:41
i+1 != length,意思是说不是最后一个字符,如果不加这句,最后一组符合(countA == 3 and countC == 3)时就 ...
代码我这边是对的,可能粘贴上去就变成这样了,
页:
[1]