- A+
所属分类:Python习题
内容规则:
- 密码两侧均有且只有三个大写字母
- 每个密码为单个小写字母
- 文件内容:string2
代码逻辑:
在开始的时候,想到第一个的是两侧都有三个大写字母,中间一个小写字母,那就是刚好7位了,后面才发现且只有(这玩意在群里问了半天,才模模糊糊的知道了),后面将切割为9位。
知识点:
replace(),去重
列表的切片(https://www.lijinlong.cc/python/pyxx/1542.html),注意从0开始索引。
代码:
- f =open(r'/Users/lijinlong/Desktop/string2.txt')
- c = f.read()
- a = c.replace("\n", "")
- n = 0
- while n<len(a):
- n+=1
- b = a[n:9+n]
- # print(b)
- if len(b) == 9:
- if b[0].islower() and b[1:4].isupper() and b[4:5].islower() and b[5:8].isupper() and b[-1].islower():
- print(b[4:5],end='')
第二种解法
通过对字符串的索引,来统计大小写的字母的数量,最终来判断是否符合规则
- str1 = c
- 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:
- countC += 1
- else:
- countC = 0
- countA += 1
- if str1[i].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[i+1].isupper():
- countB = 0
- countC = 0
- else:
- print(str1[target], end='')
- countA = 3
- countB = 0
- countC = 0