Skip to content

Instantly share code, notes, and snippets.

@felipap
Created January 21, 2015 19:42
Show Gist options
  • Save felipap/d80caaff56e43e376a3c to your computer and use it in GitHub Desktop.
Save felipap/d80caaff56e43e376a3c to your computer and use it in GitHub Desktop.
Old code from 2010. :)
'''
Created on 18/06/2010
@author: Felipe
'''
#REMENBER TO COMMENT THE CODE!!!
def separate(eq,G,icox,icoy):
'''Separates the coeficients and sums of both equations and returns'''
#removes the spaces and *s of the equation
eq = eq.replace(' ','').replace('*','')
#isolates the last member, assign its value to S and removes it from eq
for k in eq:
if k == '=':
S = int(eq[eq.index('=')+1:])
eq = eq.replace(eq[eq.index('='):],'')
##isolating each ax and by
try:secondminusposition = eq.index('-',1)
except:
try: secondminusposition = eq.index('+',1)
except: secondminusposition = None
by = eq[secondminusposition:]
eq = eq.replace(by,'')
ax = eq
incx = incy = ''
#isolating a,x,b,y
a = b = ''
S = str(S)
if ax[0] == '+': ax = ax.replace('+','',1)
if by[0] == '+': by = by.replace('+','',1)
if S[0] == '+': S = S.replace('+','',1)
####################################### WHAT MATTERS!
consta = constb = False
if "*" in eq or "/" in eq:
hmt = 0
for k in G.keys():
if ax.count(k)!=0:
assert ax.count(k)>1, 'unsupported formating'
incx = ax[ax.find(k):ax.find(k)+len(k)]
assert ax.find(k)==0 and ax.find(k)+len(k)==0, 'unsupported formating'
a = ax.replace(incx,'')
consta = True
if "/" in eq:
assert ax == "%s/%s" % (a,incx) or ax == "%s/%s" % (incx,a), 'unsupported formating'
if ax == "%s/%s" % (a,incx):
icox = 1/int(icox)
elif ax == "%s/%s" % (incx,a):
a = 1/int(a)
else: consta = False
print (ax,a,x)
for k in G.keys():
if by.count(k)!=0:
assert by.count(k)>1, 'unsupported formating'
incy = by[by.find(k):by.find(k)+len(k)]
assert by.find(k)==0 and by.find(k)+len(k)==0, 'unsupported formating'
b = by.replace(incy,'')
constb = True
if "/" in eq:
assert by == "%s/%s" % (b,incy) or by == "%s/%s" % (incy,b), 'unsupported formating'
if by == "%s/%s" % (b,incy):
icoy = 1/int(icoy)
elif by == "%s/%s" % (incy,b):
b = 1/int(b)
else: constb = False
############## DEVISE
elif consta == False or constb == False:
if consta == False :
a = ax.replace(str(icox),'')
if "/" in a:
assert a.replace("/",'').isnumeric() == b.replace("/",'') == True
if ax == "%s/%s" % (icox,a.replace("/",'')) and a.replace("/",'').isnumeric() == True:
a = 1/int(a.replace("/",''))
if constb == False:
b = by.replace(str(icoy),'')
if "/" in b:
assert a.replace("/",'').isnum() == b.replace("/",'') == True
if by == "%s/%s" % (icoy,b.replace("/",'')) and b.replace("/",'').isnumeric() == True:
b = 1/int(b.replace("/",''))
##
## if not "*" in eq:
## for k in ax:
## if k.isnumeric()==True or k == '-' or k == '.': a = a + str(k)
## else: incx = incx + str(k)
## #
## for k in by:
## if k.isnumeric()==True or k == '-' or k == '.': b = b + str(k)
## else: incy = incy + str(k)
if a == '': a = 1
if b == '': b = 1
#print (a,b)
return a,b,S,icox,icoy
###############################END OF WHAT MATTERS =[
def solve(a,b,S,m,n,S_2):
''' Solves any linear equation. Variables are ax+by=S; mx+ny=S_2 '''
y= ((S*m)-(S_2*a))/((m*b)-(n*a))
x = (S-b*y)/a
return x,y
##program
## constants setting
def cons_ass():
''' sets the constants values to a dictionary G (returned) in a format G[constant]=value'''
empty = input('Hit enter. ')
print('To stop defining new variables, hit enter when asked for a new constant name')
def assertingpart(g):
key = input('Enter the constant %s name.\n' % g)
# print(".",key,".",type(key))
if key == empty or key == ' ':
print ('Ending constants setting.')
return None
related = input('Enter the constant %s (%s) value.\n' % (g,key))
if related == empty or key == ' ':
print ('invalid related value input. restarting...')
return assertingpart(g)
if key.isdigit == True or key.isalnum == False:
print('invalid key input. restarting...')
return assertingpart(g)
return key,related
G = {}
g_false = False
g = 0
print('\n---------Alert!\n Instruction[1]! Maximum number of coeficients that can be used in a single system is 4, and so it is about constants')
print('Instruction[2]! Non-used constants will be ignored. You can set as many constants as you want before overflowing the computer\'s memory')
print('Instruction[3]! When using constants, the * sign MUST always be used (in order to express multiplications)\nEnd of the alert!------------\n')
while g_false == False:
g += 1
output = assertingpart(g)
if output != None:
g_varname, g_varnum = output
G[g_varname]=g_varnum
else:
g_false = True
return G
yeslist = []
nolist = []
for k in open('formatofyes'): yeslist.append(k.strip())
for k in open('formatofno'): nolist.append(k.strip())
empty = input('Press enter... ')
right_format = False
cons_list = {}
while right_format == False:
ans1 = input('Do you want to pre-define constants names? (Y/N)')
if ans1.isalnum()== True and ans1.isspace()==False:
if ans1 in yeslist:
ans = 'Y'
right_format = True
cons_list = cons_ass()
if ans1 in nolist:
ans = 'N'
right_format = True
else:
print ('error: non-recognized entry.')
# end of constants setting
equation_1 = input('Enter the first equation in the form of ax+bx=c\n')
equation_2 = input('Enter the second equation in the same previous form\n')
xt = input('Enter the first variable name to be used in the equatioS...')
yt = input('Enter the second variable name to be used in the equationS...')
assert x != y, 'the variables can not have the same name'
a,b,S,incx,incy = separate(equation_1,cons_list,xt,yt)
m,n,S_2,incx2,incy2 = separate(equation_2,cons_list,xt,yt)
#calculando erro de posicionamento de variável e consertando: \/
if incx != incx2:
assert incy != incy2, 'variables error'
assert incx == incy2, 'variables error'
assert incx != incy, 'variables error'
if incx == incy2:
k3,k4,c3,c4 = incx2,incy2,m,n
incy2,n,incx2,m=k3,c3,k4,c4
assert incx == incx2 and incy == incy2, 'Variables Error finale'
print (a,b,m,n,S,S_2)
a = float(a)
b = float(b)
m = float(m)
n = float(n)
S = int(S)
S_2 = int(S_2)
x,y = solve(a,b,S,m,n,S_2)
print ('''The solution for the binary system of linear equations:
%s*%s+%s*%s = %s
%s*%s+%s*%s = %s
Is the pair (%s,%s): %s,%s''' % (a,xt,b,yt,S,m,xt,n,yt,S_2,xt,yt,x,y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment