华为机考入门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)
相关推荐
tjjucheng7 分钟前
小程序定制开发公司排名
python
想逃离铁厂的老铁23 分钟前
Day43 >> 300.最长递增子序列 + 674. 最长连续递增序列+ 718. 最长重复子数组
数据结构·算法
Yzzz-F23 分钟前
P6648 [CCC 2019] Triangle: The Data Structure [st表]
算法
276695829231 分钟前
dy bd-ticket-guard-client-data bd-ticket-guard-ree-public-key 逆向
前端·javascript·python·abogus·bd-ticket·mstoken·ticket-guard
Maddie_Mo35 分钟前
智能体设计模式 第一章:提示链
人工智能·python·语言模型·rag
LateFrames37 分钟前
泰勒级数:从 “单点” 到 “理论与实践的鸿沟”
学习·算法
m0_7263658340 分钟前
哈希分分预测系统 + Python Worker + Web 仪表盘”小系统(PHP + MySQL)
前端·python·哈希算法
武帝为此1 小时前
【RC4加密算法介绍】
网络·python·算法
宵时待雨1 小时前
数据结构(初阶)笔记归纳4:单链表的实现
c语言·开发语言·数据结构·笔记·算法
时光慢煮1 小时前
基于 Flutter × OpenHarmony 图书馆管理系统之构建书籍列表
flutter·华为·开源·openharmony