简体   繁体   English

来自代码战的 ROT13 Python 3

[英]ROT13 Python 3 from codewars

This is the first time i'm asking problems as i'm a noob in programming.这是我第一次提出问题,因为我是编程新手。

I'm trying to complete problems from Codewars,我正在尝试解决 Codewars 中的问题,

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

Create a function that takes a string and returns the string ciphered with Rot13.创建一个 function,它接受一个字符串并返回使用 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 “实现”中一样。”

I've tried to code it at my best abilities, and it come up like this.我试图尽我最大的能力对其进行编码,结果是这样的。

def rot13(message):
    v = []
    for i in message:
        if i.isupper()== True:
            d = ord(i) + 13
            e = chr(d)
            if d > 90:
                u = (d - 91)
                o = chr(u+65)
                v.append(o)
            else:
                v.append(e)
        elif i.islower() == True:
            x = (ord(i)+13)
            y = chr(x)
        
            if x > 122:
                z = (x - 123)
                p = chr(z+97)
                v.append(p)
            
            else:
                v.append(y)
        else:
            v.append(i)

    print(''.join(v))
        

It worked (seemingly), but it says that it's not correct.它有效(似乎),但它说它不正确。 Where does it wrong?哪里错了? I know it's ugly, but well.我知道这很丑,但还好。 Thank you in advance.先感谢您。

If I'm not mistaken, you're referring to https://www.codewars.com/kata/530e15517bc88ac656000716 .如果我没记错的话,您指的是https://www.codewars.com/kata/530e15517bc88ac656000716 Note what the test case does:注意测试用例做了什么:

test.assert_equals(rot13("test"),"grfg")
test.assert_equals(rot13("Test"),"Grfg")

They don't want you to print the result, they want you to return it.他们不希望您打印结果,而是希望您返回结果。 This matches the task, which says这与任务相匹配,它说

Create a function that takes a string and returns the string ciphered with Rot13创建一个 function 接受一个字符串并返回用 Rot13 加密的字符串

If you simply change the last line from如果您只是将最后一行从

print(''.join(v))

to

return ''.join(v)

it passes the test.它通过了测试。

There are some other questions that go into detail about the differences between printing and returning, for example this one还有一些问题go详细介绍一下打印和退货的区别,比如这个

Edit: Note that your algorithm so far is correct, but only when you assume your input only includes ascii characters .编辑:请注意,您的算法到目前为止是正确的,但仅当您假设您的输入仅包含ascii 字符时。 For non-ascii characters, you might not want to rotate them, but they could be identified as being lower or upper case by python.对于非 ascii 字符,您可能不想旋转它们,但 python 可以将它们识别为小写或大写。 One example would be the German umlauts "ä", "ö" and "ü".一个例子是德语变音符号“ä”、“ö”和“ü”。 For these cases, Matthias and Jon have provided ideas (that also allow a more elegant implementation).对于这些情况,Matthias 和 Jon 提供了一些想法(也允许更优雅的实现)。 Also, you could check out other submissions on Codewars and see how other people did it.此外,您可以查看 Codewars 上的其他提交,看看其他人是如何做到的。

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

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