Python's clear ciphertext conversion

Source: Internet
Author: User

This question is one of the problems I encountered when I was learning udacity (a web-based course platform) to learn the Web Development course:

The topic is probably to define a ROT13 function, and then convert the input letter to the corresponding 13 position of the letter, such as input hello, output should be urryb, the case is unchanged, the other symbols unchanged.

Instinctively think of a corresponding two-tuple list to map: The zip used here, its function is to merge two lists into a two-tuple, each element in the tuple is the element of the original list

L1 = [1,3,5,7]
L2 = [2,4,6,8]
#使用zip将两个列表合并 print zip (l1,l2) for (a,b) in
zip (l1,l2):
  print (A,B) C5/>L3 = [2,4,6]
#当长度不一的时候, redundant ignored
print zip (l1,l3) #extra items are ignored
#map则不会忽略, such as the following example. When the length of L1 and L3 is different,
#会用第一个参数来填充.
Print map (NONE,L1,L3)

Now I'm going to use zip to try to solve this problem.

s = "abcdefghijklmnopqrstuvwxyz"
t = "NOPQRSTUVWXYZABCDEFGHIJKLM"
list = Zip (s,t)

def ROT13 (str): For
    (i,o) in list:
        str = str.replace (i,o)
    Print str

Run the module, the results are as follows

>>> ROT13 ("abcdefghijklmnopqrstuvwxyz")
Abcdefghijklmabcdefghijklm

In other words, from A to M, the conversion from N to Z succeeded.

I have also cited another example:

>>> ROT13 ("NOPQRSTUVWXYZABCDEFGHIJKLM")
ABCDEFGHIJKLMABCDEFGHIJKLM also replaces only N to Z

Here I feel this replace may be to find all a, replace, and then find all the B ..., and then when the a~m are replaced with n~z, it's the same as the ones that follow, and then all the letters in the string will be converted to A~m

Thinking about this, I changed the code.

Change list = Zip (s,t) to list =zip (t,s)

The result:

>>> ROT13 ("abcdefghijklmnopqrstuvwxyz")
Nopqrstuvwxyznopqrstuvwxyz

Just replace A~m, the assumption seems to make sense.

To further verify my idea, I changed the code again.

s = "abc"
t = "BCA"
list = Zip (s,t)

def ROT13 (str): For
    (i,o) in list:
        str = str.replace (i,o)
    Prin T str

Thus, if the above conjecture is correct, then ROT13 ("ABCA") should be after AAA

The specific transformation process is ABC->BBC->CCC->AAA

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.