Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. import random
  2. from Binary_Search_Tree import Binary_Search_Tree
  3.  
  4. class Fraction:
  5.  
  6. def __init__(self, numerator, denominator):
  7. # use caution here... In most languages, it is not a good idea to
  8. # raise an exception from a constructor. Python is a bit different
  9. # and it shouldn't cause a problem here.
  10. if denominator == 0:
  11. raise ZeroDivisionError
  12. self.__n = numerator
  13. self.__d = denominator
  14. self.__reduce()
  15.  
  16. @staticmethod
  17. def gcd(n, d):
  18. while d != 0:
  19. t = d
  20. d = n % d
  21. n = t
  22. return n
  23.  
  24. def __reduce(self):
  25. if self.__n < 0 and self.__d < 0:
  26. self.__n = self.__n * -1
  27. self.__d = self.__d * -1
  28. divisor = Fraction.gcd(self.__n, self.__d)
  29. self.__n = self.__n // divisor
  30. self.__d = self.__d // divisor
  31.  
  32. def __add__(self, addend):
  33. num = self.__n * addend.__d + self.__d * addend.__n
  34. den = self.__d * addend.__d
  35. return Fraction(num, den)
  36.  
  37. def __sub__(self, subtrahend):
  38. num = self.__n * subtrahend.__d - self.__d * subtrahend.__n
  39. den = self.__d * subtrahend.__d
  40. return Fraction(num, den)
  41.  
  42. def __mul__(self, multiplicand):
  43. num = self.__n * multiplicand.__n
  44. den = self.__d * multiplicand.__d
  45. return Fraction(num, den)
  46.  
  47. def __truediv__(self, divisor):
  48. if divisor.__n == 0:
  49. raise ZeroDivisionError
  50. num = self.__n * divisor.__d
  51. den = self.__d * divisor.__n
  52. return Fraction(num, den)
  53.  
  54. def __lt__(self, other):
  55. return (self.__n*other.__get_denom()<other.__get_num()*self.__d)
  56.  
  57. def __gt__(self, other):
  58. return (self.__n*other.__get_denom()>other.__get_num()*self.__d)
  59.  
  60.  
  61. def __eq__(self, other):
  62. return (self.__n*other.__get_denom()==other.__get_num()*self.__d)
  63.  
  64.  
  65. def to_float(self):
  66. #this is safe because we don't allow a
  67. #zero denominator
  68. return self.__n / self.__d
  69.  
  70. def __str__(self):
  71. return str(self.__n) + '/' + str(self.__d)
  72.  
  73. # the __repr__ method is similar to __str__, but is called
  74. # when Python wants to display these objects in a container like
  75. # a Python list.
  76. def __repr__(self):
  77. return str(self)
  78.  
  79. def __get_num(self):
  80. return self.__n
  81.  
  82. def __get_denom(self):
  83. return self.__d
  84.  
  85. if __name__ == '__main__':
  86. list=[]
  87. avl_tree=Binary_Search_Tree()
  88. for i in range(100):
  89. random_int_1=random.randint(1,10000)
  90. random_int_2=random.randint(1,10000)
  91. list+=[Fraction(random_int_1,random_int_2)]
  92. print("original list:")
  93. print(list)
  94. for i in list:
  95. avl_tree.insert_element(i)
  96. print("sorted list:")
  97. print(avl_tree.to_list())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement