Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q: Help with a code file in Python that contains a definition of a class point t

ID: 3823749 • Letter: Q

Question

Q: Help with a code file in Python that contains a definition of a class point that describes a dot in a two-dimensional space, and a class Circle that describes a circle by the center of the circle, R, and its radius. I have to use this function, def __lt__(self, other) : which compares two circles based on Comparison criterion: The maximum x coordinate of the points on the circle. It also should Check that insertion_sort correctly sorts the list of circles created by the given function create_circles_list. (Can you put some # comments to better understand especially later, Much thanks ahead of your Help)

Here is a sample of a possible run:

>>> circles = create_circles_list(4)

>>> print circles

[c = (8,7) r = 13, c = (4,7) r = 12, c = (4,6) r = 0, c = (5,0) r = 3]

>>> insertion_sort(circles)

>>> print 'sorted:' circles

sorted : [c = (4,6) r = 0, c = (5,0) r = 3, c = (4,7) r = 12, c = (8,7) r = 13]

Explanation / Answer

class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return "(" + str(self.x) + ", " + str(self.y) + ")"

class Circle:
def __init__(self, point, radius):
self.point = point
self.radius = radius
def __lt__(self, other):
return (self.point.x + self.radius)< (other.point.x + other.radius)
def __repr__(self):
return "c = " + str(self.point) + " r = " + str(self.radius)

def insertion_sort(alist):
for index in range(1,len(alist)):

currentvalue = alist[index]
position = index

while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1

alist[position]=currentvalue

def create_circles_list(n):
circles = []
circles.append(Circle(Point(8,7), 13))
circles.append(Circle(Point(4,7), 12))
circles.append(Circle(Point(4,6), 0))
circles.append(Circle(Point(5,0), 3))
return circles

circles = create_circles_list(4)
print circles
insertion_sort(circles)
print 'sorted:' + str(circles)

# pastebin link for code: https://pastebin.com/ETypP8hB