Function to determine whether or not a triangle is Isosceles.
the objective is to create a function to determine, from the three sides of a triangle, whether it is isosceles or not. I wrote the following code, yet its not very efficient, how could i have done this better? and is there anything wrong with this code? i tested it and it passed, but its a bit overly complicated for the task.
def isIsosceles(x, y, z):
l = [x, y, z]
for a in l:
for b in l:
if a <= 0:
return False
elif a == b:
if x == y or x == z:
return True
else:
return False
pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
def isIsosceles(x, y, z):
return x == y or y == z
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
def isIsosceles(x, y, z):
return x == y or y == z
hahaha, i just realized that, i learnt some bad habits somewhere. thank you!
pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
def isIsosceles(x, y, z):
return x == y or y == z
whilst this thread is open, could you help me with another problem?
this time i am to determine whether its scalene or not, so i wrote;
def isScalene(x, y, z):
if x <= 0 or y <=0 or z <=0:
return False
elif x == y or x == z:
return False
else:
return True
but its not being accepted. whats wrong with this?
pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
whilst this thread is open, could you help me with another problem?
this time i am to determine whether its scalene or not, so i wrote;
def isScalene(x, y, z):
if x <= 0 or y <=0 or z <=0:
return False
elif x == y or x == z:
return False
else:
return True
but its not being accepted. whats wrong with this?
I understand that a trinagle is scalene if it is not isoceles, so this should work
def isScalene(x, y, z):
return not isIsoceles(x, y, z)
You issue may come from the tab characters that you're using in your code. Configure your editor toindent python code with 4 spaces. Furthermore, pyTony's function doesn't work, one could have x == z when his function returns False. I suggest
def checkValidTriangle(x, y, z):
if x < 0 or y < 0 or z < 0:
raise ValueError("Negative side in triangle.")
elif not (abs(x-y) <= z <= x + y):
raise ValueError("Impossible triangle.")
def isIsoceles(x, y, z):
checkValidTriangle(x, y, z)
return (x == y) or (x == z) or (y == z)
# or
def isIsoceles(x, y, z):
checkValidTriangle(x, y, z)
return len(set((x, y, z))) <= 2
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
def isScalene(x,y,z):
if x == y or x == z or y == z:
return False
elif x <= 0 or y <=0 or z <= 0:
return False
else:
return True
pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
def isScalene(x,y,z):
if x == y or x == z or y == z:
return False
elif x <= 0 or y <=0 or z <= 0:
return False
else:
return True
It works, only it's better to raise an exception if one of x, y, z is < 0, because there is no triangle with x = -1 for example. Actually, the function
def isValidTriangle(x, y, z)
return abs(x-y) <= z <= x + y
suffices, because if this it returns True, then z >= 0 and also x - y <= x + y, so that y >=0 and y - x <= x + y, so that x >=0. You can use strict inequalities if you don't want 'flat' triangles.
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
It works, only it's better to raise an exception if one of x, y, z is < 0, because there is no triangle with x = -1 for example. Actually, the function
def isValidTriangle(x, y, z)
return abs(x-y) <= z <= x + y
suffices, because if this it returns True, then z >= 0 and also x - y <= x + y, so that y >=0 and y - x <= x + y, so that x >=0. You can use strict inequalities if you don't want 'flat' triangles.
noted, thank you for the help.
pwolf
Junior Poster in Training
71 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
if one of x, y, z is < 0
This is also expressible more obviously as:
def is_valid_triangle(*sides):
return len(sides) == 3 and min(sides) > 0
Also your function return False for 39, 4, 874 and 14, 64, 82 and as you are missing also : at end of def line, I assume you had not chance to test the function.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
39, 4, 874 and 14, 64, 82 are not valid triangles ! Try to draw them ! There is another way to write the function
def isValidTriangle(x, y, z):
return min(x+y-z, y+z-x, z+x-y) >= 0
Ever heard of the triangle inequality ?
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
Ok, so you more far than OP, I do not think the mathematical validity was required for OP.
Triangle inequality with positiveness I understand more easily in form:
def is_valid_triangle(x,y,z):
return x + y >= z > 0 and y + z >= x > 0 and x + z > y
I assume that you mean by flat triangle with 'triangle' with one or more sides 0. This function does not allow them.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852