1

In this answer:

https://stackoverflow.com/a/3269756/3225934

Paul Rubel (and then wjandra) staggers his text entry like so:

rot13 = string.maketrans( 
    "ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz", 
    "NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")

rather than

rot13 = string.maketrans( 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 
    "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm")

Why is this?

I would have just asked him directly but because of my point level I'm not allowed to do something crazy like ask a clarifying question so I can understand the solution better.

spconrad
  • 50
  • 1
  • 6
  • 1
    My guess is that you can easily verify that it is rotated 13 places, since if you read the second line, it simply continues where the first line left. – Willem Van Onsem Aug 05 '17 at 23:53

1 Answers1

1

There is no difference in output: both produce a dictionary. Now since the input is a bit different, the dictionary can be different as well. But dictionaries are not ordered in Python, so that means no one can make assumptions with that one. Both produce the same dictionary:

>>> str.maketrans( 
...     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 
...     "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm")
{65: 78, 66: 79, 67: 80, 68: 81, 69: 82, 70: 83, 71: 84, 72: 85, 73: 86, 74: 87, 75: 88, 76: 89, 77: 90, 78: 65, 79: 66, 80: 67, 81: 68, 82: 69, 83: 70, 84: 71, 85: 72, 86: 73, 87: 74, 88: 75, 89: 76, 90: 77, 97: 110, 98: 111, 99: 112, 100: 113, 101: 114, 102: 115, 103: 116, 104: 117, 105: 118, 106: 119, 107: 120, 108: 121, 109: 122, 110: 97, 111: 98, 112: 99, 113: 100, 114: 101, 115: 102, 116: 103, 117: 104, 118: 105, 119: 106, 120: 107, 121: 108, 122: 109}
>>> str.maketrans( 
...     "ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz", 
...     "NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")
{65: 78, 66: 79, 67: 80, 68: 81, 69: 82, 70: 83, 71: 84, 72: 85, 73: 86, 74: 87, 75: 88, 76: 89, 77: 90, 78: 65, 79: 66, 80: 67, 81: 68, 82: 69, 83: 70, 84: 71, 85: 72, 86: 73, 87: 74, 88: 75, 89: 76, 90: 77, 97: 110, 98: 111, 99: 112, 100: 113, 101: 114, 102: 115, 103: 116, 104: 117, 105: 118, 106: 119, 107: 120, 108: 121, 109: 122, 110: 97, 111: 98, 112: 99, 113: 100, 114: 101, 115: 102, 116: 103, 117: 104, 118: 105, 119: 106, 120: 107, 121: 108, 122: 109}

The reason I think is that it is easier to verify for the human eye that the string is indeed shifted 13 positions. The alphabet contains 26 characters, and it is halfway between 'm' and 'n'. If we write:

rot13 = string.maketrans( 
    "ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz", 
    "NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")

Notice the characters put in boldface. One can see that where the character sequence on the first line ends, it continues on the second line and vice versa. So here we have 2 points in the source code where we can easily check that we are still correct. This is easier than calculating 13 places forward and backward.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555