I have a program I am writing that calculates Algebra 2 functions and equations. The interface is all CLI, and I even made commands that are similar to shell commands. That's not the part I want review on, so in the interest of space I'm going to consolidate as much as possible. If you want to check out the full program so far, you can see it here.
Tonight, I added a class and a few methods that factors the quadratic formula. The methods are capable of factoring when the
a term is 1, or greater than 1.
Here is the code relevant to the function:
class Factor(Formulas): #---------- factor quadratic equation def FactorQuad(self, aval, bval, cval): # if the a value is equal to 1, we can factor the standard way if aval == 1: common_factor = self.MultSum(bval, cval) if common_factor: print "(x + %s)" % common_factor print "(x + %s)" % common_factor return True else: return False # if the a value is greater than 1, we must factor by grouping else: multval = aval * cval common_factor = self.MultSum(bval, multval) group_one = [aval, common_factor] group_two = [common_factor, cval] # abs() insures the gcd is not negative due to negative values # on the second terms of each group group_one_gcd = fractions.gcd(group_one, abs(group_one)) group_two_gcd = fractions.gcd(group_two, abs(group_two)) # this conditional makes sure not to ignore negative values # on the first terms of each group if group_one < 0: group_one_gcd *= -1 if group_two < 0: group_two_gcd *= -1 set_one = (group_one_gcd, group_two_gcd) set_two = (int(group_one / group_one_gcd), int(group_one / group_one_gcd)) print "(%sx + %s)(%sx + %s)" % (set_one, set_one, set_two, set_two) #---------- multsum method def MultSum(self, bval, cval): """Find the number that multiplies to c and adds to b This is used to factor terms in the quadratic equation """ tries = 0 common_factor = 0 factor = abs(int(cval / 2)) while not common_factor: # first part of condition makes sure factor doesn't cause zero division error if factor and cval % factor == 0 and (factor + (cval / factor)) == bval: common_factor = factor if tries > abs(cval * 2): print "Error: The quadratic equation is not factorable" return False factor -= 1 tries += 1 return (int(common_factor), int(cval / common_factor))
Factorclass inherits from the
Formulasclass which contains various basic calculations that can be used universally across functions and methods (reduce fractions, count significant figures, etc). In these methods shown here, none of the inheritance is used so I figured it was pointless to show the parent class.
Factorclass will soon contain factorization methods for other equations, but right now it is just the quadratic.
It contains two methods, one that factors the quadratic equation (
FactorQuad) and one that finds the factors that multiply to the
cvalue and add to the
MultSumfor lack of a better name).
FactorQuadis separated into two parts - one if the
avalue of the quadratic is 1, which is fairly simple to calculate, and a second part if the
avalue is greater than 1, which requires factoring by grouping.
I am hoping to get two things out of this review post:
I'd love to hear feedback on my code formatting, clarity of comments, and overall "cleanness" of the code. I only recently started writing useful programs over 50 lines long and I know that "clean code" is essential to programming. I'm really interested to see how I could improve formatting, docstrings, and comments.
I would also like to know if there are simpler ways to calculate the factorization. My particular concern is in the
MultSummethod. I feel like there must be a quicker way than looping through and checking a condition in every loop, but I'm missing it. Also, although factoring by grouping is more complicated than the first type of factoring, it seems like it could be shorter.