Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from Binary_Search_Tree import Binary_Search_Tree
- class Fraction:
- def __init__(self, numerator, denominator):
- # use caution here... In most languages, it is not a good idea to
- # raise an exception from a constructor. Python is a bit different
- # and it shouldn't cause a problem here.
- if denominator == 0:
- raise ZeroDivisionError
- self.__n = numerator
- self.__d = denominator
- self.__reduce()
- @staticmethod
- def gcd(n, d):
- while d != 0:
- t = d
- d = n % d
- n = t
- return n
- def __reduce(self):
- if self.__n < 0 and self.__d < 0:
- self.__n = self.__n * -1
- self.__d = self.__d * -1
- divisor = Fraction.gcd(self.__n, self.__d)
- self.__n = self.__n // divisor
- self.__d = self.__d // divisor
- def __add__(self, addend):
- num = self.__n * addend.__d + self.__d * addend.__n
- den = self.__d * addend.__d
- return Fraction(num, den)
- def __sub__(self, subtrahend):
- num = self.__n * subtrahend.__d - self.__d * subtrahend.__n
- den = self.__d * subtrahend.__d
- return Fraction(num, den)
- def __mul__(self, multiplicand):
- num = self.__n * multiplicand.__n
- den = self.__d * multiplicand.__d
- return Fraction(num, den)
- def __truediv__(self, divisor):
- if divisor.__n == 0:
- raise ZeroDivisionError
- num = self.__n * divisor.__d
- den = self.__d * divisor.__n
- return Fraction(num, den)
- def __lt__(self, other):
- return (self.__n*other.__get_denom()<other.__get_num()*self.__d)
- def __gt__(self, other):
- return (self.__n*other.__get_denom()>other.__get_num()*self.__d)
- def __eq__(self, other):
- return (self.__n*other.__get_denom()==other.__get_num()*self.__d)
- def to_float(self):
- #this is safe because we don't allow a
- #zero denominator
- return self.__n / self.__d
- def __str__(self):
- return str(self.__n) + '/' + str(self.__d)
- # the __repr__ method is similar to __str__, but is called
- # when Python wants to display these objects in a container like
- # a Python list.
- def __repr__(self):
- return str(self)
- def __get_num(self):
- return self.__n
- def __get_denom(self):
- return self.__d
- if __name__ == '__main__':
- list=[]
- avl_tree=Binary_Search_Tree()
- for i in range(100):
- random_int_1=random.randint(1,10000)
- random_int_2=random.randint(1,10000)
- list+=[Fraction(random_int_1,random_int_2)]
- print("original list:")
- print(list)
- for i in list:
- avl_tree.insert_element(i)
- print("sorted list:")
- print(avl_tree.to_list())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement