欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > 华为机考入门python3--(36)牛客36-字符串加密

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

2025/2/26 4:36:45 来源:https://blog.csdn.net/u013288190/article/details/139710905  浏览:    关键词:华为机考入门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 += charkey_set.add(char)# 未出现的字母按照正常字母表顺序加入新字母表for char in "abcdefghijklmnopqrstuvwxyz":if char not in key_set:cipher_table += charreturn 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_charreturn result# 输入
key = input().strip()
message = input().strip()# 生成加密表和加密信息,然后输出结果
cipher_table = generate_cipher_table(key)
encrypted_message = encrypt_message(cipher_table, message)
print(encrypted_message)

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词