• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Apr 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class Fraction:
2.     #Constructor. Puts fraction in simplest form
3.     def __init__(self,a,b):
4.         self.num = a
5.         self.den = b
6.         self.simplify()
7.     #Print Fraction as a String
8.     def __str__(self):
9.         if self.den==1:
10.             return str(self.num)
11.         else:
12.             return str(self.num)+"/"+str(self.den)
13.     #Get the Numerator
14.     def getNum(self):
15.         return self.num
16.     #Get the Denominator
17.     def getDen(self):
18.         return self.den
19.     #Give Numerical Approximation of Fraction
20.     def approximate(self):
21.         return self.num/self.den
22.     #Simplify fraction
23.     def simplify(self):
24.         x = self.gcd(self.num,self.den)
25.         self.num = self.num // x
26.         self.den = self.den // x
27.     #Find the GCD of a and b
28.     def gcd(self,a,b):
29.         if b==0:
30.             return a
31.         else:
32.             return self.gcd(b,a % b)
33.     #Complete these methods in lab
34.     def __add__(self,other):
35.         return (self.num * other.den + self.den * other.num)/(self.den * other.den)
36.     def __sub__(self,other):
37.         return (self.num * other.den + self.den * other.num * -1)/(self.den * other.den)
38.     def __mul__(self,other):
39.         return (self.num * other.num)/(self.den * other.den)
40.     def __truediv__(self,other):
41.         return (self.num * other.den)/(self.den * other.num)
42.     def __pow__(self,exp):
43.         if exp > 0:
44.             return self.num ** exp / self.den ** exp
45.         else:
46.             return 1
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top