问题

题目每次随机生成字串和等式,求解等式并按要求生成结果

解决

Python2

import urllib2
import math

def baseconvert(number, fromdigits, todigits):
    if str(number)[0] == '-':
        number = str(number)[1:]
        neg = 1
    else:
        neg = 0
    # make an integer out of the number
    x = long(0)
    for digit in str(number):
        x = x*len(fromdigits) + fromdigits.index(digit)

    # create the result in base 'len(todigits)'
    res = ""
    while x > 0:
        digit = x % len(todigits)
        res = todigits[digit] + res
        x /= len(todigits)
    if neg:
        res = "-"+res
    return res


req = urllib2.Request('http://www.wechall.net/challenge/anto/FlowOverAstronomy/index.php')
req.add_header('Cookie', 'WC=11892678-47832-mNZZCRgxzLvJx66g')
r = urllib2.urlopen(req)
data = r.read()
##########################################
idx = data.index('Charset: ')
charset = ''
if idx != 0:
    idx += len('Charset: ')
    charset = data[idx:idx+69]
    # print "charset : " + charset
##########################################
idx = data.index('Input Base: ')
InputBase = 0
if idx != 0:
    idx += len('Input Base: ')
    InputBase = int(data[idx:idx+2])
    # print "Input Base : ",InputBase
###########################################
idx = data.index('Solution Base: ')
SolutionBase = 0
if idx != 0:
    idx += len('Solution Base: ')
    SolutionBase = int(data[idx:idx+2])
    # print "Solution Base : ",SolutionBase
###########################################
Equation = ''
idx = data.index('Equation')
# print idx
if idx != 0:
    #attention 0A 09 newline,tab
    idxStart = idx + data[idx:].index('">') + 2
    idxEnd = idxStart + data[idxStart:].index('/div') - 1
    Equation = data[idxStart:idxEnd]
    Equation = Equation.replace('<br/>', '')
    # print Equation
###########################################
CalcCharset = charset[0:InputBase]
# print "Calc charset = " + CalcCharset
###########################################
ResultCharset = charset[0:SolutionBase]
# print "Solution charset = " + ResultCharset
BASE10 = "0123456789"
OldIdx = 0
# print(Equation)
NewIdx = Equation.find(' ')
# print(NewIdx)
inst = ''
opcode = ' '
operand1 = 0
operand2 = 0
result = 0
while OldIdx != NewIdx + 1:
    inst = Equation[OldIdx: NewIdx]
    if inst == '*' or inst == '+':
        opcode = inst
        # print opcode,
    else:
        if result == 0:
            operand1 = int(baseconvert(
                Equation[OldIdx: NewIdx], CalcCharset, BASE10))
            result = operand1
            # print operand1,
        else:
            operand2 = int(baseconvert(
                Equation[OldIdx: NewIdx], CalcCharset, BASE10))
            # print operand2,
            if opcode == '*':
                result *= operand2
            elif opcode == '+':
                result += operand2
            inst = ''
            opcode = ''
            operand1 = 0
            operand2 = 0

    OldIdx = NewIdx + 1
    #in final loop, NewIdx equal the last space, OldIdx equal first letter of the end word
    NewIdx = OldIdx + Equation[OldIdx:].find(' ')

#deal with the last operand
NewIdx = Equation.rfind(' ') + 1
operand2 = int(baseconvert(Equation[NewIdx:], CalcCharset, BASE10))
# print operand2
result += operand2
FinalResult = baseconvert(str(result), BASE10, ResultCharset)
submiturl = 'http://www.wechall.net/challenge/anto/FlowOverAstronomy/index.php?answer=' + FinalResult + '&action=Submit'
req2 = urllib2.Request(submiturl)
req2.add_header('Cookie', 'WC=11892678-47832-mNZZCRgxzLvJx66g')
r2 = urllib2.urlopen(req2)
data2 = r2.read()
print data2
# print FinalResult, result

# idx = data2.find('would have been ') + len('would have been ')
# RealResult = data2[idx: idx + data2[idx:].find('.<')]
# print RealResult, baseconvert(RealResult, ResultCharset, BASE10)

代码的内容很长,但实际上什么都没有。
访问部分HTML页面,读取Charset,input base,solution base,equation,
将读取值转换为十进制数并计算,再转换为Solution Base并提交。

ps:时间有限,暂时不打算将代码转成python3.

注意:网络问题可能会出现超时,多试几次即可。


转载:
http://m.blog.naver.com/dual5651/60131982546