コード例 #1
0
def test_triangle():
    """
    test triangle and search count of errors
    :return: int
    """
    errors = 0
    try:
        import triangle
        import point
    except:
        errors += 1
    try:
        triangle = triangle.Triangle(point.Point(1, 1), point.Point(3, 1),\
                                     point.Point(2, 3))
    except:
        errors += 1
    try:
        print(triangle.is_triangle())
    except:
        errors += 1
    try:
        print(triangle.perimeter())
    except:
        errors += 1
    try:
        print(triangle.area())
    except:
        errors += 1
    try:
        print(triangle)
    except:
        errors += 1
    return errors
コード例 #2
0
def test_lengths_from_valid_angles(lengths):
    def angles():
        a, b, c = lengths
        for _ in range(3):
            yield math.acos((a**2 + b**2 - c**2) / (2 * a * b))
            a, b, c = b, c, a

    def valid_angles():
        try:
            positive = True if not any(i <= 0 for i in angles()) else False
            sum_to_pi = math.isclose(sum(angles()), math.pi, rel_tol=1e-7)
            return positive and sum_to_pi
        except (ValueError, OverflowError):
            return False

    assert triangle.is_triangle(*lengths) == valid_angles()
コード例 #3
0
def test_all_zeros():
    assert triangle.is_triangle(0, 0, 0) is False
コード例 #4
0
def test_345():
    assert triangle.is_triangle(3, 4, 5)
コード例 #5
0
def test_bad_type_input(lst):
    with pytest.raises(TypeError):
        triangle.is_triangle(*lst)
コード例 #6
0
def test_machine_precision_small():
    assert triangle.is_triangle(1e-100, 1e-150, 1e-100)
コード例 #7
0
def test_machine_precision_large():
    assert triangle.is_triangle(1e100, 0.1, 1e100)
コード例 #8
0
ファイル: e42.py プロジェクト: kyuf/euler
#euler 42
"""
The nth term of the sequence of triangle numbers is given by, tn = n(n+1)/2; so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.

Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
"""
from triangle import is_triangle

with open("words.txt", "r") as f:
    words = f.readline().split('","')
    
#remove leading "
words[0] = words[0][1:]
#remove trailing "/n
words[-1] = words[-1][:-2]

count = 0

for word in words:
    Tn = 0
    for letter in word:
        Tn += (ord(letter) - 64)
    if is_triangle(Tn):
        count += 1
print(count)
コード例 #9
0
def test_zero_lengths_return_false(lengths):
    assert triangle.is_triangle(*lengths) is False
コード例 #10
0
def test_negative_lengths_return_false(lengths):
    assert triangle.is_triangle(*lengths) is False
コード例 #11
0
def test_equilateral():
    assert triangle.is_triangle(1, 1, 1)
コード例 #12
0
import triangle
import point

if __name__ == '__main__':
    triangle = triangle.Triangle(point.Point(1, 1), point.Point(3, 1), point.Point(2, 3))
    assert triangle.is_triangle()
    assert triangle.perimeter() == 6.47213595499958
    assert triangle.area() == 2.0
コード例 #13
0
def test_randos():
    for _ in range(40):
        a, b, c = [randint(-2, 10) for i in range(3)]
        assert is_triangle(a, b, c) == solution(a, b, c)
コード例 #14
0
def test_is_triangle(val1, val2, val3, result):
    """Test the smaller examples."""
    assert is_triangle(val1, val2, val3) == result
コード例 #15
0
def test_one_negative():
    assert triangle.is_triangle(-5, 0.9, 100) is False
コード例 #16
0
def test_negative_equilateral():
    assert triangle.is_triangle(-1, -1, -1) is False
コード例 #17
0
def test_pythagorean_triples(a_sq):
    b_sq = ((a_sq - 1) / 2)**2
    c_sq = b_sq + a_sq
    assert triangle.is_triangle(*[math.sqrt(i) for i in [a_sq, b_sq, c_sq]])