简体   繁体   English

Python 2.7:ROT13字母替换密码

[英]Python 2.7: ROT13 letter substitution cipher

Problem Statement: 问题陈述:

ROT13 is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13是一种简单的字母替换密码,它将字母中的字母替换为后面的13个字母。 ROT13 is an example of the Caesar cipher. ROT13是凯撒密码的一个示例。

Create a function that takes a string and returns the string ciphered with Rot13. 创建一个接受字符串并返回用Rot13加密的字符串的函数。 If there are numbers or special characters included in the string, they should be returned as they are. 如果字符串中包含数字或特殊字符,则应按原样返回它们。 Only letters from the latin/english alphabet should be shifted, like in the original Rot13 "implementation". 像原来的Rot13“实现”中一样,仅应转换来自拉丁语/英语字母的字母。

Please note that using "encode" in Python is considered cheating. 请注意,在Python中使用“编码”被认为是作弊行为。

My code: 我的代码:

from string import *

def rot13(message):
    melist = list(message)

    for i in message:
        lpos = index(letters,i)

        if i.islower():
            print lpos
            melist[index(message,i)] =  lower(letters[lpos + 13])

        elif i.isupper():
            try:
                melist[index(message,i)] =  upper(letters[lpos + 13])
            except IndexError:
                melist[index(message,i)] =  upper(letters[lpos + 13 - 52])

    return ''.join(melist)

This thing works fine for converting a string except for the last letter 这个东西可以很好地转换字符串,除了最后一个字母

for example if you do: 例如,如果您这样做:

>>> rot13('test') 
    'grft'

The last character 't' is unchanged although it should, I don't know where I did it wrong. 最后一个字符“ t”虽然没有变化,但没有改变,我不知道我在哪里做错了。 Some help will be greatly appreciated. 一些帮助将不胜感激。

The problem here is not the last letter. 这里的问题不是最后一个字母。 This part of the code will always find the first occurence of a letter in your string: 这部分代码将始终查找字符串中字母的首次出现:

letters.index(i)

So instead of giving the index of the last 't' in 'test' , you get the first index, even if you give 'texttttttt' as input - all the last 't' will stay untouched. 因此,而不是给予最后的索引't''test' ,你得到的第一个指数,即使你给'texttttttt'输入-所有过去的't'将保持不变。

You have to do some changes in your algorithm... 您必须对算法进行一些更改...

Here is a very simple version of rot13 that uses the translate function. 这是使用翻译功能的rot13的非常简单的版本。

import string
def rot13(message):
    norm=string.maketrans('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', \
                          'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM')
    return message.translate(norm)

print rot13("now is the time FOR ALL GOOD")

Output is 输出是
abj vf gur gvzr SBE NYY TBBQ abj vf gur gvzr SBE NYY TBBQ

The problem is not with the last letter, the problem is with duplicate letters: 问题不是最后一个字母,而是重复的字母:

>> print rot13('abba')
noba

The cause is the index function: 原因是index函数:

    index(message,i)

It returns the position of the first occurrence of i in message . 它返回imessage 第一次出现的位置。 A solution would be to use the optional start argument for index() , to specify that search should begin at the specified position: 一种解决方案是对index()使用可选的start参数,以指定搜索应从指定位置开始:

for j,i in enumerate(message):
    lpos = index(letters,i)

    if i.islower():
        print lpos

        // this will find the correct position of the current occurence of i
        melist[index(message,i,j)] =  lower(letters[lpos + 13])
    ...

Your problem comes from the use of 'index' method ! 您的问题来自使用“索引”方法!

In your example the second 't' is processed but the result will go in the same place as the first 't' in your list. 在您的示例中,处理了第二个“ t”,但结果将与列表中第一个“ t”位于相同的位置。

I reckon there's no use for index here, you can simply start with an empty, and append each letter to the end of your list, likewise : 我认为这里的索引没有用,您可以简单地以空开头,然后将每个字母附加到列表的末尾,同样:

melist = []
for i in message:
    if i.islower():
        melist.append(lower(letters[lpos + 13]))
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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