字符长串破解密码

a)每个密码为单个小写字母

b)每个密码左右两边均有且只有三个大写字母

#方法一:

 1 str1=''' '''
 2 lenth = len(str1)
 3 for each in range(lenth):
 4   if (str1[each].islower() and str1[each-1].isupper() and
 5                 str1[each-2].isupper() and
 6                 str1[each-3].isupper() and
 7                 str1[each-4].islower() and
 8                 str1[each+1].isupper() and
 9                 str1[each+2].isupper() and
10                 str1[each+3].isupper() and
11                 str1[each+4].islower()) :
12                   print(str1[each],end='')

 

#方法二:

 1 str1 = ''' '''
 2 
 3 countA = 0  # 统计前边的大写字母
 4 countB = 0  # 统计小写字母
 5 countC = 0  # 统计后边的大写字母
 6 length = len(str1)
 7 
 8 for i in range(length):
 9     if str1[i] == '\n':
10         continue
11 
12     """
13     |如果str1[i]是大写字母:
14     |-- 如果已经出现小写字母:
15     |-- -- 统计后边的大写字母
16     |-- 如果未出现小写字母:
17     |-- -- 清空后边大写字母的统计
18     |-- -- 统计前边的大写字母
19     """
20     if str1[i].isupper():
21         if countB:
22             countC += 1
23         else:
24             countC = 0
25             countA += 1
26 
27     """
28     |如果str1[i]是小写字母:
29     |-- 如果小写字母前边不是三个大写字母(不符合条件):
30     |-- -- 清空所有记录,重新统计
31     |-- 如果小写字母前边是三个大写字母(符合条件):
32     |-- -- 如果已经存在小写字母:
33     |-- -- -- 清空所有记录,重新统计(出现两个小写字母)
34     |-- -- 如果该小写字母是唯一的:
35     |-- -- -- countB记录出现小写字母,准备开始统计countC
36     """
37     if str1[i].islower():
38         if countA != 3:
39             countA = 0
40             countB = 0
41             countC = 0
42         else: 
43             if countB:
44                 countA = 0
45                 countB = 0
46                 countC = 0
47             else:
48                 countB = 1
49                 countC = 0
50                 target = i
51 
52     """
53     |如果前边和后边都是三个大写字母:
54     |-- 如果后边第四个字母也是大写字母(不符合条件):
55     |-- -- 清空记录B和C,重新统计
56     |-- 如果后边仅有三个大写字母(符合所有条件):
57     |-- -- 打印结果,并清空所有记录,进入下一轮统计
58     """
59     if countA == 3 and countC == 3:
60         if i+1 != length and str1[i+1].isupper():
61             countB = 0
62             countC = 0
63         else:
64             print(str1[target], end='')
65             countA = 3
66             countB = 0
67             countC = 0

 

posted @ 2017-01-31 23:37  道高一尺  阅读(407)  评论(0编辑  收藏  举报