Question

In: Computer Science

BackgroundConsider a trianglewith sides of lengths a, b, and c where c is the...

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**2
  • right 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()

Solutions

Expert Solution

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()


Related Solutions

A long conducting pipe has a rectangular cross section with sides of lengths a and b....
A long conducting pipe has a rectangular cross section with sides of lengths a and b. One face of the pipe is maintained at a constant potential V = V0 while the other 3 faces are grounded (V = 0). Using separation of variables, find the potential for points inside the pipe V (x,y).
For the following questions: A. Draw a picture B. Compute the different lengths C. Compare lengths...
For the following questions: A. Draw a picture B. Compute the different lengths C. Compare lengths or replace into formula for sphere D. Explain your final results in complete sentences Find the midpoint of the segment connecting the points A(3,5,2) and B(4,8,6). Find the equation of a sphere if one of the diameters has endpoints A(5,4,3) and B(1,6,-9). Determine whether the points A(1,0,1), B(2,1,-1), and C(3,2,1) all lie on the same straight line. Determine whether the triangle with corners at...
Write a program that asks the user for the lengths of the sides of a rectangle....
Write a program that asks the user for the lengths of the sides of a rectangle. Again, check for valid input and exit with an error msg if you don’t get it. Testing: use some known values to confirm that the calculations are correct. E.g. 3 – 4 - 5 triangle >> 3 X 4 rectangle Then print • The area and perimeter of the rectangle • The length of the diagonal (use the Pythagorean theorem). This question should be...
Write a program that prompts for the lengths of the sides of a triangle and reports...
Write a program that prompts for the lengths of the sides of a triangle and reports the three angles. Make sure you have a good introduction stating what the program does when it is run, and a helpful prompt for the user when asking for input: i.e. Here is the generated output from a sample program: This program computes the angles of a triangle given the lengths of the sides. What is the length of side 1? <wait for user...
Write a program that accepts the lengths of three sides of a triangle as inputs. The...
Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is a right triangle. Recall from the Pythagorean theorem that in a right triangle, the square of one side equals the sum of the squares of the other two sides. Use The triangle is a right triangle. and The triangle is not a right triangle. as your final outputs. An example of the program input...
Write a program named Triangle.java that asks for the lengths of the three sides of a...
Write a program named Triangle.java that asks for the lengths of the three sides of a triangle and computes the perimeter if the input is valid. The input is valid if the sum of every pair of two sides is greater than the remaining side. For example, the lengths 3, 4, and 5 define a valid triangle: 3 plus 4 is greater than 5; 4 plus 5 is greater than 3, and 5 plus 3 is greater than 4. However,...
use c++ A right-angle triangle with integer sides a, b, c, such as 3, 4, 5,...
use c++ A right-angle triangle with integer sides a, b, c, such as 3, 4, 5, is called a Pythagorean triple. The condition is that hypotenuse to power of 2 is equal to adding of square of the 2 other sides. In this example 25 = 16 + 9. Use brute force approach to find all the triples such that no side is bigger than 50. For this you should first implement the following Triangle class and its methods. Then...
/* A right-angle triangle with integer sides a, b, c, such as 3, 4, 5, is...
/* A right-angle triangle with integer sides a, b, c, such as 3, 4, 5, is called a Pythagorean triple. The condition is that hypotenuse to power of 2 is equal to adding of square of the 2 other sides. In this example 25 = 16 + 9. Use brute force approach to find all the triples such that no side is bigger than 50. For this you should first implement the following Triangle class and its methods. Then use...
“Triangle Guessing” game in Python The program accepts the lengths of three (3) sides of a...
“Triangle Guessing” game in Python The program accepts the lengths of three (3) sides of a triangle as input . The program output should indicate if the triangle is a right triangle, an acute triangle, or an obtuse triangle. Make sure each side of the triangle is a positive integer. Use try/except for none positive integer input. validating the triangle. That is the sum of the lengths of any two sides of a triangle is greater than the length of...
A parallelogram has consecutive sides with lengths 9 and 7 and diagonals of integral length How...
A parallelogram has consecutive sides with lengths 9 and 7 and diagonals of integral length How long are these diagonals?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT