查找隐藏的密码,能讲解下吗?
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
不是很明白,中间的汉字是啥作用? 鱼油你好!这段代码的目标是找出字符串中符合特定模式的字符。我来帮你分析一下:
汉字注释是用来解释代码逻辑的,你可以忽略它们。代码的实际功能是:
[*]查找符合这种模式的字符:三个大写字母 + 一个小写字母 + 三个大写字母
[*]找到后输出中间的那个小写字母
代码逻辑简化说明:
[*]countA:统计小写字母之前的大写字母数量(需要刚好3个)
[*]countB:标记是否遇到了小写字母(0表示未遇到,1表示遇到了)
[*]countC:统计小写字母之后的大写字母数量(需要刚好3个)
在给定的字符串 ABSaDKSbRIHcRHGcdDIF 中:
[*]ABS(3大写)+ a(小写)+ DKS(3大写)→ 输出 a
[*]DKS(3大写)+ b(小写)+ RIH(3大写)→ 输出 b
[*]RHG(3大写)+ c(小写)+ DIF(3大写)→ 输出 c
最终输出结果是:abc
注意:原始代码有个小错误,应该使用 str1 而不是 str1。修正后的代码:
str1 = '''ABSaDKSbRIHcRHGcdDIF'''
countA = 0
countB = 0
countC = 0
length = len(str1)
for i in range(length):
if str1 == '\n':
continue
if str1.isupper():
if countB:
countC += 1
else:
countC = 0
countA += 1
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
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
这样就能正确找到所有符合条件的"隐藏密码"字符了。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 {:10_277:} 我也才学到这儿不久,也没搞定,随便看了下,明白,但是,自己写估计还是写不出来,恼火。 我还有种想法,也可以,只是没去做,就是循环查询之后,大写字母1小写字母0,然后,查找'1110111'且非'11110111'或'11101111'字符出现的下标,找出中间个,然后,所有找到的拼接起来。 我夏天的到来 发表于 2026-4-20 09:17
我还有种想法,也可以,只是没去做,就是循环查询之后,大写字母1小写字母0,然后,查找'1110111'且非'1111 ...
ABSaDKSbRIHcRHGcdDIF
用isuper和islower标记成了'11101110111011100111'
按照规则查出:
1110111--下标
1110111--下标
1110111--下标
就是abc 看是明白了,但是自己还是写不出来,还是得多练习
页:
[1]