Python Challenge[1]

作者: Recgat | 来源:发表于2017-02-06 17:32 被阅读0次

    [Level 1]


    Title: What about making trans?

    图片下方有两段话,但只有第一段能识别:

    everybody thinks twice before solving this.

    而图中K→M,O→Q,E→G,理清其中映射关系,思路就有了。

    import string
    table = str.maketrans(string.ascii_lowercase,string.ascii_lowercase[2:] + 'ab')
    s.translate(table)#s是要转换的字符串
    

    转换第二段话得到:

    i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url.

    转换url中的map,得到ocr[Level 2]

    小结

    使用转换表转换每一个字符。

    1. string.ascii_lowercase表示26个顺序小写字母
    2. static str.maketrans(x[, y[, z]]),方法返回用于str.translate()的转换表。
      只一个参数时,类型必须是dict,键为Unicode ordinals(整数)或字符,值为任意长度字符串或者None,键对应的字符将被转换为值对应的字符(串);如果有两个参数,参数长度必须相等且x中每一个字符对应转换y中每一个字符;如果有第三个参数,对应的字符转换为None。
    3. str.translate(table)返回通过给定的翻译表映射每个字符的字符串的副本。

    Python Challenge Wiki

    1. 使用chr()和ord()列表解析

    • "".join(map(lambda x: x.isalpha() and chr((ord(x)+2-ord("a"))%26 + ord("a")) or x, input))
    • "".join([(c.isalpha() and chr(ord('a') + (ord(c)-ord('a')+2)%26) or c) for c in "map"])
    思路小解

    相同的思路,在Ascii码和字符之间进行转换得到结果。

    1. str.isalpha()函数判断字符串是否是字母。
    2. chr(i)返回表示Unicode码点为整数i的字符的字符串;ord(c)返回一个表示该字符的Unicode代码点的整数。
    3. map(function, iterable, ...),应用function方法,(并行)返回参数中的迭代元素。
    4. lambda表达式相当于创建一个匿名函数。
    5. str.join(iterable),返回迭代器里的所有字符串。

    More

    相关文章

      网友评论

        本文标题:Python Challenge[1]

        本文链接:https://www.haomeiwen.com/subject/dksuittx.html