Triangle Exercise

On submitting this exercise, 17 tests passed and 4 tests failed (3 isosceles and 1 scalene).
Some of these 4 failed tests have similar test values to some of those that have passed.
I have run my code in an online IDE and all these “failed” cases provide the desired result. I fail to understand why these 4 cases failed.

Below is my code.

def equilateral(sides):
    return (sides[0] > 0 and sides[1] > 0 and sides[2] > 0) and (sides[0] == sides[1] and sides[1] == sides[2])


def isosceles(sides):
    return (sides[0] > 0 and sides[1] > 0 and sides[2] > 0) and (sides[0] == sides[1] or sides[0] == sides[2] or sides[1] == sides[2])


def scalene(sides):
    return (sides[0] > 0 and sides[1] > 0 and sides[2] > 0) and not(isosceles(sides))

The failed test cases are:

  1. self.assertIs(isosceles([1, 1, 3]), False) //BUT self.assertIs(isosceles([4, 4, 3]), True)

  2. self.assertIs(isosceles([1, 3, 1]), False) //BUT self.assertIs(isosceles([4, 3, 4]), True)

  3. self.assertIs(isosceles([3, 1, 1]), False) //BUT self.assertIs(isosceles([3, 4, 4]), True)

  4. self.assertIs(scalene([7, 3, 2]), False) //BUT self.assertIs(scalene([5, 4, 6]), True)

Below is the code I used in the online IDE which debunks these failed cases.

sides = [1, 1, 3]
# Online Python - IDE, Editor, Compiler, Interpreter
sides = [1, 1, 3]

def equilateral(sides):
    equi = (sides[0] > 0 and sides[1] > 0 and sides[2] > 0) and (sides[0] == sides[1] and sides[1] == sides[2])
    print ("Is Equilateral: " + str(equi))
    return equi


def isosceles(sides):
    isos = (sides[0] > 0 and sides[1] > 0 and sides[2] > 0) and (sides[0] == sides[1] or sides[0] == sides[2] or sides[1] == sides[2])
    print ("Is Isosceles: " + str(isos))
    return isos


def scalene(sides):
    scal = (sides[0] > 0 and sides[1] > 0 and sides[2] > 0) and not(isosceles(sides))
    print ("Is Scalene: " + str(scal))
    return scal
    
#print (sides[0])    
isosceles(sides)
equilateral(sides)
scalene(sides)

This is the link to the above code at online IDE (valid for 6 months from 23rd June 2025) - Triangle Exercise at Online Python

For a shape to be a triangle at all, all sides have to be of length > 0, and the sum of the lengths of any two sides must be greater than or equal to the length of the third side.

For example, scalene([7, 3, 2]) should be false because that is not a triangle. 3+2 is not >= 7.

4 Likes

Thanks for pointing this out.
The failed cases were failure for triangle validity. I had considered only the non-zero length criteria.