华为机考入门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)
相关推荐
一个闪现必杀技4 分钟前
Python练习2
开发语言·python
Eric.Lee202110 分钟前
音频文件重采样 - python 实现
人工智能·python·深度学习·算法·audio·音频重采样
大神薯条老师11 分钟前
Python从入门到高手5.1节-Python简单数据类型
爬虫·python·深度学习·机器学习·数据分析
huapiaoy32 分钟前
Redis中数据类型的使用(hash和list)
redis·算法·哈希算法
Mr.D学长41 分钟前
毕业设计 深度学习社交距离检测系统(源码+论文)
python·毕业设计·毕设
wdxylb1 小时前
解决Python使用Selenium 时遇到网页 <body> 划不动的问题
python
冷白白1 小时前
【C++】C++对象初探及友元
c语言·开发语言·c++·算法
代码骑士1 小时前
【一起学NLP】Chapter3-使用神经网络解决问题
python·神经网络·自然语言处理
鹤上听雷1 小时前
【AGC005D】~K Perm Counting(计数抽象成图)
算法
一叶祇秋1 小时前
Leetcode - 周赛417
算法·leetcode·职场和发展