简体   繁体   中英

Convert the code to list comprehension in python

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.

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.

Exercises:

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: 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.

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.

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM