Skip to content
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

'Solution' #1626

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
31 changes: 27 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
class Person:
# write your code here
pass
people = {}

Choose a reason for hiding this comment

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

The people dictionary is correctly defined as a class attribute, but ensure that it is used consistently throughout the class to store instances by their name as required by the task description.

Choose a reason for hiding this comment

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

The people dictionary is a class variable, meaning it is shared across all instances of the Person class. This can lead to unexpected behavior if multiple Person objects are created, as they will all share the same people dictionary. Consider using an instance variable instead.


def __init__(self, name: str, age: int) -> None:
self.name = name
self.age = age
self.people[self.name] = self


def create_person_list(people: list) -> list:
# write your code here
pass
our_list = []
for human in people:
person = Person(human["name"], human["age"])

Choose a reason for hiding this comment

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

The Person class constructor is being called with human["name"] and human["age"]. Ensure that these keys are always present in the human dictionary to avoid potential KeyError exceptions. You might want to handle cases where these keys are missing.

if human.get("husband"):
person.husband = human["husband"]

Choose a reason for hiding this comment

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

Instead of directly assigning human["husband"] to person.husband, you could optimize this by checking if the husband is already in our_list and directly assigning the Person object if it exists.

if human.get("wife"):
person.wife = human["wife"]

Choose a reason for hiding this comment

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

Similar to the previous comment, consider optimizing the assignment of human["wife"] to person.wife by checking if the wife is already in our_list and directly assigning the Person object if it exists.

our_list.append(person)

for item in our_list:
if hasattr(item, "wife"):
for i in our_list:
if i.name == item.wife:
item.wife = i

if hasattr(item, "husband"):
for i in our_list:
if i.name == item.husband:
item.husband = i

return our_list
Loading