华为机考入门python3--(36)牛客36-字符串加密

分类:字符串

知识点:

  1. 判断一个元素是否在集合中 if char not in key_set

  2. 计算字母差 index = ord(char) - ord('a')

题目来自【牛客】

python 复制代码
# 生成加密表
def generate_cipher_table(key):
    key_set = set()
    cipher_table = ""

    # 去重
    for char in key:
        if char not in key_set:
            cipher_table += char
            key_set.add(char)

    # 未出现的字母按照正常字母表顺序加入新字母表
    for char in "abcdefghijklmnopqrstuvwxyz":
        if char not in key_set:
            cipher_table += char

    return cipher_table


# 使用给定的密匙加密信息
def encrypt_message(cipher_table, message):
    result = ""
    for char in message:
        # 计算索引
        index = ord(char) - ord('a')
        encrypted_char = cipher_table[index]
        result += encrypted_char
    return result

# 输入
key = input().strip()
message = input().strip()

# 生成加密表和加密信息,然后输出结果
cipher_table = generate_cipher_table(key)
encrypted_message = encrypt_message(cipher_table, message)
print(encrypted_message)
相关推荐
paeamecium16 小时前
【PAT甲级真题】- Rational Sum (20)
数据结构·c++·python·算法·pat考试·pat
爱昏羔1 天前
下篇:从检索到交互 — 物流RAG问答系统与WebUI全实现
python·langchain·agent
_Jimmy_1 天前
Agent引用数据库知识过时的增量同步方案
人工智能·python·langchain
min(a,b)1 天前
学习第 4 天:面向对象与异常处理
python·学习·学习方法
拳里剑气1 天前
C++算法:BFS解决FloodFill算法
c++·算法·bfs·宽度优先
yangshicong1 天前
第19章:AI安全防护与AI安全
人工智能·python·安全·prompt·ai编程
果汁华1 天前
Function Calling 与 Python 实战完整指南
开发语言·网络·python
c_lb72881 天前
2026年不同基础做量化,先找AI能参与的位置
人工智能·python
wanderist.1 天前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法