From cfaf16adddd8b4a018bbbc523fd275a585e273ae Mon Sep 17 00:00:00 2001 From: IvankaKuzin Date: Mon, 2 Dec 2024 20:48:05 +0200 Subject: [PATCH 1/3] Solution --- app/main.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index f6bcaa1dd..afedd19b5 100644 --- a/app/main.py +++ b/app/main.py @@ -1,8 +1,26 @@ class Person: - # write your code here - pass + people = {} + def __init__(self, name: str, age: int) -> None: + self.name = name + self.age = age -def create_person_list(people: list) -> list: - # write your code here - pass + Person.people[name] = self + + +def create_person_list(people: list[dict]) -> list: + person_list = [] + + for person_data in people: + person = Person(name=person_data.get("name"), + age=person_data.get("age")) + person_list.append(person) + + for person_data in people: + person = Person.people[person_data.get("name")] + if "wife" in person_data and person_data.get("wife"): + person.wife = Person.people[person_data.get("wife")] + if "husband" in person_data and person_data.get("husband"): + person.husband = Person.people[person_data.get("husband")] + + return person_list From e2b87bd067848b28ed960314c29676f28a8de16e Mon Sep 17 00:00:00 2001 From: IvankaKuzin Date: Mon, 2 Dec 2024 22:16:03 +0200 Subject: [PATCH 2/3] Solution2 --- app/main.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/app/main.py b/app/main.py index afedd19b5..ff73a8c67 100644 --- a/app/main.py +++ b/app/main.py @@ -8,19 +8,19 @@ def __init__(self, name: str, age: int) -> None: Person.people[name] = self -def create_person_list(people: list[dict]) -> list: - person_list = [] - - for person_data in people: - person = Person(name=person_data.get("name"), - age=person_data.get("age")) - person_list.append(person) - - for person_data in people: - person = Person.people[person_data.get("name")] - if "wife" in person_data and person_data.get("wife"): - person.wife = Person.people[person_data.get("wife")] - if "husband" in person_data and person_data.get("husband"): - person.husband = Person.people[person_data.get("husband")] - - return person_list +def create_person_list(people: list) -> list: + person_objects = [ + Person(person["name"], person["age"]) + for person in people + ] + for person in people: + person_object = Person.people[person["name"]] + if "wife" in person: + if person["wife"]: + wife = Person.people[person["wife"]] + person_object.wife = wife + elif "husband" in person: + if person["husband"]: + husband = Person.people[person["husband"]] + person_object.husband = husband + return person_objects From 218f81fecbacff9ca024fb2ed8df868696ad62a7 Mon Sep 17 00:00:00 2001 From: IvankaKuzin Date: Wed, 4 Dec 2024 12:47:05 +0200 Subject: [PATCH 3/3] simplified condition by using method.get() --- app/main.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/app/main.py b/app/main.py index ff73a8c67..e5fee1f40 100644 --- a/app/main.py +++ b/app/main.py @@ -14,13 +14,11 @@ def create_person_list(people: list) -> list: for person in people ] for person in people: - person_object = Person.people[person["name"]] - if "wife" in person: - if person["wife"]: - wife = Person.people[person["wife"]] - person_object.wife = wife - elif "husband" in person: - if person["husband"]: - husband = Person.people[person["husband"]] - person_object.husband = husband + person_object = Person.people[person.get("name")] + if person.get("wife"): + wife = Person.people[person.get("wife")] + person_object.wife = wife + elif person.get("husband"): + husband = Person.people[person.get("husband")] + person_object.husband = husband return person_objects