The Python Challenge – Level 6

문제 페이지 : http://www.pythonchallenge.com/pc/def/channel.html

먼저 주석을 보면 ZIP 이라는 단어가 있기에 다음 페이지에 접속하여 보니 ZIP 파일을 찾으라고 한다.
http://www.pythonchallenge.com/pc/def/zip.html

http://www.pythonchallenge.com/pc/def/channel.zip 파일이란게 있는 것을 확인했고 readme.txt 파일을 확인하여 보면 시작파일이 어떤 파일인지 확인할 수 있다. 각 파일을 읽어가며 따라가면 최종적으로 "Collect the comments." 라는 메세지가 나온다.

한참을 고민한 결과 zip 파일 내부에 각 파일에 대해 코멘트가 달려 있는것을 확인할 수 있었다. 이제 다시 아까의 순서대로 ZIP 파일에 달린 코멘트를 출력하는 코드를 작성하여 보자.

#!c:\python26\python.exe
import re, zipfile

def search_comment(name):
    zfileloc = 'channel.zip'
    a = zipfile.ZipFile(zfileloc)

    for f in a.infolist():
        tmp = str(f.filename)
        if tmp == name:
            return str(f.comment)
            break

def gogo(number):
    file = open('channel\\' + str(number) + '.txt', 'r')
    tmp = file.read()
    print tmp

    get = re.search('Next nothing is [\d]+', tmp).group()
    get = re.search('[\d]+', get).group()
    return int(get)

try:
    output = ''
    x = gogo(90052)
    file = str(x) + '.txt'
    output += search_comment(file)

    while 1:
        x = gogo(x)
        output += search_comment(str(x) + '.txt')

except:
    print ''
    print output

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다