Dogukan Sonmez

Currently in Munich, Germany

Java Groovy Python

No-Sql TDD Scrum

Django Shell Cloud

May 09 2012

Puzzles with Python

In this puzzles series I'll try to solve puzzles by using python. Frankly I am not good at python programming(at least right now:)). I am also trying to learn python so it's good chance for me doing some practices.

Puzzle #1, Find Triangle

Problem Definition:

You're interested in writing a program to classify triangles. Triangles can be classified according to their internal angles. If one of the internal angles is exactly 90 degrees, then that triangle is known as a "right" triangle. If one of the internal angles is greater than 90 degrees, that triangle is known as an "obtuse" triangle. Otherwise, all the internal angles are less than 90 degrees and the triangle is known as an "acute" triangle.

Triangles can also be classified according to the relative lengths of their sides. In a "scalene" triangle, all three sides have different lengths. In an "isosceles" triangle, two of the sides are of equal length. (If all three sides have the same length, the triangle is known as an "equilateral" triangle, but you can ignore this case since there will be no equilateral triangles in the input data.)

Your program must determine, for each set of three points, whether or not those points form a triangle. If the three points are not distinct, or the three points are collinear, then those points do not form a valid triangle. (Another way is to calculate the area of the triangle; valid triangles must have non-zero area.) Otherwise, your program will classify the triangle as one of "acute", "obtuse", or "right", and one of "isosceles" or "scalene".

Input

The first line of input gives the number of cases, N. N test cases follow. Each case is a line formatted as

 x1 y1 x2 y2 x3 y3 

Output

For each test case, output one line containing "Case #x: " followed by one of these strings:

           isosceles acute triangle
           isosceles obtuse triangle
           isosceles right triangle
           scalene acute triangle
           scalene obtuse triangle
           scalene right triangle
           not a triangle 

Example

Simple Input:

            0 0 0 4 1 2
            1 1 1 4 3 2
            2 2 2 4 4 3
            3 3 3 4 5 3
            4 4 4 5 5 6
            5 5 5 6 6 5
            6 6 6 7 6 8
        

Simple Output:

            Case #1: isosceles obtuse triangle
            Case #2: scalene acute triangle
            Case #3: isosceles acute triangle
            Case #4: scalene right triangle
            Case #5: scalene obtuse triangle
            Case #6: isosceles right triangle
            Case #7: not a triangle
        

My solution

            __author__ = 'dogukan'

            def calculateDistance(x1, y1, x2, y2):
            """ distance between two points"""
            return (x1 - x2) ** 2 + (y1 - y2) ** 2


            def startProcess(x1, y1, x2, y2, x3, y3,i):
            if not ((x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1)):
            return "Case #" + str(i) +": not a triangle"
            else:
            result_1 = "acute"
            a = calculateDistance(x1, y1, x2, y2)
            b = calculateDistance(x1, y1, x3, y3)
            c = calculateDistance(x2, y2, x3, y3)

            condition_1 = (b + c - a)
            condition_2 = (a + c - b )
            condition_3 = (b + a - c)

            if condition_1 == 0 or condition_2 == 0 or condition_3 == 0:
            result_1 = "right"
            elif condition_1 < 0 or condition_2 < 0 or condition_3 < 0:
            result_1 = "obtuse"

            result_2 = "scalene"
            temp = {a: 1, b: 2, c: 3}
            if len(temp) == 2:
            result_2 = "isosceles"
            return "Case #" + str(i) +": " + result_2 + " " + result_1 + " triangle"

            file = open("input")
            n = int(file.readline())
            result = []
            for i in range(1,n+1):
            line = file.readline()
            points = line.split(" ")
            print startProcess(int(points[0]), int(points[1]), int(points[2]), int(points[3]), int(points[4]), int(points[5]),i)+"\n"