diff --git a/students/dychowicz_monika/lesson_13_objects_and_classes/README.md b/students/dychowicz_monika/lesson_13_objects_and_classes/README.md new file mode 100644 index 000000000..07a701571 --- /dev/null +++ b/students/dychowicz_monika/lesson_13_objects_and_classes/README.md @@ -0,0 +1,14 @@ +### Lesson 13 - Objects and classes +#### introduction +- [Think Python: How to Think Like a Computer Scientist / Chapter 15](http://greenteapress.com/thinkpython2/html/thinkpython2016.html) +- [Python dir() + function](https://www.programiz.com/python-programming/methods/built-in/dir) +- [Python id() function](https://www.programiz.com/python-programming/methods/built-in/id) +- [Python isinstance() function](https://www.programiz.com/python-programming/methods/built-in/isinstance) +- [How To Construct Classes and Define Objects in Python 3](https://www.digitalocean.com/community/tutorials/how-to-construct-classes-and-define-objects-in-python-3) (supplementary materials) +- [Understanding Class and Instance Variables in Python 3](https://www.digitalocean.com/community/tutorials/understanding-class-and-instance-variables-in-python-3) (supplementary materials) +#### practice projects +1. [Think Python: How to Think Like a Computer Scientist / Chapter 15 / Exercise 1 ](http://greenteapress.com/thinkpython2/html/thinkpython2016.html) +1. Object inspector 1 - write a function that for a given object and list of attribute names returns dictionary with names and values of object's attributes. +1. Object inspector 2 - write a function that for a given object returns dictionary with names and values of all object's attributes that are instances of string, integer or float. +1. Selective shallow compare - write a function that for given 2 objects and list of attribute names checks if objects' attributes are equal. \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_13_objects_and_classes/chapter_exercise1.py b/students/dychowicz_monika/lesson_13_objects_and_classes/chapter_exercise1.py new file mode 100644 index 000000000..96463b678 --- /dev/null +++ b/students/dychowicz_monika/lesson_13_objects_and_classes/chapter_exercise1.py @@ -0,0 +1,92 @@ +import math + + +class Point: + def __init__(self, x=0, y=0): + """ Create a new point at the origin """ + self.x = x + self.y = y + + +class Circle: + def __init__(self, center, radius): + self.center = center + self.radius = radius + + +class Rectangle: + def __init__(self, topLeft, topRight, bottomLeft, bottomRight): + self.tL = topLeft + self.tR = topRight + self.bL = bottomLeft + self.bR = bottomRight + +def point_in_circle(point, circle): + distance_between_points = math.sqrt((point.x - circle.center.x)** 2 + + (point.y - circle.center.y)**2) + return distance_between_points <= circle.radius + + +def point_in_circle_bounduary(point, circle): + distance_between_points = math.sqrt((point.x - circle.center.x)** 2 + + (point.y - circle.center.y)**2) + return distance_between_points == circle.radius + + +def rect_circle(rectangle, circle): + check_tL = point_in_circle_bounduary(rectangle.tL, circle) + check_tR = point_in_circle_bounduary(rectangle.tR, circle) + check_bL = point_in_circle_bounduary(rectangle.bL, circle) + check_bR = point_in_circle_bounduary(rectangle.bL, circle) + return check_tL and check_tR and check_bL and check_bR + + +def rect_circle_overlap(rectangle, circle): + check_tL = point_in_circle(rectangle.tL, circle) + check_tR = point_in_circle(rectangle.tR, circle) + check_bL = point_in_circle(rectangle.bL, circle) + check_bR = point_in_circle(rectangle.bL, circle) + return check_tL and check_tR and check_bL and check_bR + + +def main(): + point1 = Point(370,550) + circle_center = Point(150, 100) + my_circle = Circle(circle_center, 75) + print('Is point(%s, %s) in the circle(center=(%s, %s), radious=%s)?' + % (point1.x, point1.y, circle_center.x, circle_center.y, + my_circle.radius)) + print(point_in_circle(point1, my_circle)) + + # rectangle in circle + # rectangle1 = Rectangle(Point(1,10),Point(6,10), Point(1,5), Point(6,5)) + rectangles_points = (Point(-2, 2), Point(2, 2), Point(-2, -2), Point(2, -2)) + rectangle2 = Rectangle(*rectangles_points) + circle_center = Point(0, 0) + my_circle2 = Circle(circle_center, math.sqrt(8)) + print('Is rectangle:((%s,%s),(%s,%s),(%s,%s),(%s,%s)) in the circle' + '(center=(%s, %s),radius=%s) board?' + % (rectangle2.tL.x, rectangle2.tL.y, rectangle2.tR.x, rectangle2.tR.y, + rectangle2.bL.x, rectangle2.bL.y, rectangle2.bL.x, rectangle2.bL.y, + circle_center.x, circle_center.y, my_circle2.radius)) + print(rect_circle(rectangle2, my_circle2)) + + # rectangle in circle overlap + # rectangle1 = Rectangle(Point(1,10),Point(6,10), Point(1,5), Point(6,5)) + rectangles_points = (Point(1, 10), Point(6, 10), Point(1, 5), Point(6, 5)) + rectangle2 = Rectangle(*rectangles_points) + circle_center = Point(2, 3) + my_circle2 = Circle(circle_center, 20) + print('Is rectangle:((%s,%s),(%s,%s),(%s,%s),(%s,%s)) in the circle' + '(center=(%s, %s),radius=%s)?' + % (rectangle2.tL.x, rectangle2.tL.y, rectangle2.tR.x, rectangle2.tR.y, + rectangle2.bL.x, rectangle2.bL.y, rectangle2.bL.x, rectangle2.bL.y, + circle_center.x, circle_center.y, my_circle.radius)) + print(rect_circle_overlap (rectangle2, my_circle2)) + + + +if __name__ == "__main__": + main() + + diff --git a/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector1.py b/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector1.py new file mode 100644 index 000000000..ca7645d0c --- /dev/null +++ b/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector1.py @@ -0,0 +1,31 @@ +class Point: + def __init__(self, x=0, y=0): + """ Create a new point at the origin """ + self.x = x + self.y = y + + +def object_inspector1(instance, keys): + instance_all = instance.__dict__ + selected_keys = {key:instance_all[key] for key in keys} + return(selected_keys) + + +def main(): + ainspector = Point() + ainspector.g = 7 + ainspector.x = 12 + ainspector.b = [77, 88] + ainspector.c = 4 + ainspector.y = {'c':77, 'g':88} + ainspector.s = "sssss" + ainspector.p = 4.564744 + ainspector.q = 5 + ainspector.m = 4.564744 + + a = ['b', 'x', 'y', 'p', 's'] + print(object_inspector1(ainspector, a)) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector2.py b/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector2.py new file mode 100644 index 000000000..7705e98d6 --- /dev/null +++ b/students/dychowicz_monika/lesson_13_objects_and_classes/object_inspector2.py @@ -0,0 +1,33 @@ +class Point: + def __init__(self, x=0, y=0): + """ Create a new point at the origin """ + self.x = x + self.y = y + + +def object_inspector2(instance, keys): + instance_all = instance.__dict__ + selected_keys = {key: instance_all[key] for key in keys + if (isinstance(instance_all[key], int) or + isinstance(instance_all[key], str) or + isinstance(instance_all[key], float))} + return(selected_keys) + + +def main(): + ainspector = Point() + ainspector.g = 7 + ainspector.x = 12 + ainspector.b = [77, 88] + ainspector.c = 4 + ainspector.y = {'c':77, 'g':88} + ainspector.s = "sssss" + ainspector.p = 4.564744 + ainspector.q = 5 + ainspector.m = 4.564744 + + a = ['b', 'x', 'y', 'p', 's'] + print(object_inspector2(ainspector, a)) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_13_objects_and_classes/selective_shallow_compare.py b/students/dychowicz_monika/lesson_13_objects_and_classes/selective_shallow_compare.py new file mode 100644 index 000000000..12cce2972 --- /dev/null +++ b/students/dychowicz_monika/lesson_13_objects_and_classes/selective_shallow_compare.py @@ -0,0 +1,44 @@ +class Point: + def __init__(self, x=0, y=0): + """ Create a new point at the origin """ + self.x = x + self.y = y + + +def compere_objects(instance1, instance2, keys): + instance1_all = instance1.__dict__ + instance2_all = instance2.__dict__ + compare_result = {key: (instance1_all[key] == instance2_all[key]) + for key in keys} + return(compare_result) + + +def main(): + ainspector = Point() + ainspector.g = 7 + ainspector.x = 12 + ainspector.b = [77, 88] + ainspector.c = 4 + ainspector.y = {'c':77, 'g':88} + ainspector.s = "sssss" + ainspector.p = 4.564744 + ainspector.q = 5 + ainspector.m = 4.564744 + + binspector = Point() + binspector.g = 7 + binspector.x = 5 + binspector.b = [22, 88] + binspector.c = 4 + binspector.y = {'c':77, 'g':88} + binspector.s = "ssassss" + binspector.p = 4.564744 + binspector.q = 5 + ainspector.m = 4.564744 + + attributes = ['b', 'x', 'y', 'p', 's'] + print(compere_objects(ainspector, binspector, attributes)) + + +if __name__ == "__main__": + main() \ No newline at end of file