Move set_random() out of class
As generating a random Point is mostly not used on an existing object, the method is now an independent function get_random() instead of a class method.
This commit is contained in:
17
point.py
17
point.py
@@ -31,31 +31,26 @@ class Point():
|
|||||||
def get_y(self):
|
def get_y(self):
|
||||||
"""Returns the Y-Coordinate of the Point"""
|
"""Returns the Y-Coordinate of the Point"""
|
||||||
return self.__y
|
return self.__y
|
||||||
def set_random(self, min_x=0, max_x=100, min_y=0, max_y=100):
|
|
||||||
"""Set the point to a random place inside the boundaries"""
|
|
||||||
self.set_x(random.randint(min_x, max_x))
|
|
||||||
self.set_y(random.randint(min_y, max_y))
|
|
||||||
return self
|
|
||||||
def str(self):
|
def str(self):
|
||||||
"""Returns the coordinates as a string"""
|
"""Returns the coordinates as a string"""
|
||||||
return "(" + str(self.get_x()) + "/" + str(self.get_y()) + ")"
|
return "(" + str(self.get_x()) + "/" + str(self.get_y()) + ")"
|
||||||
def draw(self):
|
def draw(self):
|
||||||
"""TODO: Draw the point in a canvas"""
|
"""TODO: Draw the point in a canvas"""
|
||||||
|
|
||||||
|
def get_random(min_x=0, max_x=100, min_y=0, max_y=100):
|
||||||
|
"""Set the point to a random place inside the boundaries"""
|
||||||
|
return Point(random.randint(min_x, max_x), random.randint(min_y, max_y))
|
||||||
|
|
||||||
def sort_point_set(point_set, sort_by_y=False):
|
def sort_set(point_set, sort_by_y=False):
|
||||||
"""Sort a Point set using merge sort"""
|
"""Sort a Point set using merge sort"""
|
||||||
num_compares = 0
|
num_compares = 0
|
||||||
for pnt in point_set:
|
|
||||||
if not isinstance(pnt, Point):
|
|
||||||
raise Exception("Elements of point set are not all of type point.Point")
|
|
||||||
if len(point_set) > 1:
|
if len(point_set) > 1:
|
||||||
mid = len(point_set)//2 #Finding the mid of the array
|
mid = len(point_set)//2 #Finding the mid of the array
|
||||||
left = copy.deepcopy(point_set[:mid]) # Dividing the array elements
|
left = copy.deepcopy(point_set[:mid]) # Dividing the array elements
|
||||||
right = copy.deepcopy(point_set[mid:]) # into 2 halves
|
right = copy.deepcopy(point_set[mid:]) # into 2 halves
|
||||||
|
|
||||||
num_compares += sort_point_set(left, sort_by_y) # Sorting the first half
|
num_compares += sort_set(left, sort_by_y) # Sorting the first half
|
||||||
num_compares += sort_point_set(right, sort_by_y) # Sorting the second half
|
num_compares += sort_set(right, sort_by_y) # Sorting the second half
|
||||||
|
|
||||||
i = j = k = 0
|
i = j = k = 0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user