Python/2.7 simple coding(+ c++)

hack this site Programming missions : String manipulation

qkqhxla1 2015. 1. 21. 15:10

This level is about string manipulation.

In this challenge, you will be given a string. Take all the numbers from the string and classify them as composite numbers or prime numbers. You should assume all numbers are one digit, and neither number 1 nor number 0 counts. Find the sum of every composite number, then find the sum of every prime number. Multiply these sums together. Then, take the first 25 non-numeric characters of the given string and increment their ASCII value by one (for example, # becomes $). Take these 25 characters and concatenate the product to them. This is your answer.

Your answer should look like this: oc{lujxdpb%jvqrt{luruudtx140224


랜덤으로 생성되는 스트링을 받아와서, 합성수와 소수로 나뉘어야 한다. 각각 숫자 한글자씩으로 인식하며, 1,0도 마찬가지다. 합성수의 전체 합과, 소수의 전체 합을 구해 두개를 서로 곱해라. 그다음에 처음부터 숫자가 아닌 25개의 문자를 뽑은 다음 그것들의 아스키코드값을 각각 1씩 증가시킨 문자를 가져와라.(예로 #이면 %로.) 그리고 그 25개의 문자를 위에 서로 두개 곱한 값과 연결해서 보내라. 이게 답이다. 이런 형식이 되어야 한다 oc{lujxdpb%jvqrt{luruudtx140224



이번문제는 참 이상하게도 직접 보내기 버튼을 눌러서 문자열(답)을 전송하면 답은 맞았는데 시간이 늦었다고 한다. 요구하는 문자열이 잘 나오는데 비해 프로그래밍적으로 전송하면 그냥 처음 페이지가 보인다. 알고리즘은 완성해서 잘 되는걸 확인했는데, 왜 보내는 부분이 안 되는지 모르겠다.(이전 페이지와 똑같은 방법을 썼음.) 계속 해본결과 프로그래밍적으로 POST방식으로 보낸 데이터를 받아들이지를 못한다. 그냥 일반적으로 페이지 요청한듯한 페이지밖에 안뜬다. 복붙하면서 인코딩문제인가.. 해봤지만 11번,1번 등의 문제로 바꾸면 잘 되는 것으로 보아 12번 페이지만의 또 다른 문제인것 같다. 그래도 풀긴 풀었으니 올림.


# -*- encoding: cp949 -*-
import urllib2,re
req = urllib2.Request('https://www.hackthissite.org/missions/prog/12/index.php')
req.add_header('cookie','PHPSESSID=쿠키')
req.add_header('referer','https://www.hackthissite.org/missions/prog/12/')
s = urllib2.urlopen(req).read().split('value="')[1].split('" /><br /><br />')[0]
compo = 0; prime = 0; cnt = 0; string = ''
for i in range(len(s)):
    try:
        num = int(s[i])
        flag = 1
        for j in range(1,num):
            for k in range(1,j):
                if num%k==0 and k!=1:
                    flag = 0
                    break
            if j==num-1 and flag:
                prime += num
        if flag==0 and num!=1 and num!=0:
            compo += num
    except:
        if cnt < 25:
            string += chr(ord(s[i])+1)
        cnt += 1
multi = compo * prime
answer = urllib2.quote(string+str(multi))
print answer

req = urllib2.Request('https://www.hackthissite.org/missions/prog/11/','solution='+answer)
req.add_header('cookie','PHPSESSID=쿠키')
req.add_header('referer','https://www.hackthissite.org/missions/prog/11/index.php')
print urllib2.urlopen(req).read()[:10000]