Use dataclasses and math module to get real Pi values.
Also fixed test cases.
#!/usr/bin/env python3 """ Debugging: FixDatTrash # 2 """ import math from dataclasses import dataclass PI = math.pi @dataclass class Calculator: """A calculator class.""" num: int def calculate_power(self, exp): """ Calculates power of self.num. parameter `exp` is the exponent. """ return self.num ** exp def calculate_square_root(self): """Calculates square root of self.num.""" return self.num ** 0.5 def calculate_cube_root(self): """Calculates cube root of self.num.""" return self.num ** (1 / 3) def calculate_circumference(self): """ Calculates circumference of circle. self.num is the diameter of the circle. """ return PI * self.num def calculate_circle_area(self): """ Calculates area of circle. self.num is the radius of the circle """ return self.num ** 2 * PI
- #!/usr/bin/env python3
- """
- Debugging: FixDatTrash # 2
- """
- import math
- from dataclasses import dataclass
# constant:PI = 3.1415926535897- PI = math.pi
- @dataclass
- class Calculator:
- """A calculator class."""
def __init__(self, num):"""Initialize attributes."""self.num = num- num: int
- def calculate_power(self, exp):
- """
- Calculates power of self.num.
- parameter `exp` is the exponent.
- """
- return self.num ** exp
- def calculate_square_root(self):
- """Calculates square root of self.num."""
- return self.num ** 0.5
- def calculate_cube_root(self):
- """Calculates cube root of self.num."""
- return self.num ** (1 / 3)
- def calculate_circumference(self):
- """
- Calculates circumference of circle.
- self.num is the diameter of the circle.
- """
- return PI * self.num
- def calculate_circle_area(self):
- """
- Calculates area of circle.
- self.num is the radius of the circle
- """
- return self.num ** 2 * PI
from solution import Calculator, PI import unittest import math class TestCalculator(unittest.TestCase): def test_calculate_power(self): self.assertEqual(Calculator(2).calculate_power(2), 4) self.assertEqual(Calculator(4).calculate_power(4), 256) def test_calculate_square_root(self): self.assertEqual(Calculator(3).calculate_square_root(), 1.7320508075688772) self.assertEqual(Calculator(16).calculate_square_root(), 4) def test_calculate_cube_root(self): self.assertEqual(Calculator(27).calculate_cube_root(), 3) self.assertEqual(Calculator(125).calculate_cube_root(), 4.999999999999999) def test_calculate_circumference(self): self.assertEqual(Calculator(3).calculate_circumference(), 9.42477796076938) self.assertEqual(Calculator(12).calculate_circumference(), 37.69911184307752) def test_calculate_circle_area(self): self.assertEqual(math.floor(Calculator(3).calculate_circle_area()), 28) self.assertEqual(math.floor(Calculator(54).calculate_circle_area()), 9160) if __name__ == '__main__': unittest.main()
- from solution import Calculator, PI
- import unittest
- import math
- class TestCalculator(unittest.TestCase):
- def test_calculate_power(self):
- self.assertEqual(Calculator(2).calculate_power(2), 4)
- self.assertEqual(Calculator(4).calculate_power(4), 256)
- def test_calculate_square_root(self):
- self.assertEqual(Calculator(3).calculate_square_root(), 1.7320508075688772)
- self.assertEqual(Calculator(16).calculate_square_root(), 4)
- def test_calculate_cube_root(self):
- self.assertEqual(Calculator(27).calculate_cube_root(), 3)
- self.assertEqual(Calculator(125).calculate_cube_root(), 4.999999999999999)
- def test_calculate_circumference(self):
self.assertEqual(Calculator(3).calculate_circumference(), 9.424777960769099)self.assertEqual(Calculator(12).calculate_circumference(), 37.699111843076395)- self.assertEqual(Calculator(3).calculate_circumference(), 9.42477796076938)
- self.assertEqual(Calculator(12).calculate_circumference(), 37.69911184307752)
- def test_calculate_circle_area(self):
- self.assertEqual(math.floor(Calculator(3).calculate_circle_area()), 28)
- self.assertEqual(math.floor(Calculator(54).calculate_circle_area()), 9160)
- if __name__ == '__main__':
- unittest.main()