华为机考入门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)
相关推荐
VVVVWeiYee14 分钟前
BGP配置华为——路径优选验证
运维·网络·华为·信息与通信
阿尔法波14 分钟前
python与pycharm如何设置文件夹为源代码根目录
开发语言·python·pycharm
xing251623 分钟前
pytest下allure
开发语言·python·pytest
眸笑丶28 分钟前
使用 Python 调用 Ollama API 并调用 deepseek-r1:8b 模型
开发语言·python
dexianshen31 分钟前
配置mysql8.0使用PXC实现高可用
python
中国loong33 分钟前
pandas连接mysql数据库
python
带娃的IT创业者1 小时前
《Python实战进阶》专栏 No.3:Django 项目结构解析与入门DEMO
数据库·python·django
MZWeiei1 小时前
PTA:运用顺序表实现多项式相加
算法
GISer_Jing1 小时前
Javascript排序算法(冒泡排序、快速排序、选择排序、堆排序、插入排序、希尔排序)详解
javascript·算法·排序算法
cookies_s_s1 小时前
Linux--进程(进程虚拟地址空间、页表、进程控制、实现简易shell)
linux·运维·服务器·数据结构·c++·算法·哈希算法