In: Computer Science
Background
Consider a triangle with sides of lengths a, b, and c where c is the longest side. We can describe the side and angle properties of the triangle using these lengths.
The side properties include:
scalene if all three sides have a different length
isosceles if two of the sides have the same length
equilateral if all three sides have the same length
The Pythagorean Theorem determines the angle properties which include:
acute if all angles are acute (
<90 2="" or="">c**2right if one angle is right (=90 degrees) or a**2 + b**2 = c**2
obtuse if one angle is obtuse *(>90 degrees) or a**2 + b**2 < c**2
Note that the sides do not represent a triangle if a + b <= c. Both the side and angle relationships should be none in this case.
Solution
Complete the following program to:
prompt for a filename
print a heading for the output
print a line with the side and triangle properties for each triangle in the file
Example Input
Each line in the file contains 3 numbers separated by spaces that represent the side lengths of the triangle as in the following example. The lengths may be in any order on the line.
5 4 3 4 4 4
Example Output
The side lengths and properties should align with the column headings as in the following example. The side lengths should be right justified and in the same order as the input file, while the properties should be left justified.
111 222 333 angle side 5 4 3 right scal 4 4 4 acute equi
Test Data File
There is a single test file, triangles you may use to test your program during development.
3 5 4 3 4 4 3 3 6 4 4 4 2 11 4 6 4 3
Provided Code:
# Triangle Properties
#
# This program:
# * prompts for a filename
# * prints a heading for the output
# * prints a line with the side and triangle properties for each
triangle in the file
#
# Refer to the above description for definitions of
triangles/angles
def side(a, b, c):
"""
Returns 'scalene', 'isosceles', 'equilateral', or 'none'
based on the triangle side relationships given the lengths of the
sides.
"""
# add code to return the correct side relationship
def angle(a, b, c):
"""
Returns 'acute', 'obtuse', 'right', or 'none'
based on the triangle angle relationships given the lengths of the
sides
where c is the longest side.
"""
# add code to return the correct angle relationship
def triangle_properties(sides):
"""
Returns a properly formatted string containing the triangle
properties
from a list of strings containing the lengths of the sides.
"""
# Don't change this code that figures out the longest side.
abc = [int(sides[0]),int(sides[1]),int(sides[2])]
c = max(abc)
abc.remove(c)
a = abc[0]
b = abc[1]
# add code to return a properly formatted string that matches the
column headings
def column_headings():
"""
Return a string containing the column headings.
DO NOT CHANGE THIS CODE
"""
return '111 222 333 angle side'
def triangles(filename):
"""
Prints column headings and a line containing the properties of each
triangle in the file.
"""
print(column_headings())
# add code to process the file, printing the triangle properties
for each line in file
def main():
"""
Prompts for filename and processes the file.
DO NOT CHANGE THIS CODE
"""
filename = input('filename?\n')
triangles(filename)
if __name__ == '__main__':
main()
Code:
# Triangle Properties
#
# This program:
# * prompts for a filename
# * prints a heading for the output
# * prints a line with the side and triangle properties for each triangle in the file
#
# Refer to the above description for definitions of triangles/angles
def side(a, b, c):
"""
Returns 'scalene', 'isosceles', 'equilateral', or 'none'
based on the triangle side relationships given the lengths of the sides.
"""
# add code to return the correct side relationship
if a!=b and a!=c and b!=c:
return "scal"
elif a==b==c:
return "equi"
elif a==b or a==c or b==c:
return "isos"
else:
return "none"
def angle(a, b, c):
"""
Returns 'acute', 'obtuse', 'right', or 'none'
based on the triangle angle relationships given the lengths of the sides
where c is the longest side.
"""
# add code to return the correct angle relationship
if a**2 + b**2 > c**2:
return "acute"
elif a**2 + b**2 == c**2:
return "right"
elif a**2 + b**2 > c**2:
return "obtuse"
else:
return "none"
def triangle_properties(sides):
"""
Returns a properly formatted string containing the triangle properties
from a list of strings containing the lengths of the sides.
"""
# Don't change this code that figures out the longest side.
abc = [int(sides[0]),int(sides[1]),int(sides[2])]
c = max(abc)
abc.remove(c)
a = abc[0]
b = abc[1]
# add code to return a properly formatted string that matches the column headings
return (a,b,c)
def column_headings():
"""
Return a string containing the column headings.
DO NOT CHANGE THIS CODE
"""
return '111 222 333 angle side'
def triangles(filename):
"""
Prints column headings and a line containing the properties of each triangle in the file.
"""
print(column_headings())
# add code to process the file, printing the triangle properties for each line in file
fp = open(filename)
for line in fp:
s = line.strip().split()
(a, b, c) = triangle_properties(s)
print(s[0], s[1], s[2], angle(a, b, c), side(a, b, c))
fp.close()
def main():
"""
Prompts for filename and processes the file.
DO NOT CHANGE THIS CODE
"""
filename = input('filename?\n')
triangles(filename)
if __name__ == '__main__':
main()