Created
June 15, 2010 13:10
-
-
Save whs/439099 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re, sys | |
def seperate(e): | |
"""seperate the equalization: | |
eg. 2x^2+3x-2 -> ["2x^2", "3x", "-2"]""" | |
s = re.split(r"([+-])", e) | |
opt = [] | |
nextMinus=False | |
if s[0] == "": | |
s = s[1:] | |
for i in range(len(s)): | |
if s[i] not in ("+", "-"): | |
a=s[i] | |
if nextMinus: | |
a="-"+a | |
nextMinus = False | |
opt.append(a) | |
else: | |
if s[i] == "-": | |
nextMinus = True | |
return opt | |
def writeWithSign(num): | |
if num > 0: | |
return "+"+str(num) | |
else: | |
return str(num) | |
def getDegree(s): | |
degree = re.findall(r"^([\-0-9]+)", s) | |
if not degree: | |
return 1 | |
else: | |
if degree[0] == "-": | |
return 1 | |
return int(degree[0]) | |
def getVar(s): | |
var = re.findall("([a-zA-Z]+)", s) | |
try: | |
return var[0] | |
except IndexError: | |
return "x" | |
def extract(s, eq="N/A"): | |
degrees = [] | |
var = getVar(s[0]) | |
# 1 -> find the degree | |
degrees.append(getDegree(s[0])) | |
degrees.append(getDegree(s[2])) | |
# 2 -> find the second degree | |
target = getDegree(s[1]) | |
# 3 -> brute force! | |
found = None | |
sides = [] | |
if degrees[0] < 0: | |
sides.append(-1) | |
else: | |
sides.append(1) | |
if degrees[1] < 0: | |
sides.append(-1) | |
else: | |
sides.append(1) | |
for i in xrange(sides[0], degrees[0]+sides[0], sides[0]): | |
if found != None: | |
break | |
i2 = float(degrees[0])/float(i) | |
if i2 != int(i2): | |
continue | |
sys.stderr.write(`i`+" "+`i2`+"\n") | |
for j in xrange(1, abs(degrees[1])+1): | |
j2 = float(degrees[1])/float(j) | |
if j2 != int(j2): | |
continue | |
sys.stderr.write("-> "+`j`+" "+`j2`+"\n") | |
sys.stderr.write("--> "+`abs(j)*-1`+" "+`abs(j2)*-1`+" "+`degrees[1] == abs(degrees[1])`+"\n") | |
if (i * j)+(i2 * j2) == float(target): | |
found = [[i, i2], [j, j2]] | |
break | |
elif (i * (abs(j)*-1))+(i2 * (abs(j2)*-1)) == float(target) and degrees[1] == abs(degrees[1]): | |
found = [[i, i2], [abs(j)*-1, abs(j2)*-1]] | |
break | |
elif ((abs(i)*-1) * (abs(j)*-1))+((abs(i2)*-1) * (abs(j2)*-1)) == float(target) and degrees[1] == abs(degrees[1]): | |
found = [[(abs(i)*-1), (abs(i2)*-1)], [abs(j)*-1, abs(j2)*-1]] | |
break | |
elif ((abs(i)*-1) * j)+((abs(i2)*-1) * j2) == float(target) and degrees[1] == abs(degrees[1]): | |
found = [[(abs(i)*-1), (abs(i2)*-1)], [j, j2]] | |
break | |
#else: | |
# print "Not "+`target`+": "+`[[i, i2], [j, j2]]` | |
# 4 -> now cross and show the output | |
out = "" | |
out += "%s:\t%s\t->\t%s\t%s\n\t\t%s\t->\t%s\t%s\n"%( | |
eq, degrees[0], found[0][0], found[0][1], | |
degrees[1], found[1][0], found[1][1] | |
) | |
if found[0][0] == 1: | |
found[0][0] = "" | |
else: | |
found[0][0] = int(found[0][0]) | |
if found[0][1] == 1: | |
found[0][1] = "" | |
else: | |
found[0][1] = int(found[0][1]) | |
out += "%s:\t(%s%s%s)(%s%s%s)\n"%( | |
eq, found[0][0], var, | |
writeWithSign(int(found[1][1])), found[0][1], var, | |
writeWithSign(int(found[1][0]))) | |
return out | |
if __name__ == "__main__": | |
if len(sys.argv) > 1: | |
eq = [sys.argv[1]] | |
else: | |
eq = [ | |
'2x^2+5x+2', | |
'2x^2+5x-3', | |
'2x^2+5x-12', | |
'3x^2+19x-40', | |
'9x^2-42x+49', | |
'12x^2-25x+7', | |
'16x^2+88x+121', | |
'3x^2+19x+6', | |
'2x^2-x-6', | |
'11x^2-12x-20', | |
'3x^2-16x+16', | |
'12x^2-23x+5', | |
'7x^2-40x-12', | |
'24x^2-13x-2', | |
'13x^2-16x-20', | |
'6x^2-x-12', | |
'81x^2+0-169', | |
'8x^2-34x+33', | |
'8x^2-34x+21', | |
'12x^2+8x-7', | |
'6x^2+7x-3', | |
'-18x^2+3x+28', | |
'-10x^2-11x+6', | |
'8x^2+2x-15', | |
] | |
# another set | |
eq += [ | |
'-36x^2+19x+6', | |
'-12x^2-x+6', | |
'40x^2-50x-35', | |
'3x^2+30x+75', | |
'3x^2-6x-9', | |
'9y^2-6y+1', | |
'6a^2+a-12', | |
'12a^2+41a+35', | |
'12a^2-a-35', | |
'-12a^2-a+35', | |
'5x^2+4x-1', | |
'12x^2-107x-9', | |
'35m^2+18m-8', | |
'x^2-7x+6', | |
'49y^2-42y+9', | |
'16x^2-8x+1', | |
'-3z^2+2z+5', | |
'15x^2+8x-7', | |
'6x^2+31x-52', | |
'25z^2+0-121', | |
'9x^2-12x-5', | |
#'4x^2+16x-2', #bug! | |
'-6x^2+10x+4', | |
'3b^2-26b+35', | |
'12x^2+0-75', | |
'6a^2+17a+12', | |
'6a^2-a-12', | |
] | |
for e in eq: | |
s = seperate(e) | |
print extract(s, e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment