From dda86c3fc4242146d34ea08cf3498c81b2b18091 Mon Sep 17 00:00:00 2001 From: Olena Bukhova Date: Wed, 4 Dec 2024 12:29:12 +0100 Subject: [PATCH 1/3] Solution --- app/main.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index f6bcaa1dd..ee6cc3159 100644 --- a/app/main.py +++ b/app/main.py @@ -1,8 +1,22 @@ class Person: - # write your code here - pass + people = {} + + def __init__(self, name: str, age: int) -> None: + self.name = name + self.age = age + Person.people[name] = self def create_person_list(people: list) -> list: - # write your code here - pass + people_list = [] + for person_data in people: + people_list.append(Person(person_data["name"], person_data["age"])) + + for person_data in people: + person = Person.people[person_data["name"]] + if "wife" in person_data and person_data["wife"]: + person.wife = Person.people[person_data["wife"]] + elif "husband" in person_data and person_data["husband"]: + person.husband = Person.people[person_data["husband"]] + + return people_list From 5cc16561d26ac3f2bf0cb32257a85f2da7d9e5f1 Mon Sep 17 00:00:00 2001 From: Olena Bukhova Date: Wed, 4 Dec 2024 12:42:05 +0100 Subject: [PATCH 2/3] add get() and list comprehension --- app/main.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index ee6cc3159..eb80b6de8 100644 --- a/app/main.py +++ b/app/main.py @@ -8,15 +8,14 @@ def __init__(self, name: str, age: int) -> None: def create_person_list(people: list) -> list: - people_list = [] - for person_data in people: - people_list.append(Person(person_data["name"], person_data["age"])) + people_list = [Person(person_data["name"], person_data["age"]) + for person_data in people] for person_data in people: - person = Person.people[person_data["name"]] + person = Person.people.get(person_data["name"]) if "wife" in person_data and person_data["wife"]: - person.wife = Person.people[person_data["wife"]] + person.wife = Person.people.get(person_data["wife"]) elif "husband" in person_data and person_data["husband"]: - person.husband = Person.people[person_data["husband"]] + person.husband = Person.people.get(person_data["husband"]) return people_list From 4074d7e766473e71c770535cacf083e1e74fa3d9 Mon Sep 17 00:00:00 2001 From: Olena Bukhova Date: Wed, 4 Dec 2024 12:46:29 +0100 Subject: [PATCH 3/3] improved the code by adding more get() --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index eb80b6de8..b4491ecfe 100644 --- a/app/main.py +++ b/app/main.py @@ -13,9 +13,9 @@ def create_person_list(people: list) -> list: for person_data in people: person = Person.people.get(person_data["name"]) - if "wife" in person_data and person_data["wife"]: + if person_data.get("wife") and person_data["wife"]: person.wife = Person.people.get(person_data["wife"]) - elif "husband" in person_data and person_data["husband"]: + elif person_data.get("husband") and person_data["husband"]: person.husband = Person.people.get(person_data["husband"]) return people_list