If you are given three sticks, you may or may not be able to arrange them in a triangle. For example, if one of the sticks is 12 inches long and the other two are 1 inch long, it is clear that you will not be able to get the shorter sticks to meet. For any three lengths, there is a simple test to see if it is possible to form a triangle:
“If any of the three lengths is greater than the sum of the other two, then you cannot form a
triangle. Otherwise, you can.”
a) Write a function in Python named is_triangle that takes three integers as arguments, and prints either True or False depending on whether you can or cannot form a triangle from sticks with the given lengths.
b) Write a function in Python called semi_prmtr that takes three integers a, b and c as arguments and returns the result of the following calculation:
(a+b+c)/ 2
c) Modify the is_triangle (call it is_triangleV2) function so that it now returns a boolean value. Using is_triangleV2, and semi_prmtr, write a function called tri_Area that takes three integers as arguments and returns the area of a valid triangle.
Use the following formula:
area= vs(s-a)(s-b)(s-c)
where:
a,b and c are the lengths of the sides, and s= (a+b+c)/ 2
Here is the Solution for the problem above:
import math
a).
def is_triangle(x,y,z):
if(z>(x+y)):
print ("False")
elif(y>(x+z)):
print ("False")
elif(x>(z+y)):
print ("False")
else:
print ("True")
b).
def semi_prmtr(a,b,c):
return (a+b+c)/2
c).
def is_triangleV2(x,y,z):
if(z>(x+y)):
return False
elif(y>(x+z)):
return False
elif(x>(z+y)):
return False
else:
return True
def tri_Area(a,b,c):
if(is_triangleV2(a,b,c)):
s = semi_prmtr(a,b,c)
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
return area
else:
print("Invalid Triangle")