As student it can be boring to do math, especially if its just repeating the same thing over and over. Well, to solve that problem, I created a function to solve many equations at a time, using well – more math. Here I use string manipulation methods in order to solve these proportion equations in a second.
The first step would be to determine the input and output of the program. In this case, our input is a multi line string, and our output is a float.
# input - string
# output - float
Then we need to break our long multi line string into a list, in which the elements will be the equations that are on separate lines. We can do this by using the split() method.
equation_list = '''12/6 = 16/x
5/5 = 13/x
6/2 = 6/x
15/7 = 6/x
14/11 = 4/x
6/16 = 13/x
4/8 = 15/x
18/19 = 8/x
2/8 = 8/x
9/2 = 5/x'''.split("\n")
Next, we need to separate the different numbers in our equations, so that we can use them to solve the proportion. To do this, we can use the split() method again, this time to split it first in to two sides using the equal sign, then using the hyphen.
for equa in equation_list:
equation = equa
first_expr = equation.split(" = ")[0].split("/")
Since our input is a string, we need to convert each element in our list of values to a integer, so it’s easier to work with.
first_expr2 = []
for i in first_expr:
first_expr2.append(int(i))
Finally, to solve the equation we need to divide the numerator of the rightmost fraction by the quotient of the fraction on the left.
equation2 = equation.split("/x")
dividend = int(equation2[0].split(" = ")[1])
print(equa)
print(dividend / (first_expr2[0]/first_expr2[1]))