#三角形のタイプ判定
def is_triangle(a, b, c):
"""
3つの正整数が辺の長さとして三角形を構成できるかどうかを判定する関数
Args:
a: 1辺の長さ
b: 1辺の長さ
c: 1辺の長さ
Returns:
True: 三角形を構成できる場合
False: 三角形を構成できない場合
"""
return a + b > c and a + c > b and b + c > a
def triangle_type(a, b, c):
"""
与えられた辺の長さで構成される三角形のタイプを判定する関数
Args:
a: 1辺の長さ
b: 1辺の長さ
c: 1辺の長さ
Returns:
"acute": 鋭角三角形の場合
"right": 直角三角形の場合
"obtuse": 鈍角三角形の場合
"isosceles":二等辺三角形
"equilateral":正三角形
"none": 三角形を構成できない場合
"""
if not is_triangle(a, b, c):
return "none"
squares = [a**2, b**2, c**2]
squares.sort()
if a == b and a==c and b==c:
return "equilateral"
if squares[0] + squares[1] == squares[2]:
return "right"
elif squares[1] == squares[2] :
return "isosceles"
elif squares[0] + squares[1] < squares[2]:
return "acute"
else:
return "obtuse"
print("triangle_type(1,1,1)")
print(triangle_type(1,1,1))
print("triangle_type(3,4,5)")
print(triangle_type(3,4,5))
print("triangle_type(5,1,1)")
print(triangle_type(5,1,1))
print("triangle_type(5,6,6)")
print(triangle_type(5,6,6))
print("triangle_type(5,6,4)")
print(triangle_type(5,6,4))
print("triangle_type(3,6,4)")
print(triangle_type(3,6,4))
print()
0 件のコメント:
コメントを投稿