|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目:请在字符串中找出密码,密码符合以下规律:
1.每位密码为单个小写字母;
2.每位密码字母的左右两边均有且只有3个大写字母
问题:好不容易读懂了大部分代码,但最终运行时没显示结果,究竟是为什么呢?请赐教,感谢!
str1 = '''拷贝AAAiBBkkAAAiBBB究竟是怎样的呢CCCuDDD
看一看'''
countA = 0 #统计前三个字母为大写时计数
countB = 0 #满足前三个为大写字母后跳转条件用
countC = 0 #统计后三个字母为大写时计数
length = len(str1)
for i in range(length):
if str1[i] == '\n': #换行则重新执行循环
continue
if str1[i].isupper(): #字符为大写时执行
if countB == 1: #countB为1时统计countC的数量
countC += 1
countA = 0
else:
countA += 1 #否则重新统计countA的数量
continue
if str1[i].islower() and countA == 3: #字符为小写,且countA为3时执行
countB = 1
countA = 0
target = i
continue
if str1[i].islower() and countC == 3: #字符为小写,且countC为3时执行
print(str1[target],end='')
countA = 0 #不符合上述条件时countA,B,C置0
countB = 0
countC = 0
附上了注释,感觉没问题啊,但就是打印不了结果:iu 。
希望帮忙看看!
全部用平行if判定肯定不可能满足条件
因为循环到任意位置的时候,都不知道后面三个字符到底是什么
所以最后的if应该判定i大于等于3 且考察 str1[i-3]字符是否大小写,而且位置也应该放到第一个if后面
修改如下:
- str1 = '''拷贝AAAiBBkkAAAiBBB究竟是怎样的呢CCCuDDD
- 看一看'''
- countA = 0 #统计前三个字母为大写时计数
- countB = 0 #满足前三个为大写字母后跳转条件用
- countC = 0 #统计后三个字母为大写时计数
- length = len(str1)
- for i in range(length):
- if str1[i] == '\n': #换行则重新执行循环
- continue
- if str1[i].isupper(): #字符为大写时执行
- if countB == 1: #countB为1时统计countC的数量
- countC += 1
- countA = 0
- if i > 2 and str1[i-3].islower() and countC == 3: #字符为小写,且countC为3时执行
- print(str1[target],end='')
- else:
- countA += 1 #否则重新统计countA的数量
- continue
- if str1[i].islower() and countA == 3: #字符为小写,且countA为3时执行
- countB = 1
- countA = 0
- target = i
- continue
- countA = 0 #不符合上述条件时countA,B,C置0
- countB = 0
- countC = 0
-
-
复制代码
|
|