华为机考入门python3--(36)牛客36-字符串加密

分类:字符串

知识点:

  1. 判断一个元素是否在集合中    if char not in key_set

  2. 计算字母差    index = ord(char) - ord('a')

题目来自【牛客】

图片

# 生成加密表
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. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 21:48:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 21:48:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 21:48:03       82 阅读
  4. Python语言-面向对象

    2024-06-16 21:48:03       91 阅读

热门阅读

  1. C# —— 异常捕获

    2024-06-16 21:48:03       34 阅读
  2. 解释一下 Flux

    2024-06-16 21:48:03       33 阅读
  3. 从C语言到C++(五)

    2024-06-16 21:48:03       28 阅读
  4. Git与SSH

    2024-06-16 21:48:03       34 阅读
  5. Vue3 和 Vue2 对比分析及示例代码解析(初级)

    2024-06-16 21:48:03       31 阅读
  6. Web前端高级课程:深入探索与技能飞跃

    2024-06-16 21:48:03       27 阅读
  7. 常见的中间件都在解决什么问题?

    2024-06-16 21:48:03       30 阅读
  8. 大数据数仓30问

    2024-06-16 21:48:03       28 阅读