fork download
  1. #!/usr/bin/env python3
  2.  
  3. from math import gcd
  4.  
  5. class Rational(object):
  6. def __init__(self, n, d = 1):
  7. if d != 0:
  8. g = gcd(abs(n), abs(d))
  9. self.numer, self.denom = n // g, d // g
  10. else:
  11. raise ZeroDivisionError
  12. def __add__(self, that):
  13. match type(that).__name__:
  14. case 'Rational':
  15. return Rational(
  16. self.numer * that.denom + that.numer * self.denom,\
  17. self.denom * that.denom
  18. )
  19. case 'int':
  20. return Rational(self.numer + that * self.denom, self.denom)
  21. case _:
  22. return NotImplemented
  23. ## if isinstance(that, Rational):
  24. ## return Rational(
  25. ## self.numer * that.denom + that.numer * self.denom,\
  26. ## self.denom * that.denom
  27. ## )
  28. ## elif isinstance(that, int):
  29. ## return Rational(self.numer + that * self.denom, self.denom)
  30. ## else:
  31. ## return NotImplemented
  32. def __sub__(self, that):
  33. match type(that).__name__:
  34. case 'Rational':
  35. return Rational(
  36. self.numer * that.denom - that.numer * self.denom,\
  37. self.denom * that.denom
  38. )
  39. case 'int':
  40. return Rational(self.numer - that * self.denom, self.denom)
  41. case _:
  42. return NotImplemented
  43. ## if isinstance(that, Rational):
  44. ## return Rational(
  45. ## self.numer * that.denom - that.numer * self.denom,\
  46. ## self.denom * that.denom
  47. ## )
  48. ## elif isinstance(that, int):
  49. ## return Rational(self.numer - that * self.denom, self.denom)
  50. ## else:
  51. ## return NotImplemented
  52. def __mul__(self, that):
  53. match type(that).__name__:
  54. case 'Rational':
  55. return Rational(self.numer * that.numer, self.denom * that.denom)
  56. case 'int':
  57. return Rational(self.numer * that, self.denom)
  58. case _:
  59. return NotImplemented
  60. ## if isinstance(that, Rational):
  61. ## return Rational(self.numer * that.numer, self.denom * that.denom)
  62. ## elif isinstance(that, int):
  63. ## return Rational(self.numer * that, self.denom)
  64. ## else:
  65. ## return NotImplemented
  66. def __truediv__(self, that):
  67. match type(that).__name__:
  68. case 'Rational':
  69. return Rational(self.numer * that.denom, self.denom * that.numer)
  70. case 'int':
  71. return Rational(self.numer, self.denom * that)
  72. case _:
  73. return NotImplemented
  74. ## if isinstance(that, Rational):
  75. ## return Rational(self.numer * that.denom, self.denom * that.numer)
  76. ## elif isinstance(that, int):
  77. ## return Rational(self.numer, self.denom * that)
  78. ## else:
  79. ## return NotImplemented
  80. def __repr__(self):
  81. return f'{self.numer}/{self.denom}'
  82.  
  83. if __name__ == '__main__':
  84. oneHalf = Rational(1, 2)
  85. print(oneHalf)
  86. twoThirds = Rational(2, 3)
  87. print(twoThirds)
  88. print(oneHalf / 7)
  89. print(Rational(1, 2))
  90. x = Rational(1, 3)
  91. print(x)
  92. y = Rational(5, 7)
  93. print(y)
  94. print(oneHalf + twoThirds)
  95. y = Rational(3)
  96. print(y)
  97. print(Rational(66, 42))
  98. print(oneHalf + twoThirds)
  99. print(oneHalf + oneHalf * twoThirds)
  100. print((oneHalf + oneHalf) * twoThirds)
  101. print(oneHalf + (oneHalf * twoThirds))
  102. r = Rational(2, 3)
  103. print(r * r)
  104. print(r * 2)
  105.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "/usr/lib/python3.9/py_compile.py", line 144, in compile
    code = loader.source_to_code(source_bytes, dfile or file,
  File "<frozen importlib._bootstrap_external>", line 918, in source_to_code
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "./prog.py", line 13
    match type(that).__name__:
          ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.9/py_compile.py", line 150, in compile
    raise py_exc
py_compile.PyCompileError:   File "./prog.py", line 13
    match type(that).__name__:
          ^
SyntaxError: invalid syntax

stdout
Standard output is empty