Search
A circle is a 2D geometry shape characterized by the position of its centre and a radius. How can it be represented in Python?
The last option is the prefered one. It might be more difficult to implement a class/object but offers much more additional possibilities.
class
class Circle: """represents a circle in 2D space"""
def __init__(self, x, y, r): self.x = x self.y = y self.r = r
if __name__ == "__main__": c1 = Circle(1, 2, 2) c2 = Circle(4, 6, 4) print(c1.x, c1.y, c1.r) print(c2.x, c2.y, c2.r)
class Circle: (...) def get_area(self): area = 3.14 * (self.r ** 2) return area if __name__ == "__main__": c1 = Circle(1, 2, 2) print(c1.get_area())
def is_intersect(c1, c2): """Tells if the two circles intersect or not. :param c1: Circle object :param c2: Circle object :return: boolean: True if the c1, c2 intersect, otherwise False """ if __name__ == "__main__": c1 = Circle(1, 2, 2) c2 = Circle(4, 6, 4) print(is_intersect(c1,c2))
class Circle: (...) def insersects_with(self, other): """Tells if this and other circles intersect or not. :param other: Circle object :return: boolean: True if self and other intersect, otherwise False """ if __name__ == "__main__": c1 = Circle(1, 2, 2) c2 = Circle(4, 6, 4) print(c1.insersects_with(c2))
class Circle: (...) def get_shifted_copy(self, dx, dy): if __name__ == "__main__": c1 = Circle(1, 2, 2) print(c1.x, c1.y, c1.r) c1_shifted = c1.get_shifted_copy(5, -4) print(c1.x, c1.y, c1.r) print(c1_shifted.x, c1_shifted.y, c1_shifted.r)
1 2 2 1 2 2 6 -2 2
if __name__ == "__main__": c1 = Circle(1, 2, 2) print(c1)
<__main__.Circle object at 0x0000029E0C649E50>
__str__(self)
class Circle: (...) def __str__(self): return "Circle: x={}, y={}, r={}".format(self.x, self.y, self.r) if __name__ == "__main__": c1 = Circle(1, 2, 2) c2 = Circle(-5, 0, 6) print(c1) print(c2)
Circle: x=1, y=2, r=2 Circle: x=-5, y=0, r=6
Create a class representing a book (catalog card in a library). Your class must have the following structure. Copy-paste the following code and implement the methods:
Required filename: 10_weekly_hw.py.
10_weekly_hw.py
class Book: """represents a book""" def __init__(self, title, author, year): """ :param title: string, book title :param author: string, book author :param year: int, year of publication """ def __str__(self): """ >>> book1 = Book("I, Robot", "Isaac Asimov", 1950) >>> print(book1) Book: I, Robot by Isaac Asimov (1950) >>> book2 = Book("Pride and Prejudice", "Jane Austen", 1813) >>> print(book2) Book: Pride and Prejudice by Jane Austen (1813) """ def has_same_author(self, other): """Compares authors of this and the other book. :param other: Book object :return: boolean, True if self and other have the same author, otherwise False >>> book1 = Book("I, Robot", "Isaac Asimov", 1950) >>> book2 = Book("Pride and Prejudice", "Jane Austen", 1813) >>> book3 = Book("Sense and Sensibility", "Jane Austen", 1811) >>> book1.has_same_author(book3) False >>> book2.has_same_author(book3) True """ if __name__ == "__main__": import doctest doctest.testmod(verbose=True)