Solve Equations
Solve Equations
Problem:
Given two equations EQN1 and EQN2 containing the values of x co-efficient, y co-efficient and the resulting constant, the program must print the value of x and y separated by a space.
Input Format:
The first line contains the equation EQN1. The second line contains the equation EQN2.
Output Format:
The first line contains the value of x and y separated by a space.
Boundary Conditions:
1 <= Co-efficient values <= 1000
Example Input/Output 1:
Input:
5x+2y=14
-4y+3x=-2
Output:
2 2
Example Input/Output 2:
Input:
4Y-5X=17
3X+4Y=9
Output:
-1 3
Program:
def extractnum(s):
a = ""
k = []
for i in s:
if i not in "xy= ":
a += i
else:
if a!= "":
k.append(int(a))
a = ""
if a != "":
k.append(int(a))
return k
def extractorder(s):
a = ""
for i in s:
if i in "xy":
a += i
return a
s = input().strip().lower()
t = input().strip().lower()
a = extractnum(s)
b = extractnum(t)
c = extractorder(s)
d = extractorder(t)
if c == "yx":
a[0],a[1] = a[1],a[0]
if d == "yx":
b[0],b[1] = b[1],b[0]
# to find x
p, q = a.copy(), b.copy()
mul1,mul2 = p[1],q[1]
ans =[]
for i in range(3):
q[i] = q[i]*mul1
p[i] = p[i]*mul2
ans.append(q[i]-p[i])
print(ans[-1]//ans[0],end=" ")
# to find y
p,q = a.copy(), b.copy()
mul1,mul2 = p[0],q[0]
ans = []
for i in range(3):
q[i] = q[i]*mul1
p[i] = p[i]*mul2
ans.append(q[i]-p[i])
print(ans[-1]//ans[1])
Comments
Post a Comment