def main(): sh = Shape() print sh t = Triangle(5, 10) print t.area() print "%s Static Method" % t.static_area(10, 20) s = Square(20) print s.area() print s.perimeter()
def main(): Lines=1 Lines_valid=0 total_perimeter=0 total_area=0 largest_perimeter=0 largest_perimeters_area=0 largest_area=0 largest_areas_perimeter=0 triangle_perimeter='' triangle_area='' for line in fo: line = "".join(c for c in line if c not in ('(',')',' ')) linelist=line.strip().replace(' ', '').split(',') print("Line ",Lines,": ",line) sideA=linelist[0] sideB=linelist[1] sideC=linelist[2] triangle=Triangle(sideA,sideB,sideC) if triangle.is_valid()==True: Lines_valid+=1 total_perimeter+=triangle.perimeter() total_area+=triangle.area() if triangle.perimeter()>largest_perimeter: largest_perimeter=triangle.perimeter() largest_perimeters_area=triangle.area() triangle_perimeter=line if triangle.area()>largest_area: largest_area=triangle.area() largest_areas_perimeter=triangle.perimeter() triangle_area=line print("This Triangle's perimeter= {}, and area= {}".format(triangle.perimeter(),triangle.area())) else: print("Triangle is not valid") Lines+=1 print("") print("Total lines processed: ",Lines) print("Total valid triangles processed: ",Lines_valid) avg_perimeter=total_perimeter/Lines_valid avg_area=total_area/Lines_valid print("Average perimeter: ",avg_perimeter) print("Average area: ",avg_area) print("The Triangle with the largest perimeter= ",largest_perimeter," had a area= ",largest_perimeters_area," with sides: ",triangle_perimeter) print("The Triangle with the largest area= ",largest_area," had a perimeter= ",largest_areas_perimeter," with sides: ",triangle_area)
class TestTriangle(unittest.TestCase): def setUp(self): self.triangle = Triangle(100, 30) # Return dimension def test_area(self): self.assertEqual(1500, self.triangle.area())
def main(args): side1 = int(args[1]) side2 = int(args[2]) side3 = int(args[3]) height = int(args[4]) print("the three sides and height of triangle area %d %d %d and %d" % (side1, side2, side3, height)) t1 = Triangle(side1, side2, side3, height) print("Perimeter: ", t1.perimeter()) print("Area: ", t1.area())
from rectangle import Rectangle from triangle import Triangle rect = Rectangle() tri = Triangle() rect.set_values(50, 40) tri.set_values(50, 40) rect.set_color('red') tri.set_color('blue') print(rect.area()) print(tri.area()) print(rect.get_color()) print(tri.get_color())
def test_area(self): self.assertEqual(Triangle.area(triangle), 2.0)
def test_area(self): p1 = Point(0, 0) p2 = Point(3, 4) p3 = Point(3, 0) t = Triangle(p1, p2, p3) self.assertEqual(t.area(), 6)
def main(): """ analogous to other programming languages in Python has no special role """ # ============================================================================= # example 1 # ============================================================================= # # #named object created without parameters # # _p_1 = Point() # # #call object services # # _len_1 = _p_1.length() # # print("Point 1 distance from origo ", _len_1) # # #named object created with parameters # # _p_2 = Point(3, 4) # # #call object services # # _len_2 = _p_2.length() # # print("Point 2 distance forr origo ", _len_2) # # #nameless object created without parameters # # #call object services # # _len_3 = Point().length() # # print("Nameless point created without parameters distance from origo ", _len_3) # # #nameless object created with parameters # # #call object services # # _len_4 = Point(7, 1).length() # # print("Nameless point created with parameters (7,1) distance from origo ", _len_4) # ============================================================================= # ============================================================================= # exercize2 # create point objects and calculate the distances # ============================================================================= _p_1 = Point() _p_1.xy(1, 2) _p_2 = Point(4, 3) _p_3 = Point(6, -2) print( f"Point 1:({_p_1.x}, {_p_1.y}) length (length from origo) is {_p_1.length():.2f}" ) print( f"Point 2:({_p_2.x}, {_p_2.y}) length (length from origo) is {_p_2.length():.2f}" ) print( f"Point 3:({_p_3.x}, {_p_3.y}) length (length from origo) is {_p_3.length():.2f}" ) print( f"Point 1:({_p_1.x}, {_p_1.y}) length from Point 3 is {_p_1.length(_p_3):.2f}" ) print( f"Point 2:({_p_2.x}, {_p_2.y}) length from Point 1 is {_p_2.length(_p_1):.2f}" ) print( f"Point 3:({_p_3.x}, {_p_3.y}) length from Point 2 is {_p_3.length(_p_2):.2f}" ) _p_4 = Point(4, 5) print( f"Point 1:({_p_1.x}, {_p_1.y}) length from Point is ({_p_4.x},{_p_4.y})", f" is {length(_p_1, _p_4):.2f}") print( f"Point 2:({_p_2.x}, {_p_2.y}) length from Point is ({_p_4.x},{_p_4.y})", f" is {length(_p_2, _p_4):.2f}") print( f"Point 3:({_p_3.x}, {_p_3.y}) length from Point is ({_p_4.x},{_p_4.y})", f" is {length(_p_3, _p_4):.2f}") _abc = Triangle() print(_abc.perimeter(_p_1, _p_2, _p_3)) print(_abc.area(_p_1, _p_2, _p_3))
def triangle(base=0, height=0): tri = Triangle(base, height) return "Area of triangle is {}".format(tri.area())
from triangle import Triangle from rectangle import Rectangle from Shape import Shape rect = Rectangle() tri = Triangle() rect.set_values(70, 40) tri.set_values(50, 40) rect.set_color('Red') tri.set_color('blue') print(rect.area(), " Area of Rectangle") print(tri.area(), " Area of Triangle") print(rect.get_color()) print(tri.get_color())
from square import Square from triangle import Triangle s1 = Square() s1.set_value(8, 15) s1.set_color("Blue") print(s1.area(), s1.get_color()) t1 = Triangle() t1.set_value(5, 10) t1.set_color("Green") print(t1.area(), t1.get_color())
#!/bin/env python3 from point import Point from triangle import Triangle import doctest triangle = Triangle(Point(1, 1), Point(3, 1), Point(2, 3)) print(repr(triangle.is_triangle())) print(repr(triangle.perimeter())) print(repr(triangle.area())) triangle = Triangle(Point(1, 1), Point(1, 1), Point(2, 2)) print(repr(triangle.perimeter())) print(repr(triangle.area()))
from triangle import Triangle from square import Square s1 = Square() s1.set_value(2, 3) s1.set_color("Green") print(s1.area(), s1.get_color()) s2 = Triangle() s2.set_value(5, 6) s2.set_color("Blue") print(s2.area(), s2.get_color())
from triangle import Triangle from point import Point if __name__ == '__main__': triangle = Triangle(Point(1, 1), Point(3, 1), Point(2, 3)) print(triangle.perimeter()) assert isinstance(triangle, Triangle) print("Methods: ", dir(triangle)) print("Attributes: ", triangle.__dict__) assert triangle.area() == 2.0 print("There are no errors. Thank You!")
def test_area(self): tria = Triangle(3, 4, 30) self.assertEqual(3, tria.area())
from rectangle import Rectangle from triangle import Triangle import fun rectangle = Rectangle() rectangle.set_value(3, 4) rectangle.set_color('blue') print(rectangle.area()) print(rectangle.get_color()) triangle = Triangle() triangle.set_value(3, 4) triangle.set_color("yellow") print(triangle.area()) print(triangle.get_color()) print(fun.add(4, 7))
print(scalene_triangle.is_equilateral()) print('Is the Triangle isosceles?') print(scalene_triangle.is_isosceles()) print('Is the Triangle scalene?') print(scalene_triangle.is_scalene()) print('') print('What are the lengths of this Triangle again..?') print(scalene_triangle.sides()) print('What are the interior angles of this triangle?') print(scalene_triangle.angles()) print('What is the perimeter of this triangle?') print(scalene_triangle.perimeter()) print('What is the area of this triangle?') print(scalene_triangle.area()) print('What is the scale of this triangle with a factor of 1 then 2 then 5 and then 3/4') scalene_triangle.scale(1.0) print(scalene_triangle) scalene_triangle.scale(2.0) print(scalene_triangle) scalene_triangle.scale(5.0) print(scalene_triangle) scalene_triangle.scale(0.75) print(scalene_triangle)