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..aff4fcea7 --- /dev/null +++ b/students/swietczak_monika/lesson_13_objects_and_classes/exercise_1.py @@ -0,0 +1,72 @@ +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..ac6c41951 --- /dev/null +++ b/students/swietczak_monika/lesson_13_objects_and_classes/object_inspector.py @@ -0,0 +1,26 @@ +class Books: + def __init__(self, title, author, year): + self.title = title + self.author = author + self.year = 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..11e592d38 --- /dev/null +++ b/students/swietczak_monika/lesson_13_objects_and_classes/selective_shallow_compare.py @@ -0,0 +1,37 @@ +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()