The program handin21 will only submit files in the cs21/homework/9 directory.
Your programs are graded on both correctness and style. Please review the comments regarding programming style on the main page
Some of the problems will have optional components that allow you to
further practice your skills in Python. Optional portions will not be
graded, but may be interesting for those wanting some extra challenges.
Below is a sample test program that shows how to create a new instance of the Fraction class and work with fractions.
def test(): f1=Fraction(1, 2) f2=Fraction(1, 3) f3=Fraction(2 ,4) print "f1 =", f1 print "f2 =", f2 print "f1 plus f2 =",f1.add(f2) print "f1 times f2 =",f1.mul(f2) print "f1 divided by f2=", f1.div(f2) print "f1 minus f2=",f1.sub(f2) print "inverse of f1=",f1.inverse() print "negation of f1=",f1.neg() print "negation of negation of f1=",f1.neg().neg() print "decimal value of f2=",f2.float() if f1 > f2: print "%s is greater than %s" % (f1, f2) if f3==f1: print "%s equals %s" % (f3, f1) try: f3=Fraction(4, 0) except ZeroDivisionError: print "Creating a fraction with a 0 denominator is illegal"
The output of running this test code is
f1 = 1/2 f2 = 1/3 f1 plus f2 = 5/6 f1 times f2 = 1/6 f1 divided by f2= 3/2 f1 minus f2= 1/6 inverse of f1= 2 negation of f1= -1/2 negation of negation of f1= 1/2 decimal value of f2= 0.333333333333 1/2 is greater than 1/3 1/2 equals 1/2 Creating a fraction with a 0 denominator is illegal
Your Fraction class must implement the following features:
cmp(f1, f2) f1 < f2 f1 >= f2are valid comparisions in a correct implementation.
Save your class implementation in the file fraction.py
gcd(a, b) = a : if b=0 gcd(b, a mod b) : otherwise
If the denominator of any fraction is 1, you do not need to print the denominator when you display the fraction as a string. Thus 3/1 should be printed as just 3.
f1 + f2 f1 *= f3 f4 = -f5 float(f3)work as expected.
f1 = 3/4 f1+Fraction(2, 1) #returns 11/4 f1+2 #also returns 11/4 f1*8 #returns 6 as a FractionYou can use the type function to check the type of a particular function parameter. For example:
def myFunction(val): if type(val)==int: print "val is an integer, do integer stuff" elif type(val)==str: print "val is a string, do string stuff" else: print "assume val is a fraction"What happens if you try to add an integer and a fraction with the fraction on the right side of the addition, e.g., 3+f1? Try implementing the __radd__ method inside your Fraction class and have it simply call your __add__ method. Implement similar methods for the other math operations.
In writing your class for fractions, you probably used standard integer operations such as +, -, *, and / to compute your answers. Python is way too nice in giving you all those built-in functions. All you really need are the following:
add(a,b) #returns the sum of integers a and b multiply(a,b) #returns the product power(a,b) #returns a raised to the power of b sub(a,b) #returns a-b if b <= a and 0 otherwise greaterThan(a,b) #returns True if b is greater than aRemember, the first function you write can only call increment, decrement, or isZero and use recursion. You cannot use loops. You cannot check if a number is greater than 3. You cannot use +, -, * or other math operations. The only if statement you can write is if isZero(n) for some parameter n. The second function you write can use the first function you write in addition to the basic functions. Included below are the implementations of increment, decrement, and isZero. They are the only functions that are allowed to use +,-, and generic if statements
def increment(n): return n+1 def decrement(n): #don't allow negative values of n if n > 0: return n-1 return 0 def isZero(n): return n==0
How many function calls do you make to compute something simple like multiply(2,2)? Aren't you glad python let's you write 2*2 instead?