From 7111a944c7841be0113ef6268f3f0a92b24f07b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Guzm=C3=A1n?= Date: Tue, 12 Jan 2021 14:30:19 -0500 Subject: [PATCH] Reto conseguido --- .challenge.py.swp | Bin 0 -> 12288 bytes challenge.py | 211 ++++++++++++++++++++++++++++++++++++++-------- challenge.py~ | 88 +++++++++++++++++++ 3 files changed, 264 insertions(+), 35 deletions(-) create mode 100644 .challenge.py.swp create mode 100644 challenge.py~ diff --git a/.challenge.py.swp b/.challenge.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..ef9eb428859692b8f157bb90eb20cf7f8d7b7c3d GIT binary patch literal 12288 zcmeI2&2Jk;7>B2{d<6O>5C<-3Y#-u8jqL2dn3MYqVR(g`%+1=TB zpLyR|DcM$SZMi{@R%Qsc!-O=R{?IYI7tyd1wV7ICf$*eh+H!lcerjcHW$LNLEw4yV zEY4S(5m!<5x{j+$vK{+8&{0*I$5lg*>2)#Xvq*DQZTFbx@u172&E2W6uX{3>s?Bb; zRl@!)9&S%t1H0b9!RBK#)3s8uKp%Rrx$A9M0oH&uU=3IU)_^r&4Oj!#z-}|3!#(65 z1br9ifjt@cWjAZU8n6be0c*e-um-FFYrqp;T{Bp|Noo6 z0sMZ5kgMPd_#S)=-UfZp0t__3VQ>gsJ4nc%;1BQ(_z1iX&Vc8^aWDrS2HzbZ|2fH%P# zfP-1^2)K4XA-{oN!7pF~ya!$d9dHsH2MgdBD1ZZCKe#+b$dBN2@ESM|UINd8X;1@W z;OF}Y`3YPCZ-EOS1W$lvPy~P9OUPf~3vdy90M3Io&;)Z}AGnJ7xdM`Txwb3r-cUT0!{_z@-AU@w~fM1%_?O*=wiG?mKWxoH=XcG^PWW4I1i zJZN)=DbD_HkBWC=k0~dVUcW1Yk@Tq*3;DVf?nru5J?XdNXe51UC0Nk)My0N;N7q|) zCdPbj9pz0DaT$p#m?ze6FBv1Qo8eI2v^``wJ;|ldwdy|;3(d=esyL05Tx1c?(^mMQ zRC-V~M0BX*1kC4-Ll+k4xZ{{Qa-4Cbd6L0{k(VgG?xa4`y-FyOYT0-tt)1k$5?!q~ z>Wvfi)p=?jih?qQP$(3#)6dD6_SjiOD`{K0oc2%%$+=>&xXN{`0#p#n(i8@j9m?oX z!Qd)lN@=JuHLjQ|;>d)U^mpBmM?a>lr7xOX!q98SJl8CN*%EJ7YVt1wFCNSQ literal 0 HcmV?d00001 diff --git a/challenge.py b/challenge.py index ffdfcce..b98569c 100644 --- a/challenge.py +++ b/challenge.py @@ -1,88 +1,229 @@ import math - def square_area(side): """Returns the area of a square""" - # You have to code here - # REMEMBER: Tests first!!! - pass + side = float(side) + + if (side < 0.0): + raise ValueError('Negative numbers are not allowed') + + return side**2.0 def rectangle_area(base, height): """Returns the area of a rectangle""" - # You have to code here - # REMEMBER: Tests first!!! - pass + base = float(base) + height = float(height) + if (base < 0.0 or height < 0.0): + raise ValueError('Negative numbers are not allowed') + + return base * height def triangle_area(base, height): """Returns the area of a triangle""" - # You have to code here - # REMEMBER: Tests first!!! - pass + base = float(base) + height = float(height) + if (base < 0.0 or height < 0.0): + raise ValueError('Negative numbers are not allowed') + + return base * height / 2.0 def rhombus_area(diagonal_1, diagonal_2): """Returns the area of a rhombus""" - # You have to code here - # REMEMBER: Tests first!!! - pass + diagonal_1 = float(diagonal_1) + diagonal_2 = float(diagonal_2) + if (diagonal_1 < 0.0 or diagonal_2 < 0.0): + raise ValueError('Negative numbers are not allowed') + + return diagonal_1 * diagonal_2 / 2.0 def trapezoid_area(base_minor, base_major, height): """Returns the area of a trapezoid""" - # You have to code here - # REMEMBER: Tests first!!! - pass + base_minor = float(base_minor) + base_major = float(base_major) + height = float(height) + if (base_minor < 0.0 or base_major < 0.0 or height < 0.0): + raise ValueError('Negative numbers are not allowed') + + return (base_minor + base_major) * height / 2.0 def regular_polygon_area(perimeter, apothem): """Returns the area of a regular polygon""" - # You have to code here - # REMEMBER: Tests first!!! - pass + perimeter = float(perimeter) + apothem = float(apothem) + if (perimeter < 0.0 or apothem < 0.0): + raise ValueError('Negative numbers are not allowed') + + return perimeter * apothem / 2 def circumference_area(radius): """Returns the area of a circumference""" - # You have to code here - # REMEMBER: Tests first!!! - # Use math.pi for π value - pass + radius = float(radius) + + if (radius < 0.0): + raise ValueError('Negative numbers are not allowed') + + return radius**2 * math.pi if __name__ == '__main__': import unittest + class TestValues: + def __init__(self, expected, args=[], kwargs={}): + self.expected = expected + self.args = args + self.kwargs = kwargs + + class TestRaisesError: + def __init__(self, raises_error, args=[], kwargs={}): + self.args = args + self.kwargs = kwargs + self.raises_error = raises_error + class GeometrySuite(unittest.TestCase): def setUp(self): # Initialize the needed values for the tests - pass + self.square_test_values = [ + TestValues(1, [1]), + TestValues(4, [2]), + TestValues(9, [3]), + TestValues(6.25, [2.5]), + ] + + self.rectangle_test_values = [ + TestValues(1, [1, 1]), + TestValues(6, [2, 3]), + TestValues(20, [5, 4]), + TestValues(14.25, [2.5, 5.7]), + ] + + self.triangle_test_values = [ + TestValues(2, [2, 2]), + TestValues(7.5, [5, 3]), + TestValues(0.1, [0.5, 0.4]), + ] + + self.rhombus_test_values = [ + TestValues(3, [3, 2]), + TestValues(7.5, [5, 3]), + TestValues(0.1, [0.5, 0.4]), + ] + + self.trapezoid_test_values = [ + TestValues(10, [2, 2, 5]), + TestValues(16.5, [2.4, 2.6, 6.6]), + ] + + self.regular_polygon_test_values = [ + TestValues(7.5, [5, 3]), + TestValues(0.1, [0.5, 0.4]), + ] + + self.circumference_test_values = [ + TestValues(math.pi, [1]), + TestValues(math.pi * 30.25, [5.5]), + ] + + self.one_param_test_raises = [ + TestRaisesError(ValueError, [-1]), + TestRaisesError(ValueError, ['a']), + ] + + self.two_params_test_raises = [ + TestRaisesError(ValueError, [-1, 1]), + TestRaisesError(ValueError, [1, -1]), + TestRaisesError(ValueError, [-1, -1]), + TestRaisesError(ValueError, ['a', 1]), + TestRaisesError(ValueError, [1, 'a']), + ] + + self.three_params_test_raises = [ + TestRaisesError(ValueError, [-1, 1, 1]), + TestRaisesError(ValueError, [1, -1, 1]), + TestRaisesError(ValueError, [1, 1, -1]), + TestRaisesError(ValueError, [-1, -1, -1]), + TestRaisesError(ValueError, ['a', 1, 1]), + TestRaisesError(ValueError, [1, 'a', 1]), + TestRaisesError(ValueError, [1, 1, 'a']), + ] + + @unittest.expectedFailure + def test_values_rise(self, fn, values=[], raises=[]): + for test_values in values: + self.assertEqual(test_values.expected, fn( + *test_values.args, **test_values.kwargs)) + + for test_raises in raises: + with self.assertRaises(test_raises.raises_error): + fn(*test_raises.args, **test_raises.kwargs) def test_square_area(self): - # Make this test first... + self.test_values_rise( + square_area, + self.square_test_values, + self.one_param_test_raises + ) def test_rectangle_area(self): - # Make this test first... + self.test_values_rise( + rectangle_area, + self.rectangle_test_values, + self.two_params_test_raises + ) def test_triangle_area(self): - # Make this test first... + self.test_values_rise( + triangle_area, + self.triangle_test_values, + self.two_params_test_raises + ) def test_rhombus_area(self): - # Make this test first... + self.test_values_rise( + rhombus_area, + self.rhombus_test_values, + self.two_params_test_raises + ) def test_trapezoid_area(self): - # Make this test first... + self.test_values_rise( + trapezoid_area, + self.trapezoid_test_values, + self.three_params_test_raises + ) def test_regular_polygon_area(self): - # Make this test first... + self.test_values_rise( + regular_polygon_area, + self.regular_polygon_test_values, + self.two_params_test_raises + ) def test_circumference_area(self): - # Make this test first... + self.test_values_rise( + circumference_area, + self.circumference_test_values, + self.one_param_test_raises + ) def tearDown(self): - # Delete the needed values for the tests - pass - - unittest.main() + del(self.square_test_values) + del(self.rectangle_test_values) + del(self.triangle_test_values) + del(self.rhombus_test_values) + del(self.trapezoid_test_values) + del(self.regular_polygon_test_values) + del(self.circumference_test_values) + + del(self.one_param_test_raises) + del(self.two_params_test_raises) + del(self.three_params_test_raises) + + unittest.main() \ No newline at end of file diff --git a/challenge.py~ b/challenge.py~ new file mode 100644 index 0000000..ffdfcce --- /dev/null +++ b/challenge.py~ @@ -0,0 +1,88 @@ +import math + + +def square_area(side): + """Returns the area of a square""" + # You have to code here + # REMEMBER: Tests first!!! + pass + + +def rectangle_area(base, height): + """Returns the area of a rectangle""" + # You have to code here + # REMEMBER: Tests first!!! + pass + + +def triangle_area(base, height): + """Returns the area of a triangle""" + # You have to code here + # REMEMBER: Tests first!!! + pass + + +def rhombus_area(diagonal_1, diagonal_2): + """Returns the area of a rhombus""" + # You have to code here + # REMEMBER: Tests first!!! + pass + + +def trapezoid_area(base_minor, base_major, height): + """Returns the area of a trapezoid""" + # You have to code here + # REMEMBER: Tests first!!! + pass + + +def regular_polygon_area(perimeter, apothem): + """Returns the area of a regular polygon""" + # You have to code here + # REMEMBER: Tests first!!! + pass + + +def circumference_area(radius): + """Returns the area of a circumference""" + # You have to code here + # REMEMBER: Tests first!!! + # Use math.pi for π value + pass + + +if __name__ == '__main__': + import unittest + + class GeometrySuite(unittest.TestCase): + + def setUp(self): + # Initialize the needed values for the tests + pass + + def test_square_area(self): + # Make this test first... + + def test_rectangle_area(self): + # Make this test first... + + def test_triangle_area(self): + # Make this test first... + + def test_rhombus_area(self): + # Make this test first... + + def test_trapezoid_area(self): + # Make this test first... + + def test_regular_polygon_area(self): + # Make this test first... + + def test_circumference_area(self): + # Make this test first... + + def tearDown(self): + # Delete the needed values for the tests + pass + + unittest.main()