Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
909 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...