given an equation of the form "num1+num2=nim3" where one of num1, num2, num3 is replaced by an x. find the value of x
Answers
Consider the given equation num 1+num 2 = num 3
where one of num1, num2, num3 is replaced by an x.
Case 1: Let us suppose num 1 = x
Therefore, the given equation is:
x + num 2 = num 3
So, x = num 3 - num 2
Therefore, the value of x is "num 3 - num 2".
Case 2: Let us suppose num 2 = x
Therefore, the given equation is:
num 1 + x = num 3
So, x = num 3 - num 1
Therefore, the value of x is "num 3 - num 1".
Case 3: Let us suppose num 3 = x
Therefore, the given equation is:
num 1 + num 2 = x
So, x = num 1 + num 2
Therefore, the value of x is "num 1 + num 2".
If It is given that the equation will definitely be in this form then :
1) '+' sign exist btw num1 and num2.
2) '=' sign exist btw num2 and num3.
Based on this the code will be :
str1 = input("enter equation: ")
num1='p'
num2='q'
num3='r'
for i in range(len(str1)):
if str1[i]=='+':
posi_plus = i
if str1[i-1] == 'x':
num1 = 'x'
elif str1[i+1] == 'x':
num2 = 'x'
else:
num3 = 'x'
if str1[i] == '=':
posi_eq = i
break
a = str1[:posi_plus ]
b = str1[posi_plus+1 : posi_eq ]
c = str1[posi_eq+1:]
if num1 == 'x':
print(int(c)-int(b))
elif num2 == 'x':
print(int(c)-int(a))
else:
print(int(a)+int(b))
Note : I have not added the corner test cases like x+2x=10,x=0 etc ; This code is just for the basic and T(n) test cases.