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 #772

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

def __init__(self, name: str, age: int) -> None:
self.name = name
self.age = age
self.spouse = None
self.add_to_people()

Choose a reason for hiding this comment

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

Create class Person. It's __init__ method takes and store name, age of a person. This class also should have a class attribute people, it is a dict that stores Person instances by their name -- only 3 attributes according to the task description


def create_person_list(people: list) -> list:
# write your code here
pass
def add_to_people(self) -> None:
Person.people[self.name] = self
Copy link

Choose a reason for hiding this comment

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

maybe it's better to add person to people dictionary inside init method to make sure person is only added to dictionary when new instance is created.

Choose a reason for hiding this comment

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

You can do this directrly in the init



def create_person_list(people: list[dict]) -> list:
people_list = []

for person_info in people:
name = person_info["name"]
age = person_info["age"]
person = Person(name, age)
people_list.append(person)
Copy link

Choose a reason for hiding this comment

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

maybe you can try doing it with list comprehensions

Choose a reason for hiding this comment

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

Try to rewrite in one line. Hint: list comprehension 🤫


for index, person_info in enumerate(people):

Choose a reason for hiding this comment

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

Use just dict.get() method in the condition, instead of this long one

if "wife" in person_info and person_info["wife"]:
wife_name = person_info["wife"]
if wife_name in Person.people:

Choose a reason for hiding this comment

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

This check does nothing here, you can remove it

people_list[index].wife = Person.people[wife_name]

if "husband" in person_info and person_info["husband"]:
husband_name = person_info["husband"]
if husband_name in Person.people:
people_list[index].husband = Person.people[husband_name]

return people_list
Loading