华为机考入门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)
相关推荐
曦月逸霜1 天前
啥是RAG 它能干什么?
人工智能·python·机器学习
浅念-1 天前
递归解题指南:LeetCode经典题全解析
数据结构·算法·leetcode·职场和发展·排序算法·深度优先·递归
2301_769340671 天前
如何在 Vuetify 中可靠捕获 Chip 关闭事件(包括键盘触发).txt
jvm·数据库·python
Kiling_07041 天前
Java集合进阶:Set与Collections详解
算法·哈希算法
智者知已应修善业1 天前
【51单片机89C51及74LS273、74LS244组成】2022-5-28
c++·经验分享·笔记·算法·51单片机
leon_teacher1 天前
HarmonyOS 6 ArkUI 实战:用 Tabs 与 Shape Path 手写凹槽凸起底部导航栏
华为·harmonyos
洛水水1 天前
【力扣100题】33.验证二叉搜索树
算法·leetcode·职场和发展
SimpleLearingAI1 天前
聚类算法详解
算法·数据挖掘·聚类
南 阳1 天前
Python从入门到精通day66
开发语言·python
m0_596749091 天前
JavaScript中手动实现一个new操作符的底层逻辑
jvm·数据库·python