华为机考入门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 分钟前
【Python】基本语法:装饰器语法糖@
python
爱写代码的阿木23 分钟前
基于鸿蒙OS开发附近社交游戏平台(二十三)-拉黑系统、屏蔽与全局过滤
游戏·华为·harmonyos
Zachery Pole1 小时前
CCF-CSP备战NO.7【队列】
算法
闪电悠米1 小时前
力扣hot100-48.旋转图像-转置翻转详解
算法·leetcode·职场和发展
_Jimmy_1 小时前
Agent常用检索器的详细介绍
python·langchain
满天星83035771 小时前
【算法】最长递增子序列(三种解法)
算法
小柯南敲键盘1 小时前
图片翻译API接入与自动化实现指南
运维·python·自动化
旅僧2 小时前
Q-learning(自用)
python·机器学习
qizayaoshuap2 小时前
# 颜色混合器 — HarmonyOS RGB调色板与Slider组件实战
华为·harmonyos
啦啦啦啦啦zzzz2 小时前
算法:贪心算法
c++·算法·leetcode·贪心算法