From 2c64352cbc9492ffe155ab247de809d72c53cc7e Mon Sep 17 00:00:00 2001 From: Monika S Date: Sun, 22 Jul 2018 15:11:21 +0200 Subject: [PATCH 1/2] Lesson 13 solutions --- .../exercise_1.py | 69 +++++++++++++++++++ .../object_inspector.py | 30 ++++++++ .../selective_shallow_compare.py | 29 ++++++++ 3 files changed, 128 insertions(+) create mode 100644 students/swietczak_monika/lesson_13_objects_and_classes/exercise_1.py create mode 100644 students/swietczak_monika/lesson_13_objects_and_classes/object_inspector.py create mode 100644 students/swietczak_monika/lesson_13_objects_and_classes/selective_shallow_compare.py diff --git a/students/swietczak_monika/lesson_13_objects_and_classes/exercise_1.py b/students/swietczak_monika/lesson_13_objects_and_classes/exercise_1.py new file mode 100644 index 000000000..a7c886e44 --- /dev/null +++ b/students/swietczak_monika/lesson_13_objects_and_classes/exercise_1.py @@ -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() diff --git a/students/swietczak_monika/lesson_13_objects_and_classes/object_inspector.py b/students/swietczak_monika/lesson_13_objects_and_classes/object_inspector.py new file mode 100644 index 000000000..ab549617f --- /dev/null +++ b/students/swietczak_monika/lesson_13_objects_and_classes/object_inspector.py @@ -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() diff --git a/students/swietczak_monika/lesson_13_objects_and_classes/selective_shallow_compare.py b/students/swietczak_monika/lesson_13_objects_and_classes/selective_shallow_compare.py new file mode 100644 index 000000000..9f857b7bd --- /dev/null +++ b/students/swietczak_monika/lesson_13_objects_and_classes/selective_shallow_compare.py @@ -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() From d6e65624a5f6d5deafb4a98df0cb1f7b7f873425 Mon Sep 17 00:00:00 2001 From: Monika S Date: Sun, 22 Jul 2018 15:17:21 +0200 Subject: [PATCH 2/2] Just minor formatting changes --- .../exercise_1.py | 9 ++++++--- .../object_inspector.py | 4 ---- .../selective_shallow_compare.py | 18 +++++++++++++----- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/students/swietczak_monika/lesson_13_objects_and_classes/exercise_1.py b/students/swietczak_monika/lesson_13_objects_and_classes/exercise_1.py index a7c886e44..aff4fcea7 100644 --- a/students/swietczak_monika/lesson_13_objects_and_classes/exercise_1.py +++ b/students/swietczak_monika/lesson_13_objects_and_classes/exercise_1.py @@ -21,7 +21,8 @@ def __init__(self, width, height, corner): def point_in_circle(point, circle): - if (point.x - circle.center.x) * (point.x - circle.center.x) + (point.y - circle.center.y) * ( + 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: @@ -61,8 +62,10 @@ def main(): 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))) + 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__': diff --git a/students/swietczak_monika/lesson_13_objects_and_classes/object_inspector.py b/students/swietczak_monika/lesson_13_objects_and_classes/object_inspector.py index ab549617f..ac6c41951 100644 --- a/students/swietczak_monika/lesson_13_objects_and_classes/object_inspector.py +++ b/students/swietczak_monika/lesson_13_objects_and_classes/object_inspector.py @@ -5,10 +5,6 @@ def __init__(self, title, author, year): 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) diff --git a/students/swietczak_monika/lesson_13_objects_and_classes/selective_shallow_compare.py b/students/swietczak_monika/lesson_13_objects_and_classes/selective_shallow_compare.py index 9f857b7bd..11e592d38 100644 --- a/students/swietczak_monika/lesson_13_objects_and_classes/selective_shallow_compare.py +++ b/students/swietczak_monika/lesson_13_objects_and_classes/selective_shallow_compare.py @@ -17,12 +17,20 @@ 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)) + 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))) + 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__":