|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #1. 请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
- #a) 每位密码为单个小写字母
- #b) 每位密码的左右两边均有且只有三个大写字母
- length2 = len(longstr)
- password = ''
- for each in (4,length2 - 3):
- if longstr[each].islower() == True: #判断是否为小写字母
- if longstr[each+4].islower == True and longstr[each-4].islower() == True:#判断右/左侧第四个是小写
- count = 0 #初始化count
- for nums in range(1,4):
- if longstr[each+nums].isupper == True and longstr[each-nums].isupper == True: #判断右/左侧是否为大写字母
- count += 1
- if count == 3:
- password += longstr[each]
- print(password)
复制代码
每次运行都是''
服气了哦
思路如下:
1,获取字符串长度,依次迭代出每一个元素
2,判断是否为小写,并从这开始对前后的元素是否符合要求进行判断(毕竟密码全是小写么)
3,判断左右第四个是否为小写,如果不是那就pass
4,初始化count,并迭代1~3三个数,进行对前后三个元素是否符合进行判断
5,每迭代一次如果符合全是大写,count+1
6,最后判断count是否为三,如果是就说明前后三个全是大写
实在想不到哪里错了
那就正好,给你个简单的
他的代码我还没研究,感觉有点复杂就是
- # 1. 请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
- # a) 每位密码为单个小写字母
- # b) 每位密码的左右两边均有且只有三个大写字母
- # 源代码并未定义 longstr 变量
- longstr = 'ABSaDKSbRIHcRHGcdDIF'
- length2 = len(longstr)
- password = ''
- # 此处源代码少了一个 range, 没有 range 表明在 3 和 length - 2 这两个数里面运行 for,而非组合两个数的范围之间运行
- for each in range(3, length2 - 3): # 此处应该为 3 而不是 4,因为 下标从 0 开始的
- # 为了简略,可以直接写 if longstr[each].islower():
- # 不需要if longstr[each].islower() == True:
- # 因为只有当返回值不为 0 才会通过 if 语句,返回值为 0 则不通过
- # True 在 python 中可以认为是 1, False 可以认为是 0
- # 如果需要的是非小写字母通过 if 语句,则使用 if !longstr[each].islower():
- # !True 表示的就是 0
- if longstr[each].islower(): # 判断是否为小写字母
- # 避免越界,即不超过右边界。如果超过右边界,则退出循环,即 each + 3 超过了 longstr 的长度,不需要继续检查了
- if each + 3 < length2:
- # 如果当前位置左边的三个均为大写字母则通过 if 循环
- if longstr[each - 1].isupper() and longstr[each - 2].isupper() and longstr[each - 3].isupper():
- # 如果当前位置右边的三个均为大写字母则通过 if 循环, 并且将当前字符插入到 password 中,即找到了需要的密码
- if longstr[each + 1].isupper() and longstr[each + 2].isupper() and longstr[each + 3].isupper():
- password += longstr[each]
- else:
- break
- print(password)
复制代码
你别看我这么多行,其实注释占了一大半
|
|