Facebook
From Melisa Rebeca, 1 Month ago, written in Python.
Embed
Download Paste or View Raw
Hits: 140
  1. class Point():
  2.     def __init__(self, x, y):  # x , y of type INTEGER
  3.         self.__x = x
  4.         self.__y = y
  5.  
  6.     def getX(self):
  7.         return self.__x
  8.  
  9.     def getY(self):
  10.         return self.__y
  11.  
  12.     def setX(self, setX):
  13.         self.__x = setX
  14.  
  15.     def setY(self, setY):
  16.         self.__y = setY
  17.  
  18.  
  19. class Square():
  20.     def __init__(self, top, bottom):  # top and bottom of type INTEGER
  21.         self.__top = top
  22.         self.__bottom = bottom
  23.  
  24.     def getTop(self):
  25.         return self.__top
  26.  
  27.     def getBottom(self):
  28.         return self.__bottom
  29.  
  30.     def getArea(self):
  31.         Area = (self.__top.x - self.__bottom) * (self.__top.y - self.__bottom.y)
  32.         if Area < 0:
  33.             Area = Area * (-1)
  34.         return Area
  35.  
  36.  
  37. class Circle():
  38.     def __init__(self, center, radius):  # center, radius of type INTEGER
  39.         self.__center = center
  40.         self.__radius = radius
  41.  
  42.     def getCenter(self):
  43.         return self.__center
  44.  
  45.     def getRadius(self):
  46.         return self.__radius
  47.  
  48.     def getArea(self):
  49.         return 3.14 * (self.__radius ** 2)
  50.  
  51.  
  52. class Triangle():
  53.     def __init__(self, x, y, z):
  54.         self.__x = x
  55.         self.__y = y
  56.         self.__z = z
  57.  
  58.     def getX(self):
  59.         return self.__x
  60.  
  61.     def getY(self):
  62.         return self.__y
  63.  
  64.     def getZ(self):
  65.         return self.__z
  66.  
  67.     def getArea(self):
  68.         return 1
  69.  
  70.  
  71. # b
  72.  
  73. circles = []
  74. squares = []
  75. triangles = []
  76. areas = []
  77.  
  78.  
  79. def readData():
  80.     try:
  81.         file = open("Shapes.txt", "r")
  82.         line = file.readline().strip()
  83.         while line != "":
  84.             if line == "circles":
  85.                 line1 = file.readline().strip()
  86.                 line2 = file.readline().strip()
  87.                 line1List = line1.split()
  88.                 center = Point(int(line1List[0]), int(line1List[1]))
  89.                 radius = int(line2)
  90.                 myCircle = Circle(center, radius)
  91.                 circles.append(myCircle)
  92.                 areas.append(("Circle", myCircle.getArea()))
  93.  
  94.             elif line == "square":
  95.                 line1 = file.readline().strip()
  96.                 line2 = file.readline().strip()
  97.                 line1List = line1.split()
  98.                 top = Point(int(line1List[0]), int(line1List[1]))
  99.                 line2List = line2.split()
  100.                 bottom = Point(int(line2List[0]), int(line2List[1]))
  101.                 mySquare = Square(top, bottom)
  102.                 squares.append(mySquare)
  103.                 areas.append((mySquare.getArea(), "square"))
  104.  
  105.             elif line == "triangle":
  106.                 line1 = file.readline().strip()
  107.                 line2 = file.readline().strip()
  108.                 line3 = file.readline().strip()
  109.                 line1List = line1.split()
  110.                 x = Point(int(line1List[0]), int(line1List[1]))
  111.                 line2List = line2.split()
  112.                 y = Point(int(line2List[0]), int(line2List[1]))
  113.                 line3List = line3.split()
  114.                 z = Point(int(line3List[0]), int(line3List[1]))
  115.                 myTriangle = Triangle(x, y, z)
  116.                 triangles.append(myTriangle)
  117.                 areas.append((myTriangle.getArea(), "triangle"))
  118.  
  119.     except IOError:
  120.         print("File not found")
  121.  
  122.  
  123. def sortCircles():
  124.     n = len(circles)
  125.     for i in range(n - 1):
  126.         for j in range(0, n - i - 1):
  127.             if circles[j].getCenter().getX() > circles[j + 1].getCenter().getX() and circles[j].getCenter().getY() > \
  128.                     circles[j + 1].getCenter().getY():
  129.                 temp = circles[j]
  130.                 circles[j] = circles[j + 1]
  131.                 circles[j + 1] = temp
  132.  
  133.  
  134. def sortSquares():  # using insertion sort
  135.     for i in range(1, len(squares)):
  136.         current_square = squares[i]
  137.         j = i - 1  # prev element
  138.         while j >= 0 and current_square.getArea() < squares[j].getArea():
  139.             temp = squares[j]
  140.             squares[j] = squares[j + 1]
  141.             squares[j + 1] = temp
  142.  
  143.  
  144. def smallestTriangle():
  145.     minArea = triangles[0].getArea()
  146.     for i in range(1, len(triangles)):
  147.         if triangles[i].getArea() < minArea:
  148.             minArea = triangles[i].getArea()
  149.     return minArea
  150.  
  151.  
  152. readData()
  153. sortCircles()
  154. sortSquares()
  155.