Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions students/grzegorz_bajorski/lesson_13_bjects_and_classes/Exercise_1.py
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:
Copy link
Contributor

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

"""Represents a point in 2-D space.

attributes: x, y
"""


class Rectangle:
Copy link
Contributor

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

"""Represents a rectangle.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

W291 trailing whitespace


attributes: width, height, corner.
"""


def print_point(p):
Copy link
Contributor

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

"""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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class Person:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary

age = 22
name = "aa"
salary = 1


person = Person()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

person = Person('aa', 22, 1)
person2 = Person('bb', 24, 1)

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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add constructor to this class:

class Person:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary

age = 22
name = "aa"
salary = 1


person = Person()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

person = Person('aa', 22, 1)
person2 = Person('bb', 24, 1)

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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add constructor to this class:

class Person:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary

age = 22
name = "aa"
salary = 1


person = Person()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

person = Person('aa', 22, 1)
person2 = Person('bb', 24, 1)

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 :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E203 whitespace before ':'

print("Compared objects are not equal")