-
Notifications
You must be signed in to change notification settings - Fork 72
Lesson 13 solutions #694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Lesson 13 solutions #694
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| class Point: | ||
| def __init__(self, x, y): | ||
| self.x = x | ||
| self.y = y | ||
|
|
||
| def __str__(self): | ||
| return "x = {}, y = {}".format(self.x, self.y) | ||
|
|
||
|
|
||
| class Circle: | ||
| def __init__(self, center, radius): | ||
| self.center = center | ||
| self.radius = radius | ||
|
|
||
|
|
||
| class Rectangle: | ||
| def __init__(self, width, height, corner): | ||
| self.width = width | ||
| self.height = height | ||
| self.corner = corner | ||
|
|
||
|
|
||
| def point_in_circle(point, circle): | ||
| if (point.x - circle.center.x) * (point.x - circle.center.x) + (point.y - circle.center.y) * ( | ||
| point.y - circle.center.y) <= circle.radius * circle.radius: | ||
| return True | ||
| else: | ||
| return False | ||
|
|
||
|
|
||
| def rectangle_points(rect): | ||
| left_down = rect.corner | ||
| left_up = Point(left_down.x, left_down.y + rect.height) | ||
| right_down = Point(left_down.x + rect.width, left_down.y) | ||
| right_up = Point(left_down.x + rect.width, left_down.y + rect.height) | ||
| result = [left_down, left_up, right_up, right_down] | ||
| return result | ||
|
|
||
|
|
||
| def rect_in_circle(rectangle, circle): | ||
| points = rectangle_points(rectangle) | ||
| for point in points: | ||
| if not point_in_circle(point, circle): | ||
| return False | ||
| else: | ||
| return True | ||
|
|
||
|
|
||
| def rect_circle_overlap(rectangle, circle): | ||
| points = rectangle_points(rectangle) | ||
| for point in points: | ||
| if point_in_circle(point, circle): | ||
| return True | ||
| else: | ||
| return False | ||
|
|
||
|
|
||
| def main(): | ||
| circle = Circle(Point(150, 100), 75) | ||
| point = Point(50, 100) | ||
| rectangle = Rectangle(20, 10, Point(120, 100)) | ||
| print("Point:" + str(point)) | ||
| print("Is point inside the circle? " + str(point_in_circle(point, circle))) | ||
| print("Is rectangle inside the circle? " + str(rect_in_circle(rectangle, circle))) | ||
|
||
| print("Is rectangle overlapping the circle? " + str(rect_circle_overlap(rectangle, circle))) | ||
|
||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| class Books: | ||
| def __init__(self, title, author, year): | ||
| self.title = title | ||
| self.author = author | ||
| self.year = year | ||
|
|
||
|
|
||
| # def __dir__(self): | ||
| # return [self.author, self.title, self.year] | ||
|
|
||
|
|
||
| def object_inspector1(some_object): | ||
| attributes = dir(some_object) | ||
| return dict((key, getattr(some_object, key)) for key in attributes) | ||
|
|
||
|
|
||
| def object_inspector2(some_object): | ||
| attributes = dir(some_object) | ||
| return dict((key, getattr(some_object, key)) for key in attributes | ||
| if isinstance(getattr(some_object, key), (int, float, str))) | ||
|
|
||
|
|
||
| def main(): | ||
| ksiazka = Books("Hasztag", "Remigiusz Mroz", 2018) | ||
| print(object_inspector1(ksiazka)) | ||
| print(object_inspector2(ksiazka)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| class Books: | ||
| def __init__(self, title, author, year): | ||
| self.title = title | ||
| self.author = author | ||
| self.year = year | ||
|
|
||
|
|
||
| def shallow_compare(object1, object2, attributes): | ||
| for attribute in attributes: | ||
| if getattr(object1, attribute) != getattr(object2, attribute): | ||
| return False | ||
| else: | ||
| return True | ||
|
|
||
|
|
||
| def main(): | ||
| book1 = Books("Uwiklanie", "Zygmunt Miloszewski", 2007) | ||
| book2 = Books("Uwiklanie", "Zygmunt Miloszewski", 2007) | ||
| book3 = Books("Hasztag", "Remigiusz Mroz", 2018) | ||
| print("Book1: author: {}, title: {}, year: {}".format(book1.author, book1.title, book1.year)) | ||
|
||
| print("Book2: author: {}, title: {}, year: {}".format(book2.author, book2.title, book2.year)) | ||
|
||
| print("Book3: author: {}, title: {}, year: {}".format(book3.author, book3.title, book3.year)) | ||
|
||
| attributes_list = ['title', 'author', 'year'] | ||
| print("Are book 1 and book 2 equal? " + str(shallow_compare(book1, book2, attributes_list))) | ||
|
||
| print("Are book 1 and book 3 equal? " + str(shallow_compare(book1, book3, attributes_list))) | ||
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
E501 line too long (98 > 79 characters)