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 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...
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?
-Let O (0,0,0) be the origin and A(a+1,b,c) where a,b and c are the last three...
-Let O (0,0,0) be the origin and A(a+1,b,c) where a,b and c are the last three digits of your student ID. Find, describe and sketch the set of points P such that OP is perpendicular to AP - Let u=<a , b+1 , c+1> find and describe all vectors v such that |u x v|=|u| a=1 , b=1 , c=9
Complete the following program so that C = A*B where A and B are the matrices...
Complete the following program so that C = A*B where A and B are the matrices defined below. N=4; M=3; A=rand(M,N); B=rand(N,M); C = ? for m = 1: M    for n = 1 : ? for p = 1 : ? C (m,n) = ?       end    end end You may upload a script
2 In which property of protein, amino acid sides chains? a) size b) peptide bonding c)...
2 In which property of protein, amino acid sides chains? a) size b) peptide bonding c) function d) donding 2 Wich of this statements its true about protein structure ? a)Tertiary structure increases protein function, and quaternary structure decreases protein function. b)Tertiary structure involves three basic forms of protein folding, and quaternary structure involves 4 basic forms of protein folding. c) Tertiary structure involves all types of bonds (covalent, ionic, hydrogen, and hydrophobic interactions), and quaternary structure involves just hydrogen...
q = a(b+c(de+f)), t = !(q+a), m = aq, where the inputs are a, b, c,...
q = a(b+c(de+f)), t = !(q+a), m = aq, where the inputs are a, b, c, d, e, f and q; and outputs are t and m. Please provide the truth table. Please write the Sum of Products for the output t. Implement the Sum of Products of t with AND2, OR2 and NOT gates. Convert the previous part by using ONLY NAND2 gates. Provide MIPS code for the last part.
Draw ? on a torus (or the schematic representation where opposite sides of a rectangle are...
Draw ? on a torus (or the schematic representation where opposite sides of a rectangle are identified.)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT