描述
题源
- PythonChallenge 第二题
- 小甲鱼老师做了改编:第020讲:函数:内嵌函数和闭包 | 课后测试题及答案
说明
- 题中带有一条极长的字符串,不方便写在此随笔中
- 我心血来潮,也改编一下
要求
- 创建一串类似
$%#@^
的乱码 - 将字符串
"I am not a smart man, but I know what love is."
中的字符按顺序插入乱码 - 统计新乱码中各字符的个数
- 把字符串找出来
程序
from random import choice, randint
string = "I am not a smart man, but I know what love is."
# 写入
symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')']
garbled = [choice(symbols) for _ in range(520)]
pos = 0
interval = 520 // len(string)
for s in string:
pos += randint(1, interval)
garbled.insert(pos, s)
# 读取
first = {}
for e in garbled:
first[e] = first.get(e, 0) + 1
print(first)
for s in garbled:
if first[s] < 15:
print(s, end='')
print()
-
运行截图(结果不唯一)