0
0

More than 3 years have passed since last update.

Python Challenge level 2

Posted at

続けてPython Challenge を解いていきます。

level 2: http://www.pythonchallenge.com/pc/def/ocr.html

Level 2

ocr.jpg

本の画像にキャプションがついています。

recognize the characters. maybe they are in the book, but MAYBE they are in the page source.

これがヒントでしょう。文字を認識せよ。多分その本の中にある、もしくは「多分」このページのソースコードの中にあるだろう。ぐらいの意味ですので、ヒントに従いソースコードを見てみましょう。

ocr.html

<html>
<head>
  <title>ocr</title>
  <link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<center><img src="ocr.jpg">
<br><font color="#c03000">
recognize the characters. maybe they are in the book, <br>but MAYBE they 
are in the page source.</center>

<br>
<br>
<br>

<font size="-1" color="gold">
General tips:
<li>Use the hints. They are helpful, most of the times.</li>
<li>Investigate the data given to you.</li>
<li>Avoid looking for spoilers.</li>
<br>
Forums: <a href="http://www.pythonchallenge.com/forums"/>Python Challenge Forums</a>, 
read before you post.
<br>
IRC: irc.freenode.net #pythonchallenge
<br><br>
To see the solutions to the previous level, replace pc with pcc, i.e. go 
to: http://www.pythonchallenge.com/pcc/def/ocr.html

</body>
</html>

<!--
find rare characters in the mess below:
-->

<!--
%%$@_$^__#)^)&!_+]!*@&^}@[@%]()%+$&[(_@%+%$*^@$^!+]!&_#)_*}{}}!}_]$[%}@[{_@#_^{*
@##&{#&{&)*%(]{{([*}@[@&]+!!*{)!}{%+{))])[!^})+)$]#{*+^((@^@}$[**$&^{$!@#$%)!@(&
+^!{%_$&@^!}$_${)$_#)!({@!)(^}!*^&!$%_&&}&_#&@{)]{+)%*{&*%*&@%$+]!*__(#!*){%&@++
!_)^$&&%#+)}!@!)&^}**#!_$([$!$}#*^}$+&#[{*{}{((#$]{[$[$$()_#}!@}^@_&%^*!){*^^_$^
]@}#%[%!^[^_})+@&}{@*!(@$%$^)}[_!}(*}#}#___}!](@_{{(*#%!%%+*)^+#%}$+_]#}%!**#!^_
)@)$%%^{_%!@(&{!}$_$[)*!^&{}*#{!)@})!*{^&[&$#@)*@#@_@^_#*!@_#})+[^&!@*}^){%%{&#@
...続く

謎の記号列が延々とあります。ヒントとして

find rare characters in the mess below
下部の乱雑記号の中から珍しい文字を探せ。

とあるのでこの中から探していけばいいようです。ただ、どう探していけばいいでしょうかというところで出てくるのが前問で使ったstr.maketransメソッドです。

第 3 引数を指定する場合、文字列を指定する必要があり、それに含まれる文字が None に対応付けられます。

Noneに対応づけられるというのがわかりにくですが、要するにmaketransの第3引数に指定した文字列はtranslateしたときに削除されるということです。

というわけで、pythonプログラムの方針は固まりましたので書いてみましょう。

解答

2.py
#!/usr/bin/env python3

removeChars = "#_!$@*+()[]{}%&^\n"
table = str.maketrans("","",removeChars)

coText="""
%%$@_$^__#)^)&!_+]!*@&^}@[@%]()%+$&[(_@%+%$*^@$^!+]!&_#)_*}{}}!}_]$[%}@[{_@#_^{*
#省略
#@}&$[[%]_&$+)$!%{(}$^$}*
"""

print(coText.translate(table))

出力結果

equality

Level 3

ocr.html を equality.html に書き換えると次のレベルの扉が開かれました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0