题目:找出藏在字符串中的“密码”

(1) 描述

1) 题源 1 Python Challenge, level 3

2) 题源 2 小甲鱼老师的 Python 课程,第 20 讲课后习题

3) 修改

  • 题中带有一条极长的字符串,不方便写在此随笔中
  • 我自己心血来潮,将题目小小地改动了一下(其实是降低了难度)
  • 具体见下方要求

(2) 要求

  • 有一字符串,仅含英文字母(没有回车、空格)
  • 若有 1 个小写的英文字母,其左右两边均有且仅有 3 个大写字母,则将其挑出
  • 将所有挑出的字母按顺序输出
  • 例如
    • 字符串:AAAAjBBBpCCCrDDmEEEyFFFqqGGG
    • 输出:py

(3) 程序

解法 1:一一检测(像是暴力破解)

def decrypt(string):
len_str = len(string)
target = 4 if \ # 检测开头,即检测整串字符串的第 3 位
string[0].isupper() and \
string[1].isupper() and \
string[2].isupper() and \
string[3].islower() and \
string[4].isupper() and \
string[5].isupper() and \
string[6].isupper() and \
string[7].islower():
print(string[3], end='') while target < len_str-4:
if \# 检测中间
string[target-4].islower() and \ # 若有多种字符,可用 not string[i].isupper()
string[target-3].isupper() and \
string[target-2].isupper() and \
string[target-1].isupper() and \
string[target ].islower() and \
string[target+1].isupper() and \
string[target+2].isupper() and \
string[target+3].isupper() and \
string[target+4].islower():
print(string[target], end='')
target += 4
else:
target += 1 if \ # 检测结尾,即检测整串字符串的倒数第 4 位
string[len_str-7].islower() and \
string[len_str-6].isupper() and \
string[len_str-5].isupper() and \
string[len_str-4].islower() and \
string[len_str-3].islower() and \
string[len_str-2].isupper() and \
string[len_str-1].isupper() and \
string[len_str ].isupper():
print(string[len_str-3], end='')

解法 2:利用编号

  • 利用列表,使字符串中的每个字符都有一个对应的编号
  • 因为字符串中只有大小写英文字母,所以先理出所有小写英文字母的编号,再检查编号的间距
  • 若某个编号与其左右相邻的编号均相差 4,则该编号对应的字母即为所求之一
ABCdEFGhIj
0123456789
↓ ↓ ↓
3 7 9 # 第一轮,筛出小写字母的编号

3 # 第二轮,筛出符合规则的字母的编号
def decrypt(string):
len_str = len(string)
list0 = []
for i in range(len_str): # 找出所有小写字母在 string 中的编号,并写入 list0
if string[i].islower():
list0.append(i) list1 = []
if list0[0] == 3 and list0[1] == 7: # 检测开头,即检测整串字符串的第 3 位
list1.append(3)
for i in range(1, len(list0)): # 检测中间,找出 list0 中符合要求的小写字母的编号
if (list0[i]-4) == list0[i-1] and (list0[i]+4) == list0[i+1]:
list1.append(list0[i])
if list0[-1] == len_str-4 and list0[-2] == len_str-8:
# 检测结尾,即检测整串字符串的倒数第 4 位
list1.append(list0[-1]) for i in list1: # 输出
print(string[i], end='')

解法 3:利用 3 个变量,统计大小写字母的个数

(详见下方 countA、countB、countC)

def decrypt(string):
countA = 0 # 统计小写字母左侧的大写字母
countB = 0 # 统计小写字母
countC = 0 # 统计小写字母右侧的大写字母 len_str = len(string)
for i in range(len_str):
if string[i].isupper():
if countB: # AAAaA; AAAaAA; AAAaAAA
countC += 1
else: # A; AA; AAA; AAAA ...
countC = 0
countA += 1 if string[i].islower():
if countA != 3: # a; Aa; AAa; AAAAa ...
countA = 0
countB = 0
countC = 0
else:
if countB: # AAAaa
countA = 0
countB = 0
countC = 0
else: # AAAa
countB = 1
countC = 0
target = i if countC == 3:
if i+1 != len_str and \ # 若 i 未迭代到最后一位
string[i+1].isupper(): # AAAaAAAA
countB = 0
countC = 0
else: # AAAaAAAb 总算找到了一个 a
print(string[target], end='')
countA = 3 # AAAb
countB = 0
countC = 0

解法 4:re 模块

  • re 模块像“挂”似的
import re

text = "xxx"									# 改 x 或用文件导入
pattern = "[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]" # 设置格式
print(''.join(re.findall(pattern, text)))

[Python3 练习] 010 找出藏在字符串中的“密码”的更多相关文章

  1. 389. Find the Difference 找出两个字符串中多余的一个字符

    [抄题]: Given two strings s and t which consist of only lowercase letters. String t is generated by ra ...

  2. 找出此产品描述中包含N个关键字的长度最短的子串

    阿里巴巴笔试题:给定一段产品的英文描述,包含M个英文字母,每个英文单词以空格分隔,无其他标点符号:再给定N个英文关键词,请说明思路并变成实现方法. String extractSummary(Stri ...

  3. Java - Collection 高效的找出两个List中的不同元素

    如题:有List<String> list1和List<String> list2,两个集合各有上万个元素,怎样取出两个集合中不同的元素? 方法1:遍历两个集合 public ...

  4. FCC JS基础算法题(5):Return Largest Numbers in Arrays(找出多个数组中的最大数)

    题目描述: 找出多个数组中的最大数右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组.提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组 ...

  5. 使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页

    使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页 方法1:linux下使用awk命令 # cat access1.log | awk '{print $1" &q ...

  6. NLP任务:给定一句话,找出这句话中你想要的关键词,包括起始结束索引

    在实际的nlp实际任务中,你有一大堆的人工标注的关键词,来新的一句话,找出这句话中的关键词,以便你以后使用,那如何来做呢? 1)用到正则的 finditer()方法,返回你匹配的关键词的迭代对象,包含 ...

  7. Java Collection - 003 高效的找出两个List中的不同元素

    如题:有List<String> list1和List<String> list2,两个集合各有上万个元素,怎样取出两个集合中不同的元素? 方法1:遍历两个集合 public ...

  8. Class 找出一个整形数组中的元素的最大值

    目的:找出一个整形数组中的元素的最大值   以下,我们用类和对象的方法来做.   #include<iostream> using namespace std; class Array_m ...

  9. [MSSQL]找出一天数据中从第一条数据开始每累加1小时的数据

    用Sql Server找出一天数据中从第一条数据开始每累加1小时的数据 -- ============================================= -- Author: Alle ...

随机推荐

  1. Dorado环境启动出错Spring加载不到资源Bean配置 at org.springframework.asm.ClassReader.<init>(Unknown Source)

    ERROR: org.springframework.web.context.ContextLoader - Context initialization failed org.springframe ...

  2. 彻底解决mysql报错:1030, 'Got error 28 from storage engine'

    权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/harry5508/article/deta ...

  3. AI-sklearn 学习笔记(二)数据集

    from sklearn import datasets from sklearn.linear_model import LinearRegression loaded_data = dataset ...

  4. 2019-9-2-visual-studio-2015-warning-MSB3246

    title author date CreateTime categories visual studio 2015 warning MSB3246 lindexi 2019-09-02 12:57: ...

  5. flashback table

    前提:开启回收站 查看回收站状态 SQL> show parameter recyclebin; NAME TYPE VALUE -------------------------------- ...

  6. Swagger详解(SpringBoot+Swagger集成)(转)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/ai_miracle/article/de ...

  7. div+css做出带三角的弹出框 和箭头

    一.三角形 https://blog.csdn.net/Szu_AKer/article/details/51755821 notice:三角的那部分可以用图片作为背景,但是容易出现杂边.所以利用cs ...

  8. 查看ubuntu系统信息

    root@k8s001:~/go/src/k8s.io/kubernetes# cat /etc/*release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DI ...

  9. Linux下git安装配置

    一.Linux下git安装配置 2013-07-28 20:32:10|  分类: 默认分类 |  标签:linux  git  server  |举报|字号 订阅     http://abomby ...

  10. Js中window.location.href和window.location.replace的区别

    href相当于打开一个新页面,replace相当于替换当前页面:这里打开页面都是针对历史记录来说,在页面上看完全相同,只是浏览器的history表现不同如果在1.html中点击链接到2.html,然后 ...