华为机考入门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)
相关推荐
Thanks_ks6 小时前
提示词工程实战:用结构化提示词让大模型输出稳定可控
python·大模型·json·openai·提示词工程·deepseek·系统提示词
钉先生贾7 小时前
HarmonyOS 7.0性能与稳定性排障实战 05:真机预检、冷启动埋点与回归验证
华为·回归·harmonyos
BK贝壳8 小时前
布尔数组也能做按位取反、移位和乘法:bool-hybrid-array 的位运算在本机跑了一遍
开发语言·数据结构·python·性能优化·numpy
ZhengEnCi9 小时前
前端日志和后端日志怎么记:分级、JSON 格式、按天分文件与保留策略
前端·后端·python
人间乄惊鸿客10 小时前
c++ 右值
c++·算法
fanstuck11 小时前
AI 客服答错了,三分钟分清是 Prompt 还是系统的锅
算法
田里的水稻12 小时前
FA_编程算法200个分类列数-SUM
算法·分类·数据挖掘
用户03321266636713 小时前
使用 Python 为 PDF 文件添加页眉页脚
python
xurileidian13 小时前
第 17 篇 · 边玩边学Python · for 遍历 enumerate 与 zip
java·前端·python