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

wife/husband #1604

Open
wants to merge 4 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
30 changes: 26 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
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 used to store instances of Person by name. This is a good approach for quick lookups.


def __init__(self, name: str, age: int) -> None:

Choose a reason for hiding this comment

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

The Person class constructor should initialize the wife and husband attributes to None to prevent potential AttributeError when these attributes are accessed later. Consider adding self.wife = None and self.husband = None in the constructor.

self.name = name
self.age = age
Person.people[name] = self

Choose a reason for hiding this comment

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

Storing the Person instance in the people dictionary by name is correct. Ensure that names are unique to avoid overwriting instances.



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

Choose a reason for hiding this comment

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

The list comprehension correctly creates Person instances from the input list of dictionaries.


for person in people:
current_person = Person.people.get(person["name"])
wife_name = person.get("wife")
husband_name = person.get("husband")

if wife_name:
spouse = Person.people.get(wife_name)
if spouse:
current_person.wife = spouse

Choose a reason for hiding this comment

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

The code assumes that current_person has a wife attribute, but this attribute is not initialized in the Person class. Consider initializing wife and husband attributes in the Person class constructor.

spouse.husband = current_person

Choose a reason for hiding this comment

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

Similarly, the spouse object assumes a husband attribute. Ensure these attributes are initialized in the Person class to avoid potential AttributeError.


if husband_name:
spouse = Person.people.get(husband_name)
if spouse:
current_person.husband = spouse

Choose a reason for hiding this comment

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

The same issue as above applies here. Ensure that husband and wife attributes are initialized in the Person class.

spouse.wife = current_person

Choose a reason for hiding this comment

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

Again, ensure that wife and husband attributes are initialized in the Person class to avoid potential AttributeError.


return person_list
Loading