Skip to content
Snippets Groups Projects
Select Git revision
  • 5af7abb75a87146e58f9a4180cbbca630e1c705d
  • main default protected
2 results

main.py

Blame
  • main.py 1.56 KiB
    from enum import Enum
    
    from kivy.app import App
    from kivy.properties import StringProperty
    
    
    class Classification(Enum):
        INVALID = 'Invalid'
        EQUILATERAL = 'Equilateral'
        ISOSCELES = 'Isosceles'
        RIGHT = 'Right'
        SCALENE = 'Scalene'
    
    
    class TriangleApp(App):
        a = StringProperty('')
        b = StringProperty('')
        c = StringProperty('')
        classification = StringProperty()
    
        @staticmethod
        def classify(a, b, c):  # buggy
            if a * b * c < 0:
                return Classification.INVALID
            if a == b and b == c:
                return Classification.EQUILATERAL
            if a == b or b == c:
                return Classification.ISOSCELES
            maximum = a
            if b > maximum:
                maximum = b
                if c > maximum:
                    maximum = c
            semiperimeter = a + b + c / 2
            if maximum > semiperimeter:
                return Classification.INVALID
            if a ** 2 + b ** 2 == c ** 2 or b ** 2 + a ** 2 == c ** 2 or c ** 2 + b ** 2 == a ** 2:
                return Classification.RIGHT
            return Classification.SCALENE
    
        def update(self):
            try:
                self.classification = TriangleApp.classify(int(self.a), int(self.b), int(self.c)).value
            except ValueError:
                self.classification = f'{Classification.INVALID.value} (non-integer sides)'
    
        def on_start(self):
            self.update()
    
        def on_a(self, _, __):
            self.update()
    
        def on_b(self, _, __):
            self.update()
    
        def on_c(self, _, __):
            self.update()
    
    
    if __name__ == '__main__':
        app = TriangleApp()
        app.run()