Advertisement
SimeonTs

SUPyF Objects and Classes - 08. Boxes

Aug 12th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. """
  2. Objects and Classes
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/950#7
  4.  
  5. SUPyF Objects and Classes - 08. Boxes
  6.  
  7. Problem:
  8. Create a class Box, which will represent a rectangular box. The Box should have UpperLeft (Point), UpperRight (Point),
  9. BottomLeft (Point), BottomRight (Point).
  10. Create, or use from the Lab, the class Point which has X (int) and Y (int) – coordinates in 2D space.
  11. Move the CalculateDistance() method in the Point class, exactly as it is.
  12. Then use “Point.CalculateDistance(point1, point2)” signature, to use the method.
  13. Create 2 methods in the Box class:
  14. CalculatePerimeter(width, height)
  15. CalculateArea(width, height).
  16. Make them return integers, representing the perimeter and area of the box.
  17. The formulas are respectively – (2 * Width + 2 * Height) and (Width * Height).
  18. The Width is the distance between the UpperLeft and the UpperRight Points,
  19. and ALSO – the Bottomleft and the BottomRight Points.
  20. The Height is the distance between the UpperLeft and the BottomLeft Points,
  21. and ALSO – the UpperRight and the BottomRight Points.
  22. You will receive several input lines in the following format:
  23. {X1}:{Y1} | {X2}:{Y2} | {X3}:{Y3} | {X4}:{Y4}
  24. Those will be the coordinates to UpperLeft, UpperRight, BottomLeft and BottomRight (IN THE SAME ORDER).
  25. When you receive the command “end”. You must print all Boxes in the following format:
  26. “Box: {width}, {height}
  27. Perimeter: {perimeter}
  28. Area: {area}”
  29. Examples:
  30.    Input:
  31.        0:2 | 2:2 | 0:0 | 2:0
  32.        -3:0 | 0:0 | -3:-3 | 0:-3
  33.        -2:2 | 2:2 | -2:-2 | 2:-2
  34.        end
  35.    Output:
  36.        Box: 2, 2
  37.        Perimeter: 8
  38.        Area: 4
  39.        Box: 3, 3
  40.        Perimeter: 12
  41.        Area: 9
  42.        Box: 4, 4
  43.        Perimeter: 16
  44.        Area: 16
  45. """
  46.  
  47.  
  48. import math
  49.  
  50.  
  51. class Box:
  52.     def __init__(self, width, height, perimeter, area):
  53.         self.width = width
  54.         self.height = height
  55.         self.perimeter = perimeter
  56.         self.area = area
  57.  
  58.  
  59. def calculate_distance(u_l, u_r, b_l, b_r):
  60.     width = math.sqrt(math.pow(u_l[0] - u_r[0], 2) + math.pow(u_l[1] - u_r[1], 2))
  61.     height = math.sqrt(math.pow(u_l[0] - b_l[0], 2) + math.pow(u_l[1] - b_l[1], 2))
  62.     perimeter = 2 * width + 2 * height
  63.     area = width * height
  64.     current_box = Box(width, height, perimeter, area)
  65.     global all_boxes
  66.     all_boxes += [current_box]
  67.  
  68.  
  69. all_boxes = []
  70.  
  71. while True:
  72.     inp = input()
  73.     if inp == "end":
  74.         break
  75.     data = [item for item in inp.split(" | ")]
  76.     u_l_ = [int(item) for item in data[0].split(":")]
  77.     u_r_ = [int(item) for item in data[1].split(":")]
  78.     b_l_ = [int(item) for item in data[2].split(":")]
  79.     b_r_ = [int(item) for item in data[3].split(":")]
  80.     calculate_distance(u_l_, u_r_, b_l_, b_r_)
  81.  
  82. for box in all_boxes:
  83.     print(f"Box: {int(box.width)}, {int(box.height)}")
  84.     print(f"Perimeter: {int(box.perimeter)}")
  85.     print(f"Area: {int(box.area)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement