(Most likely there is no need for the Formulas class either.) All this suggests that there is no need for the Factor class, and FactorQuad and MultSum could just be ordinary functions, not methods. You can see in the examples above that they all start by creating a Factor() object that is thrown away as soon as its method has been called. And the methods FactorQuad and MultSum make no essential use of self. In object-oriented programming, an object represents some thing and a class represents a group of things with similar behaviour.īut an instance of the Factor class doesn't seem to be any kind of thing. This would avoid the need to handle the exceptional return value in FactorQuad. It is generally best in Python to handle exceptional cases by raising an exception, so in MultSum I would write: raise ValueError("Quadratic expression is not factorable.") But FactorQuad neglects to check for this exceptional value. That's because the function MultSum returns the exceptional value False if it fails. TypeError: 'bool' object has no attribute '_getitem_' The code raises TypeError if I ask it to factorize \$2x^2 + 2x + 1\$: > Factor().FactorQuad(2, 2, 1)įile "cr36689.py", line 21, in FactorQuad This is caused by a bug in MultSum when cval is 0: > Factor().MultSum(2, 0) It should print the factorization x(x + 2). The code fails to factorize \$x^2 + 2x\$: > Factor().FactorQuad(1, 2, 0) The problem here is that the loop starts at int(cval / 2), but when cval is 1 then this is 0 and so the factor 1 is never considered. This is caused by a bug in MultSum when cval is 1: > Factor().MultSum(2, 1) It should print the factorization (x + 1)(x + 1). The code fails to factorize \$x^2 + 2x + 1\$: > Factor().FactorQuad(1, 2, 1)Įrror: The quadratic equation is not factorable Also, although factoring by grouping is more complicated than the first type of factoring, it seems like it could be shorter. I feel like there must be a quicker way than looping through and checking a condition in every loop, but I'm missing it. My particular concern is in the MultSum method. I would also like to know if there are simpler ways to calculate the factorization. I'm really interested to see how I could improve formatting, docstrings, and comments. I only recently started writing useful programs over 50 lines long and I know that "clean code" is essential to programming. I'd love to hear feedback on my code formatting, clarity of comments, and overall "cleanness" of the code. I am hoping to get two things out of this review post: It contains two methods, one that factors the quadratic equation ( FactorQuad) and one that finds the factors that multiply to the c value and add to the b value ( MultSum for lack of a better name).įactorQuad is separated into two parts - one if the a value of the quadratic is 1, which is fairly simple to calculate, and a second part if the a value is greater than 1, which requires factoring by grouping. The Factor class will soon contain factorization methods for other equations, but right now it is just the quadratic. In these methods shown here, none of the inheritance is used so I figured it was pointless to show the parent class. The Factor class inherits from the Formulas class which contains various basic calculations that can be used universally across functions and methods (reduce fractions, count significant figures, etc). Return (int(common_factor), int(cval / common_factor)) Print "Error: The quadratic equation is not factorable" # this conditional makes sure not to ignore negative values Group_two_gcd = fractions.gcd(group_two, abs(group_two)) ![]() Group_one_gcd = fractions.gcd(group_one, abs(group_one)) # abs() insures the gcd is not negative due to negative values ![]() # if the a value is greater than 1, we must factor by groupingĬommon_factor = self.MultSum(bval, multval) # if the a value is equal to 1, we can factor the standard way Here is the code relevant to the function: class Factor(Formulas): The methods are capable of factoring when the a term is 1, or greater than 1. Tonight, I added a class and a few methods that factors the quadratic formula. If you want to check out the full program so far, you can see it here. That's not the part I want review on, so in the interest of space I'm going to consolidate as much as possible. The interface is all CLI, and I even made commands that are similar to shell commands. I have a program I am writing that calculates Algebra 2 functions and equations.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |