华为机考入门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)
相关推荐
木头左10 分钟前
年化波动率匹配原则在ETF网格区间选择中的应用
python
清空mega14 分钟前
从零开始搭建 flask 博客实验(3)
后端·python·flask
程序员小远42 分钟前
7个常见的Jmeter压测问题
自动化测试·软件测试·python·测试工具·测试用例·压力测试·性能测试
红尘炼丹客1 小时前
《DeepSeek-OCR: Contexts Optical Compression》速览
人工智能·python·自然语言处理·ocr
☼←安于亥时→❦1 小时前
Playwright 安装与使用
python·playwright
黑屋里的马1 小时前
java的设计模式之桥接模式(Bridge)
java·算法·桥接模式
sin_hielo1 小时前
leetcode 1611
算法·leetcode
大佬,救命!!!1 小时前
python实现象棋
开发语言·python·学习笔记·pygame·少儿编程·记录成长
棉猴1 小时前
《pygame中Sprite类实现多帧动画》注-通过多张序列帧显示动画2-2
开发语言·python·游戏·游戏程序·pygame