[Python3 练习] 010 找出藏在字符串中的“密码”
题目:找出藏在字符串中的“密码”
(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 找出藏在字符串中的“密码”的更多相关文章
- 389. Find the Difference 找出两个字符串中多余的一个字符
[抄题]: Given two strings s and t which consist of only lowercase letters. String t is generated by ra ...
- 找出此产品描述中包含N个关键字的长度最短的子串
阿里巴巴笔试题:给定一段产品的英文描述,包含M个英文字母,每个英文单词以空格分隔,无其他标点符号:再给定N个英文关键词,请说明思路并变成实现方法. String extractSummary(Stri ...
- Java - Collection 高效的找出两个List中的不同元素
如题:有List<String> list1和List<String> list2,两个集合各有上万个元素,怎样取出两个集合中不同的元素? 方法1:遍历两个集合 public ...
- FCC JS基础算法题(5):Return Largest Numbers in Arrays(找出多个数组中的最大数)
题目描述: 找出多个数组中的最大数右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组.提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组 ...
- 使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页
使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页 方法1:linux下使用awk命令 # cat access1.log | awk '{print $1" &q ...
- NLP任务:给定一句话,找出这句话中你想要的关键词,包括起始结束索引
在实际的nlp实际任务中,你有一大堆的人工标注的关键词,来新的一句话,找出这句话中的关键词,以便你以后使用,那如何来做呢? 1)用到正则的 finditer()方法,返回你匹配的关键词的迭代对象,包含 ...
- Java Collection - 003 高效的找出两个List中的不同元素
如题:有List<String> list1和List<String> list2,两个集合各有上万个元素,怎样取出两个集合中不同的元素? 方法1:遍历两个集合 public ...
- Class 找出一个整形数组中的元素的最大值
目的:找出一个整形数组中的元素的最大值 以下,我们用类和对象的方法来做. #include<iostream> using namespace std; class Array_m ...
- [MSSQL]找出一天数据中从第一条数据开始每累加1小时的数据
用Sql Server找出一天数据中从第一条数据开始每累加1小时的数据 -- ============================================= -- Author: Alle ...
随机推荐
- 加密模块hashlib
#coding=utf-8 import ConfigParser #配置文件模块 import hashlib #用于加密的模块 m = hashlib.md5() m.update(b'hello ...
- C# 委托 多线程
C#委托和多线程[转载] 很多时候写windows程序都需要结合多线程,在.net中用如下得代码来创建并启动一个新的线程.public void ThreadProc();Thread thread ...
- hash和history
location.hash="aaa" history.pushState({},'', "home") history.replaceState() hist ...
- 未能加载文件或程序集“WebApi”或它的某一个依赖项。试图加载格式不正确的程序。
将项目的平台目标改为“Any CPU” 在项目上右击选择属性——>生成——>平台目标 选择Any CPU
- Lamabda Where Select Find First等区别
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- mysql 注入绕过小特性
1. 注释 Select /*多行(单行)注释*/ version(); Select version(); #单行注释 Select version(); -- 单行注释 (两划线之后必须有空格) ...
- php7 mysqli连接mysql的几种方式
一.过程是方法 function connect(){ static $conn; if(!$conn){ $conn = mysqli_connect(DB_HOST,DB_USER,DB_PWD) ...
- spring mvc 和spring boot 中注解的使用
1 spring mvc和spring boot之间的关系 spring boot包含spring mvc.所以,spring mvc的注解在spring boot总都是可以用的吗? spring b ...
- Codeforces 892E Envy
问题描述 小Q正在玩一个叠塔的游戏,游戏的目标是叠出尽可能高的塔.在游戏中,一共有n张矩形卡片,其中第i张卡片的 长度为a_i,宽度为b_i.小Q需要把所有卡片按一定顺序叠成一座塔,要求对于任意一个矩 ...
- 【leetcode】449. Serialize and Deserialize BST
题目如下: Serialization is the process of converting a data structure or object into a sequence of bits ...