Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Python conditionals

Options
  • 31-01-2012 2:41pm
    #1
    Closed Accounts Posts: 3,915 ✭✭✭


    Doing some practice on singpath and a couple of problems are doing my head in. I'm sure its a simple fix but I'd like a bit of input.

    First one is basic enough:
    Triangle Test
    Create a function that takes three numbers as input and returns True or False depending on whether those three numbers can form a triangle. Three numbers can form a triangle if the sum of any two sides is greater than the third side.
    Example test: 
    >>> is_triangle(3,4,5) 
    True 
    >>> is_triangle(1,2,124) 
    False 
    >>> is_triangle(3,3,3) 
    True 
    >>> is_triangle(3,6,3) 
    False
    

    No matter what I do I cant get past this. I've gone through every option on this even wrote it out in separate if statements and still cant get any good of it. Code compiles by the way just gives bad results.
    def is_triangle(a,b,c):
        if a + b > c:
            return True
        elif b + c > a:
            return True
        elif a + c > b:
            return True
        else:
            return False
    

    Am I missing something really obvious here ? :o

    Just to add that I have managed to satisfy all tests on occasion but was told my code needed to be generalised.

    Link to game : http://singpath.appspot.com/eli/play.html?gameID=10808750


Comments

  • Registered Users Posts: 139 ✭✭Bald? er, dash!


    You've got to write your conditional statement to satisfy the triangle inequality theorem - a triangle can be represented by three vertices if a+b>c AND a+c>b AND b+c>a.

    You're not getting the code - I'm not doing your homework for you... ;)


  • Closed Accounts Posts: 3,915 ✭✭✭MungBean


    You've got to write your conditional statement to satisfy the triangle inequality theorem - a triangle can be represented by three vertices if a+b>c AND a+c>b AND b+c>a.

    You're not getting the code - I'm not doing your homework for you... ;)

    Very much appreciate the help. :)


  • Banned (with Prison Access) Posts: 3,455 ✭✭✭krd


    You've got to write your conditional statement to satisfy the triangle inequality theorem - a triangle can be represented by three vertices if a+b>c AND a+c>b AND b+c>a.

    Are you sure you didn't mean OR ?


  • Registered Users Posts: 139 ✭✭Bald? er, dash!


    Yes, I'm sure. OR will always return true if the three conditions are as listed, because at least one of them will be true. You need all of them true to satisfy the triangle nequality theorem


  • Closed Accounts Posts: 3,915 ✭✭✭MungBean


    krd wrote: »
    Are you sure you didn't mean OR ?

    The phrasing of the question makes it sounds like it should be satisfied with any one side being smaller than the sum of any other which is what had me confused. But Bald? er, dash! is right, all three must be true to satisfy the theorem.


  • Advertisement
  • Registered Users Posts: 139 ✭✭Bald? er, dash!


    Think about it logically:

    The shortest distance from point a to point b is a straight line, so distance from points a to c to b must be longer (providing c is not along the line from a to b, which is why we don't use >= operator) and the same goes for all of the other combinations,so they all must satisfy this requirement for the triangle to be possible


  • Banned (with Prison Access) Posts: 3,455 ✭✭✭krd


    MungBean wrote: »
    The phrasing of the question makes it sounds like it should be satisfied with any one side being smaller than the sum of any other which is what had me confused. But Bald? er, dash! is right, all three must be true to satisfy the theorem.

    Yep. It's the phrasing that had me just thinking about the logic. Three numbers can form a triangle if the sum of any two sides is greater than the third side. And the phrasing of the question is actually wrong. It would imply that a+b>c OR a+c>b OR b+c>a, will give you a triangle - I've thought about it, and it won't. If A = 8, B = 1, C = 1 you would not be able to form a triangle but the logic would say you would.

    a+b>c AND a+c>b AND b+c>a. Will give you a triangle. The sum of any two sides must exceed the third side for a triangle to be possible.


Advertisement