Python exercise
Question:
I came across an exercise in "Think Python", and I couldn’t figure out how to do it.
The exercise:
- Write a function named is_triangle that takes three integers as arguments, and that prints either “Yes” or “No,” depending on whether you can or cannot form a triangle from sticks with the given lengths.
And of course:
If any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can3.
Answers:
There are plenty of ways to do this. Here is one:
def is_triangle(a, b, c):
if (a > b + c) or (b > a + c) or (c > a + b):
print "No"
else:
print "Yes"
The triangle inequality theorem states that any side of a triangle is always shorter than the sum of the other two sides.
In the above code, it checks whether any side is greater than or equal to sum of other sides & print “Not Possible”.
Code:
>>> def is_triangle(sides):
... for i,l in enumerate(sides):
... if sides[i] > (sides[(i+1)%3] + sides[(i+2)%3]): return False
... return True
...
>>> is_triangle([3,4,5])
True
>>>
Use:
def is_triangle(a, b, c):
return a + b + c >= 2 * max(a, b, c) # Supposed circumference suffices for largest edge and back.
The sum of two sides of a triangle must be greater than the third side to make a valid triangle
Here goes the code:
def is_triangle(a,b,c): # Take three arguments
if(a+b>c)and(b+c>a)and(a+c>b): # Check the conditions
print "Yes"
else:
print "No"
One way to look at it is to notice that if any side is larger than half the perimiter (ie. Sum of stick lengths) then it can’t form a triangle. So test each stick being smaller than perim / 2. This test works for not just 3 sticks, but any number of sticks > 2
Try this:
def is_triangle(a, b, c):
if((a + b > c) && (a + c > b) && (b + c > a)):
print "Yes"
else:
print "No"
Returns True or False!
def is_triangle(a, b, c):
if (a + b <= c) or (a + c <= b) or (b + c <= a):
return False
else:
return True
or:
def is_triangle(a, b, c):
a, b, c = sorted([a, b, c])
return a + b > c
So first you need to ask for the input and name the sticks:
stick1 = float(input('Enter length for stick 1: '))
stick2 = float(input('Enter length for stick 2: '))
stick3 = float(input('Enter length for stick 3: '))
Then, if you know that one stick can’t be longer than the sum of 2 other sticks, you form a condition. If stick x> stick y + stick z, then your program has to say ‘No’:
if stick1 > stick2 + stick3:
print('No')
elif stick2 > stick1 + stick3:
print('No')
elif stick3 > stick1 + stick2:
print('No')
else:
# in other case (we excluded every possible options when it is impossible
to create a triangle):
print('Yes')
I came across an exercise in "Think Python", and I couldn’t figure out how to do it.
The exercise:
- Write a function named is_triangle that takes three integers as arguments, and that prints either “Yes” or “No,” depending on whether you can or cannot form a triangle from sticks with the given lengths.
And of course:
If any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can3.
There are plenty of ways to do this. Here is one:
def is_triangle(a, b, c):
if (a > b + c) or (b > a + c) or (c > a + b):
print "No"
else:
print "Yes"
The triangle inequality theorem states that any side of a triangle is always shorter than the sum of the other two sides.
In the above code, it checks whether any side is greater than or equal to sum of other sides & print “Not Possible”.
Code:
>>> def is_triangle(sides):
... for i,l in enumerate(sides):
... if sides[i] > (sides[(i+1)%3] + sides[(i+2)%3]): return False
... return True
...
>>> is_triangle([3,4,5])
True
>>>
Use:
def is_triangle(a, b, c):
return a + b + c >= 2 * max(a, b, c) # Supposed circumference suffices for largest edge and back.
The sum of two sides of a triangle must be greater than the third side to make a valid triangle
Here goes the code:
def is_triangle(a,b,c): # Take three arguments
if(a+b>c)and(b+c>a)and(a+c>b): # Check the conditions
print "Yes"
else:
print "No"
One way to look at it is to notice that if any side is larger than half the perimiter (ie. Sum of stick lengths) then it can’t form a triangle. So test each stick being smaller than perim / 2. This test works for not just 3 sticks, but any number of sticks > 2
Try this:
def is_triangle(a, b, c):
if((a + b > c) && (a + c > b) && (b + c > a)):
print "Yes"
else:
print "No"
Returns True or False!
def is_triangle(a, b, c):
if (a + b <= c) or (a + c <= b) or (b + c <= a):
return False
else:
return True
or:
def is_triangle(a, b, c):
a, b, c = sorted([a, b, c])
return a + b > c
So first you need to ask for the input and name the sticks:
stick1 = float(input('Enter length for stick 1: '))
stick2 = float(input('Enter length for stick 2: '))
stick3 = float(input('Enter length for stick 3: '))
Then, if you know that one stick can’t be longer than the sum of 2 other sticks, you form a condition. If stick x> stick y + stick z, then your program has to say ‘No’:
if stick1 > stick2 + stick3:
print('No')
elif stick2 > stick1 + stick3:
print('No')
elif stick3 > stick1 + stick2:
print('No')
else:
# in other case (we excluded every possible options when it is impossible
to create a triangle):
print('Yes')