将代码转换为python中的列表理解 [英] Convert the code to list comprehension in python

查看:69
本文介绍了将代码转换为python中的列表理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

密码是一种语言的密码.在本案例研究中,我们将探索当代希腊历史学家报道的一种密码,该密码在战争时期被尤利乌斯·凯撒(Julius Caesar)用来向将军发送秘密信息.

A cipher is a secret code for a language. In this case study, we will explore a cipher that is reported by contemporary Greek historians to have been used by Julius Caesar to send secret messages to generals during times of war.

Caesar密码将邮件的每个字母移到与原始字母有固定距离的字母表中的另一个字母.如果我们的加密密钥为1,我们将h移至下一个字母i,i移至下一个字母j,依此类推.如果到达字母表的结尾(对我们来说是空格字符),我们只需循环回到a.为了对消息进行解码,我们进行了类似的移位,只是我们在字母表中向后移动了相同的步数.

The Caesar cipher shifts each letter of a message to another letter in the alphabet located a fixed distance from the original letter. If our encryption key were 1, we would shift h to the next letter i, i to the next letter j, and so on. If we reach the end of the alphabet, which for us is the space character, we simply loop back to a. To decode the message, we make a similar shift, except we move the same number of steps backwards in the alphabet.

锻炼:

在本作业的五个练习中,我们将创建我们自己的Caesar密码以及该密码的消息解码器.

In the five exercises of this homework, we will create our own Caesar cipher, as well as a message decoder for this cipher.

***第一步:我们将定义密码中使用的字母.示例代码导入字符串库已经导入.创建一个称为字母的字符串,该字符串由空格字符('')和小写字母组成.请注意,本练习中我们仅使用小写字母.

***Step1 : we will define the alphabet used in the cipher. The sample code imports the string library has been imported. Create a string called alphabet consisting of the space character (' ') followed by (concatenated with) the lowercase letters. Note that we're only using the lowercase letters in this exercise.

Step2:我们将定义一个字典,该字典指定字母中每个字符的索引.请注意,字母是如练习1中所定义的.创建一个字典,其键由字母中的字符组成,值由0到26之间的数字组成.将其存储为位置.

Step2 : we will define a dictionary that specifies the index of each character in alphabet. Note that alphabet is as defined in Exercise 1. Create a dictionary with keys consisting of the characters in alphabet and values consisting of the numbers from 0 to 26. Store this as positions.

第3步:我们将使用Caesar密码对消息进行编码.请注意,字母和位置是在练习1和2中定义的.使用位置根据消息创建编码的消息,其中消息中的每个字符都按位置定义向前移动了1个位置.请注意,您可以使用结果%27来确保结果保持在0-26范围内.将其存储为encoded_message.

Step 3 : we will encode a message with a Caesar cipher. Note that alphabet and positions are as defined in Exercises 1 and 2. Use positions to create an encoded message based on message where each character in message has been shifted forward by 1 position, as defined by positions. Note that you can ensure the result remains within 0-26 using result % 27. Store this as encoded_message.

encoded_message是什么?(以列表理解方式编写的代码)***

What is encoded_message?(code written in list comprehension)***

我已经为步骤1和步骤2编写了代码.下面是我的第3步代码.我想编辑"encoded_message"以列出理解.下面是我的代码:

I have written the codes for Step1 and Step2. Below is my code for Step 3. I want to edit "encoded_message" to list comprehension.Below is my code:

message = "hi my name is caesar"
encoded_message = ''   
for c in message:
    for key, values in positions.items(): 
        if values == (positions[c] + 1) % 27:  
            encoded_message += key 
print(encoded_message)

我的代码可以正常工作.但是我需要将整个代码写在一行中,然后发送编码消息的代码?

My code is working fine as it is. But i need the write the whole code in one line and send the code of encoded_message?

推荐答案

如果您只想编码消息,请尝试此操作.这是终止密码,如果您想了解其他技术,请在评论中告诉我.

If you just want to encode a message then try this. This is ceaser cipher, if you want to know other techniques then let me know in comments.

def encrypt(text, s):
    result = ""

    # traverse text
    for i in range(len(text)):
        char = text[i]

        # Encrypt uppercase characters
        if (char.isupper()):
            result += chr((ord(char) + s - 65) % 26 + 65)

            # Encrypt lowercase characters
        else:
            result += chr((ord(char) + s - 97) % 26 + 97)

    return result


# check the above function
text = "CEASER CIPHER DEMO"
s = 4
print("Text  : " + text)
print("Shift : " + str(s))
print("Cipher: " + encrypt(text, s))

这篇关于将代码转换为python中的列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆