triosafety.blogg.se

Define isosceles
Define isosceles











  • Docstrings: I'm a stickler for docstrings.
  • Returning the expression that is evaluated saves time, and looks cleaner.
  • Return Expressions: It's better to return expressions than to just else: return False.
  • sorted(sides): was a typo, so I removed the. Return len(set()) = isosceles(a, b, c):Īfter a very quick pass, here's what I have for you: Return is_triangle(a, b, c) and func(a, b, c) You could define a decorator that makes sure the input is a triangle: from functools import wraps Note that all functions need to use is_triangle. Return is_triangle(a, b, c) and len(set()) = 3 Return is_triangle(a, b, c) and len(set()) <= 2 Instead of manually checking all combinations of sides for equality, just use set to get rid of multiples: def isosceles(a, b, c): Return is_triangle(a, b, c) and a = b = c Try to put multiple checks in one line to return right away (but don't push it if it gets too complicated). Your other functions can also be shortened a bit.

    #Define isosceles code#

    The only thing you need to change in your calling code is to call this with is_triangle(*sides), i.e. This uses the fact that after the sorted, a is always the smallest side, as mentioned in the comments. The sides of a triangle are customarily called a, b, c. Instead, just explicitly take three arguments. This opens you up to obscure bugs, such as these ones, which are not covered in your tests: > is_triangle() I find it therefore weird to take a single sides argument, which could be of any size. Self.assertIs(isosceles(), True)ĭef test_equilateral_triangles_are_also_isosceles(self):Ĭlass TestScaleneTriangle(unittest.TestCase):Ī triangle has, by definition, three sides. Self.assertIs(isosceles(), False)Ĭlass TestIsoscelesTriangle(unittest.TestCase): Self.assertIs(equilateral(), False)ĭef test_third_triangle_inequality_violation(self): Self.assertIs(equilateral(), True)ĭef test_all_zero_sides_is_not_a_triangle(self): import unittestįrom triangle import equilateral, isosceles, scaleneĬlass TestEquilateralTriangle(unittest.TestCase):

    define isosceles

    If equilateral(sides) or isosceles(sides):Īlso, I'm adding a simple unit test module.

    define isosceles

    The sides of a triangle come in a list e.g.

    define isosceles

    I've come up with a working solution, but I feel this could be greatly improved and/or simplified. I've been fiddling around with some easy code challenges and there's one about determining if a triangle is equilateral, isosceles, or scalene.











    Define isosceles