比较新奇的算法了,就是效率有点低,不过对于现在的电脑来说不算什么,先说两个显性错误:
1.while循环中i值没有变化,死循环了,加一条i+=1或者换成for循环;
2.变量c没有,是变量b。
然后是算法bug:
1.没有考虑非字母,这里主要是'\n';
2.判断第四个字符和倒数第四个字符不是简单看前三后三,而是第四个是前三后四,倒数第四个是前四后三。
修正了代码,可以跑起来了,还有可以优化的地方再慢慢想吧:
- def find1(str1):
- a = [] #建立空列表储存转化为大小写判断的flag
- b = '' #建议空字符串以便拼接密码
- for each in str1:
- if each.islower() == 1: #判断是否为小写
- flag = 1 #将小写标记为1
- a.append(flag) #将flag储存到列表a中
- elif each.isupper() == 1:
- flag = 0 #将大写标记为0
- a.append(flag) #将flag储存到列表a中
- else:
- flag = 2 #非字母标记为2
- a.append(flag) #将flag储存到列表a中
-
- i = 3 #按照题目要求,从第4个元素开始判断
- while i <= len(a)-3: #在倒数第4个元素处停止
- if i == 3 : #在第4个处判断[0,0,0,1,0,0,0,非0]的组合
- if a[i] == 1 and a[i-3] == 0 and a[i-2] == 0 and a[i-1] == 0 and a[i+1] == 0 and a[i+2] == 0 and a[i+3] == 0 and a[i+4] != 0:
- b = b + str1[i]
- if i == (len(a)-3): #在倒数第4个处判断[非0,0,0,0,1,0,0,0]的组合
- if a[i] == 1 and a[i-4] != 0 and a[i-3] == 0 and a[i-2] == 0 and a[i-1] == 0 and a[i+1] == 0 and a[i+2] == 0 and a[i+3] == 0:
- b = b + str1[i]
- else: #在其他位置上的元素判断[非0,0,0,0,1,0,0,0,非0]的组合
- if a[i] == 1 and a[i-4] != 0 and a[i-3] == 0 and a[i-2] == 0 and a[i-1] == 0 and a[i+1] == 0 and a[i+2] == 0 and a[i+3] == 0 and a[i+4] != 0:
- b = b + str1[i]
- i+=1
- print('密码是' + b)
复制代码