본문 바로가기

#WarGame/Python Challenge

Python Challenge # LV.4

반응형

출처) http://www.pythonchallenge.com/index.php

 

The Python Challenge

What people have said about us: "These sorts of things are in my opinion the best way to learn a language.", brberg at Media Cloisters "It's the best web site of the year so far.", Andy Todd at halfcooked "Addictive way to learn the ins and outs of Python.

www.pythonchallenge.com

 

http://www.pythonchallenge.com/pc/def/linkedlist.php

[그림1. LV.4 문제]

 

  • 파이썬 챌린지 레벨4 문제!
  • 저번 레벨 3에서 URL경로로 가면 linkedlist.php라는 문구가 나온다.
  • 이번 문제에서는 힌트가 적힌 문자열이 보이지 않는다.
  • 그림을 클릭해보니 다른 페이지로 넘어간다.

 

 

[그림2. nothing=12345]

http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345 주소로 넘어간 후 문자열 나왔다.

"and the next nothing is 44827"

url 주소 제일 마지막12345를 44827로 바꾸어 보니 또 다른 숫자를 가진 문자열이 나온다.

3번째쯤 반복해보니 이런 문자열이 나온다.

 

 

 

 

[그림3. nothing=45439]

 

"당신의 손은 점점 피곤해지고 있다......."

  • 여기서 부터 파이썬으로 구현해야 하는 문제인것 같다.
  • 다시 처음으로 돌아와 페이지소스를 봤다.
  • body부분에 주석이 있다.

 

[그림4. page source]

"urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough."

---> urllib가 도움이 될 수 있다. 절대 끝나지 않을 테니 모든 걸 시도하지 마라. 400배면 충분하다.

 

 

  • 다음과 같은 코드를 구현해보았다.
from urllib.request import urlopen
import re

uri= "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s"

num="12345"

while True:
    content=urlopen(uri % num).read().decode('utf-8')
    pattern="next nothing is (\d+)"
    found=re.findall(pattern,content)
    
    if found :
        num=" ".join(found)
        print(content)
    else:
        print(content)
        break
        

 

  • from 패키지.모듈 import 함수 --> from from urllib.request import urlopen
  • python3에서 urllib모듈을 import urllib.request 이렇게 써야한다.
  • urllib은 표준 라이브러리이므로 따로 설치할 필요가 없다.

 

  • 코드를 해석해보면 먼저 uri와 num을 선언한다.
  • uri 맨 끝 nothing=???에서 ???가 계속 바뀌므로 %s로 둔다.
  • 문자열과 정수형이 같이 출력할 수 없으므로 num을 문자열로 선언한다.
  • 400배면 충분하다고 했으니 while문으로 패턴이 일치할 때까지 돌려본다.
  • uri 주소에서 %s에 num값을 넣고 utf-8로 디코딩하여 읽은 값을 content 변수에 저장하였다.-->디코딩을 하지 않고 출력하면 읽을 수 없게 출력되기 때문에 디코딩을 해 주어야 한다.

 

  • 저번 문제에서도 사용했던 정규표현식 re모듈이다.
  • 매칭 표현식으로 \d는 모든 숫자와 매치하는 걸 의미한다. 다르게 쓰면 [0-9]와도 같다.
  • "next nothing is 숫자" 패턴이 일치하는 문자열을 검색하여 맞다면 검색한 숫자를 num에 넣고 content를 출력한다.

 

[그림5. 구현 결과]

 

  • 12345이후 부터 and the next nothing is 문자열이 차례로 출력된다.
  • 마지막에 "Yes. Divide by two and keep going."이라고 나왔다.
  • 16044부터 2를 나누어 계속하라는 것이다.
from urllib.request import urlopen
import re

url= "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=%s"

#num="12345"
num=str(16044/2)

while True:
    content=urlopen(url % num).read().decode('utf-8')
    pattern="next nothing is (\d+)"
    found=re.findall(pattern,content)
    
    if found :
        num=" ".join(found)
    else:
        print(content)
        break
        
    print(content)

 

  • 출력하면 중간에 이런 문자열이 있다.

"There maybe misleading numbers in the text. One example is 82683. Look only for the next nothing and the next nothing is 63579"

 

  • 63579로 설정하고 다시 돌려야 하나 했는데 결과가 나왔다. 그냥 무시해도 될 것 같다.

 

더보기
[그림6. 최종결과]

 

  • 조금만 기다리다 보면 출력결과가 끝난다.
  • 66831 다음 peak.html이라는 문자열이 나온다.

 

 

 

 

 

반응형

'#WarGame > Python Challenge' 카테고리의 다른 글

Python Challenge # LV.6 (ZipFile)  (0) 2020.08.24
Python Challenge # LV.5 (pickle)  (0) 2020.08.20
Python Challenge # LV.3  (0) 2020.08.17
Python Challenge # LV.2  (0) 2020.08.17
Python Challenge # LV.1  (0) 2020.08.16