-
Notifications
You must be signed in to change notification settings - Fork 72
Lesson 13 #707
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 #707
Changes from 2 commits
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,143 @@ | ||
| from __future__ import print_function, division | ||
|
|
||
| import copy | ||
| import math | ||
|
|
||
|
|
||
| def distance_between_points(p1, p2): | ||
| """Computes the distance between two Point objects. | ||
|
|
||
| p1: Point | ||
| p2: Point | ||
|
|
||
| returns: float | ||
| """ | ||
| dx = p1.x - p2.x | ||
| dy = p1.y - p2.y | ||
| dist = math.sqrt(dx**2 + dy**2) | ||
| return dist | ||
|
|
||
|
|
||
| class Point: | ||
| """Represents a point in 2-D space. | ||
|
|
||
| attributes: x, y | ||
| """ | ||
|
|
||
|
|
||
| class Rectangle: | ||
|
||
| """Represents a rectangle. | ||
|
||
|
|
||
| attributes: width, height, corner. | ||
| """ | ||
|
|
||
|
|
||
| def print_point(p): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E302 expected 2 blank lines, found 1 |
||
| """Print a Point object in human-readable format.""" | ||
| print('(%g, %g)' % (p.x, p.y)) | ||
|
|
||
|
|
||
| class Circle: | ||
| """Represents a circle. | ||
|
|
||
| Attributes: center, radius | ||
| """ | ||
|
|
||
|
|
||
| def point_in_circle(point, circle): | ||
| """Checks whether a point lies inside a circle (or on the boundary). | ||
|
|
||
| point: Point object | ||
| circle: Circle object | ||
| """ | ||
| d = distance_between_points(point, circle.center) | ||
| print(d) | ||
| return d <= circle.radius | ||
|
|
||
|
|
||
| def rect_in_circle(rect, circle): | ||
| """Checks whether the corners of a rect fall in/on a circle. | ||
|
|
||
| rect: Rectangle object | ||
| circle: Circle object | ||
| """ | ||
| p = copy.copy(rect.corner) | ||
| print_point(p) | ||
| if not point_in_circle(p, circle): | ||
| return False | ||
|
|
||
| p.x += rect.width | ||
| print_point(p) | ||
| if not point_in_circle(p, circle): | ||
| return False | ||
|
|
||
| p.y -= rect.height | ||
| print_point(p) | ||
| if not point_in_circle(p, circle): | ||
| return False | ||
|
|
||
| p.x -= rect.width | ||
| print_point(p) | ||
| if not point_in_circle(p, circle): | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def rect_circle_overlap(rect, circle): | ||
| """Checks whether any corners of a rect fall in/on a circle. | ||
|
|
||
| rect: Rectangle object | ||
| circle: Circle object | ||
| """ | ||
| p = copy.copy(rect.corner) | ||
| print_point(p) | ||
| if point_in_circle(p, circle): | ||
| return True | ||
|
|
||
| p.x += rect.width | ||
| print_point(p) | ||
| if point_in_circle(p, circle): | ||
| return True | ||
|
|
||
| p.y -= rect.height | ||
| print_point(p) | ||
| if point_in_circle(p, circle): | ||
| return True | ||
|
|
||
| p.x -= rect.width | ||
| print_point(p) | ||
| if point_in_circle(p, circle): | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
|
|
||
| def main(): | ||
| box = Rectangle() | ||
| box.width = 100.0 | ||
| box.height = 200.0 | ||
| box.corner = Point() | ||
| box.corner.x = 50.0 | ||
| box.corner.y = 50.0 | ||
|
|
||
| print(box.corner.x) | ||
| print(box.corner.y) | ||
|
|
||
| circle = Circle | ||
| circle.center = Point() | ||
| circle.center.x = 150.0 | ||
| circle.center.y = 100.0 | ||
| circle.radius = 75.0 | ||
|
|
||
| print(circle.center.x) | ||
| print(circle.center.y) | ||
| print(circle.radius) | ||
|
|
||
| print(point_in_circle(box.corner, circle)) | ||
| print(rect_in_circle(box, circle)) | ||
| print(rect_circle_overlap(box, circle)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| def object_inspector(obj, attributes): | ||
| attr_dict = {} | ||
| for attribute in attributes: | ||
| value = getattr(obj, attribute) | ||
| attr_dict[attribute] = value | ||
| return attr_dict | ||
|
|
||
|
|
||
| class Person: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| age = 22 | ||
| name = "aa" | ||
| salary = 1 | ||
|
|
||
|
|
||
| person = Person() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| attributes = ['name', 'age'] | ||
| att_dict = object_inspector(person, attributes) | ||
| print(att_dict) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| def object_inspector(objects): | ||
| attr_dict = {} | ||
| for attribute in dir(objects): | ||
| value = getattr(objects, attribute) | ||
| if isinstance(value, (str, int, float)): | ||
| attr_dict[attribute] = value | ||
| return attr_dict | ||
|
|
||
|
|
||
| class Person: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add constructor to this class: |
||
| age = 22 | ||
| name = "aa" | ||
| salary = 1 | ||
|
|
||
|
|
||
| person = Person() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| person2 = Person() | ||
| person2.age = 30 | ||
| person2.name = "bb" | ||
|
|
||
| print(object_inspector(person)) | ||
| print(object_inspector(person2)) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| def compare_attributes(obj1, obj2, attributes): | ||
| for attribute in attributes: | ||
| if getattr(obj1, attribute) != getattr(obj2, attribute): | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| class Person: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add constructor to this class: |
||
| age = 22 | ||
| name = "aa" | ||
| salary = 1 | ||
|
|
||
|
|
||
| person = Person() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| person2 = Person() | ||
| person2.age = 24 | ||
| person2.name = "bb" | ||
| person2.salary = "1" | ||
|
|
||
| attributes = ['age', 'name', 'salary'] | ||
|
|
||
| if compare_attributes(person, person2, attributes): | ||
| print("Compared objects are equal") | ||
| else : | ||
|
||
| print("Compared objects are not equal") | ||
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.
E302 expected 2 blank lines, found 1