-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: master
Are you sure you want to change the base?
Solution #772
Changes from 1 commit
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 |
---|---|---|
@@ -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() | ||
|
||
def create_person_list(people: list) -> list: | ||
# write your code here | ||
pass | ||
def add_to_people(self) -> None: | ||
Person.people[self.name] = self | ||
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. 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. 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. 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) | ||
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. maybe you can try doing it with list comprehensions 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. Try to rewrite in one line. Hint: list comprehension 🤫 |
||
|
||
for index, person_info in enumerate(people): | ||
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. 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: | ||
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. 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 |
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.
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