Check whether right angled triangle is valid or not for large sides in Python

PythonServer Side ProgrammingProgramming

Suppose we have three sides in a list. We have to check whether these three sides are forming a right angled triangle or not.

So, if the input is like sides = [8, 10, 6], then the output will be True as (8^2 + 6^2) = 10^2.

To solve this, we will follow these steps −

  • sort the list sides
  • if (sides[0]^2 + sides[1]^2) is same as sides[2]^2, then
    • return True
  • return False

Let us see the following implementation to get better understanding −

Example Code

Live Demo

def solve(sides):
   sides.sort()
   if (sides[0]*sides[0]) + (sides[1]*sides[1]) == (sides[2]*sides[2]):
      return True
   return False
   
sides = [8, 10, 6]
print(solve(sides))

Input

[8, 10, 6]

Output

True
raja
Published on 16-Jan-2021 09:15:16
Advertisements