华为机考入门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)
相关推荐
追求源于热爱!5 小时前
记10,Gradio介绍
python
CelestialYuxin5 小时前
A.R.I.S.系统:YOLOx在破碎电子废料分拣中的新探索
人工智能·深度学习·算法
_ziva_5 小时前
YOLO 目标检测算法深度解析:从原理到实战价值
算法·yolo·目标检测
破晓之翼5 小时前
Skill原理及国内大模型实践
人工智能·python
IT管理圈6 小时前
Cursor Rules 实战指南—让AI按你的规矩写代码
python
Java后端的Ai之路6 小时前
微调模型成本太高,用RAG技术,低成本实现AI升级
开发语言·人工智能·python·rag·ai升级
Jason_Honey26 小时前
【蚂蚁金服Agent算法岗一面】
人工智能·算法·自然语言处理·面试
weixin_477271696 小时前
《老子》一书作者“李耳”简介
算法·图搜索算法
智算菩萨6 小时前
交错多模态内容生成:从“单张图“到“图文混排长文“的创作范式变革
人工智能·算法·aigc
喵手6 小时前
Python爬虫实战:从零构建书籍价格情报数据库(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·csv导出·构建书籍价格情报·书籍价格采集