【每日一题0106】有效三角形

:woman_mage: 给定3个正整数a,b,c分别表示三角形的边长,请编写一个函数,判断给定的条件是否能够构成一个有效的三角形。如果能则返回True,否则返回False。

备注:有效的三角形需要满足的条件是“任意两条边长之和大于第三边”。

示例:
输入:a=7, b=10, c=5,返回:True
输入:a=1, b=10, c=12,返回:False

题目难度:简单
题目来源:CodeWars:Is this a triangle?

def solution(a, b, c) -> bool:
    # your code

assert solution(1, 2, 2) is True
assert solution(7, 10, 5) is True
assert solution(1, 10, 12) is False
def solution(a, b, c) -> bool:
    return sum((a, b)) > c and sum((a, c)) > b and sum((b, c)) > a


assert solution(1, 2, 2) is True
assert solution(7, 10, 5) is True
assert solution(1, 10, 12) is False
def solution(a, b, c) -> bool:
    if a + b > c and a + c > b and b + c > a:
        return True
    else:
        return False


assert solution(1, 2, 2) is True
assert solution(7, 10, 5) is True
assert solution(1, 10, 12) is False

import heapq
def solution(a,b,c):
    list = [a, b, c]
    return max(list)<sum(heapq.nsmallest(2,list))
def solution(a, b, c) -> bool:
    if a+b > c and a+c > b and b+c > a:
        return True
    else:
        return False
def solution(a, b, c) -> bool:
    return (a+b > c) and (a+c > b) and (b+c > a)
assert solution(1, 2, 2) is True
assert solution(7, 10, 5) is True
assert solution(1, 10, 12) is False
def solution15(a,b,c):
    if a+b > c and a+c > b and b+c > a:
        return True
    else:
        return False
assert solution15(1, 2, 2) is  True
assert solution15(7, 10, 5) is True
assert solution15(1, 10, 12) is False
def solution(a, b, c) -> bool:
    return a + b > c and b + c > a and c + a > b
def solution(a, b, c) -> bool:
    return min(a+b,b+c,a+c) > max(a,b,c)

assert solution(1, 2, 2) is True
assert solution(7, 10, 5) is True
assert solution(1, 10, 12) is False
assert solution(0, 10, 12) is False
assert solution(-7, -10, -5) is False
def solution(a, b, c) -> bool:
    if a+b>c and a+c>b and b+c >a:
        return True
    else:
        return False
def solution(a, b, c) -> bool:
    if a > 0 and b > 0 and c > 0 and a + b > c and b + c > a and c + a > b:
        return True
    else:
        return False

assert solution(1, 2, 2) is True
assert solution(7, 10, 5) is True
assert solution(1, 10, 12) is False
def solution(a, b, c) -> bool:
    length_list = [a, b, c]
    # 对三边长度进行排序,只需要判断2个短边之和大于长边
    length_list.sort()
    return length_list[0] + length_list[1] > length_list[2]


assert solution(1, 2, 2) is True
assert solution(7, 10, 5) is True
assert solution(1, 10, 12) is False
关闭