华为机考入门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)
相关推荐
stolentime19 小时前
CF2066D2 Club of Young Aircraft Builders (hard version)题解
c++·算法·动态规划·组合数学
copyer_xyf19 小时前
Python 函数全面总结
前端·后端·python
zmzb010319 小时前
Python课后习题训练记录Day123
开发语言·python
PersistJiao19 小时前
python环境下免费、专业的中英翻译
开发语言·windows·python·机器翻译
weixin_6042366719 小时前
华为企业级路由器完整版实战配置
网络·安全·华为·智能路由器·华为交换机命令·华为路由器
一个不知名程序员www19 小时前
算法学习入门---算法题DAY3
c++·算法
七夜zippoe19 小时前
DolphinDB向量化计算:高性能数据处理
算法·dolphindb
Pocker_Spades_A19 小时前
[鸿蒙PC命令行移植适配]移植rust三方库erdtree到鸿蒙PC的完整实践
华为·rust·harmonyos
禁默19 小时前
[鸿蒙PC命令行移植适配]移植rust三方库starship到鸿蒙PC的完整实践
华为·rust·harmonyos