华为机考入门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)
相关推荐
鸿蒙开发工程师—阿辉2 分钟前
一键多环境构建——用 Hvigor 玩转 HarmonyOS Next
ubuntu·华为·harmonyos
学c真好玩7 分钟前
Django创建的应用目录详细解释以及如何操作数据库自动创建表
后端·python·django
NapleC8 分钟前
HarmonyOS NEXT:多设备的自由流转
华为·harmonyos
沐暖沐20 分钟前
Django(快速上手版)
python·django
weixin_4284984924 分钟前
使用HYPRE库并行装配IJ稀疏矩阵指南: 矩阵预分配和重复利用
算法·矩阵
槑槑紫1 小时前
pytorch(gpu版本安装)
人工智能·pytorch·python
知识中的海王1 小时前
猿人学web端爬虫攻防大赛赛题第15题——备周则意怠-常见则不疑
爬虫·python
小白学大数据1 小时前
如何避免爬虫因Cookie过期导致登录失效
开发语言·爬虫·python·scrapy
测试老哥1 小时前
接口测试和功能测试详解
自动化测试·软件测试·python·功能测试·测试工具·测试用例·接口测试
小白学大数据2 小时前
Python自动化解决滑块验证码的最佳实践
开发语言·python·自动化