Python/Python Challenge

[Python Challenge 19] You are An Idiot!

Python Challenge 19의 Url은 다음과 같다

Python challenge 19 : http://www.pythonchallenge.com/pc/hex/bin.html


구성

인도 사진이다. 특별한 게 안보이니까 주석을 보자

MIME Type의 e-mail이 보인다. 아래 있는 데이터로 문제를 풀어나가야 될 것 같다.


해결 아이디어 

우선 붙임파일을 base64 decode 해야 될 거 같다. 하는 김에 반환된 바이트 객체를 indian.wav로 바꾸자

### 19.py

import base64

if __name__=="__main__" :

    with open("hex.txt","r") as f:
        data = f.read()
        data = data.replace("\n","")

    data = base64.b64decode(data)

    with open("indian.wav","wb") as f:
        f.write(data)

hex.txt에 붙임파일을 집어넣어두었다. 하여튼 출력된 indian.wav는 다음과 같다.

indian.wav
0.11MB

 

 

중간에 sorry! 라는 말만 나오는 5초짜리 음성이다. 으흠; 여기서 막혔다. 일단 Python Challenge의 성격상 모든 것을 python으로 처리하니, python의 모듈 wave(wav처리 모듈)을 다운로드하여서 여러 가지 해봤는데, 답이 없다...

해결할 수 있는 키노트는 어이없게도 이 사람이 indian이라는 점이다. 

indian >> endian

그니까 endian type을 바꾸면 된다는 것이다.(지금은 LITTLE이다. BIG으로 바꾸고 오라는 거다)

다음은 코드이다. 모듈은 soundfile이라는 모듈을 이용했다.

https://pysoundfile.readthedocs.io/en/latest/#soundfile.SoundFile

 

SoundFile — PySoundFile 0.10.3post1-1-g0394588 documentation

Parameters: file (str or int or file-like object) – The file to open. This can be a file name, a file descriptor or a Python file object (or a similar object with the methods read()/readinto(), write(), seek() and tell()). mode ({'r', 'r+', 'w', 'w+', 'x

pysoundfile.readthedocs.io

### 19_1.py

import soundfile

if __name__=="__main__" :
    ind = soundfile.SoundFile("indian.wav")

    soundfile.write("big.wav",ind.read(),ind.samplerate,ind.subtype,"BIG",ind.format)

근데.... 파일이 깨진다. 결국 구글 신의 힘을 빌려 알아낸것은 BIG endiantype으로 변형할 때는 파라미터가 변경되어야 한다고 한다. 참나.. 이런 건 어떻게 안다는 말인가? 최종적인 코드는 다음과 같다. soundfile에는 framerate에는 마땅한 파라미터가 없어 처음에 가져온 wave를 다시 이용했다.

### 19_2.py

import wave

if __name__=="__main__" :
    with wave.open("indian.wav","rb") as ori :
        new = wave.open("big.wav","wb")
        new.setnchannels(ori.getnchannels())
        new.setsampwidth(ori.getsampwidth()//2)
        new.setframerate(ori.getframerate()*2)
        frames = ori.readframes(ori.getnframes())
        wave.big_endiana = 1
        new.writeframes(frames)
    new.close()

출력은 다음과 같다.

big.wav
0.11MB

 

 

 

idiot으로 들어가 보면 레오파드씨가 우리를 반겨준다.

다음으로 넘어가자 

Answer Url : http://www.pythonchallenge.com/pc/hex/idiot2.html