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挑战:00~03关

    Python Challenge Python Challenge 00 网址: http://www.pytho...

  • The Python Challenge(1)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 求出图中2^38的结果,替换问题连接中的相应字符串即可:

  • Python Challenge[1]

    [Level 1] Title: What about making trans? 图片下方有两段话,但只有第一段...

  • Python挑战:04-05关

    Python Challenge Python Challenge 04 现在,我们来挑战第四关,从第三关的结果,...

  • Python Challenge 第1关

    第1关 根据图片可以看出每个字母往后推移了2步,得到后面的字母。后面给了一段话,嗯~~~ 意思很明显了,让我们根据...

  • The Python Challenge(5)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 根据页面源码提示: 再点击页面图片显示: 可知是需要...

  • The Python Challenge(8)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 页面和源码中无任何提示,但图片中有一条很明显的灰度线...

  • The Python Challenge(9)

    问题链接 问题链接如下: 答案链接 答案链接如下: 登陆用户名密码为huge和file。 解题思路 阅读源码有如下...

  • The Python Challenge(2)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 将页面给定的字符串根据给定规则进行替换即可,规则如下...

  • The Python Challenge(3)

    问题链接 问题链接如下: 答案链接 答案链接如下: 解题思路 根据页面提示: 阅读源码,有如下内容: 编写代码从中...

网友评论

    本文标题:Python Challenge[1]

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