-
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 #1634
base: master
Are you sure you want to change the base?
Solution #1634
Changes from all commits
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,35 @@ | ||
class Person: | ||
# write your code here | ||
pass | ||
people = {} | ||
|
||
def __init__(self, name: str, age: int) -> None: | ||
self.name = name | ||
self.age = age | ||
self.wife = None | ||
self.husband = None | ||
Person.people[name] = self | ||
|
||
def __repr__(self) -> str: | ||
return f"Person(name={self.name}, age={self.age})" | ||
|
||
|
||
def create_person_list(people: list) -> list: | ||
# write your code here | ||
pass | ||
persons = [] | ||
|
||
for data in people: | ||
person = Person(data["name"], data["age"]) | ||
persons.append(person) | ||
|
||
for data in people: | ||
person = Person.people[data["name"]] | ||
|
||
if "wife" in data and data["wife"] is not None: | ||
wife_name = data["wife"] | ||
if wife_name in Person.people: | ||
person.wife = Person.people[wife_name] | ||
Comment on lines
+25
to
+28
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. The code assumes that the |
||
|
||
if "husband" in data and data["husband"] is not None: | ||
husband_name = data["husband"] | ||
if husband_name in Person.people: | ||
person.husband = Person.people[husband_name] | ||
Comment on lines
+30
to
+33
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. Similar to the |
||
|
||
return persons |
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.
Using a class variable
people
to store instances ofPerson
by name assumes that names are unique. If two people have the same name, the latter will overwrite the former in the dictionary. Consider using a unique identifier for each person.