From b42261578fc675fd95f0ae2c77a629e9d21ecd73 Mon Sep 17 00:00:00 2001 From: Olga Dolnyk Date: Wed, 18 Oct 2023 17:26:07 +0300 Subject: [PATCH 1/3] 'Solution' --- app/main.py | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index f6bcaa1dd..f09f1912a 100644 --- a/app/main.py +++ b/app/main.py @@ -1,8 +1,37 @@ +from typing import List, Optional + + class Person: - # write your code here - pass + people = {} + + def __init__(self, name: str, + age: int, + spouse: Optional["Person"] = None) -> None: + self.name = name + self.age = age + self.spouse = spouse + Person.people[name] = self + + +def create_person_list(people_data: List[dict]) -> List[Person]: + person_list = [] + + for person_data in people_data: + name: str = person_data["name"] + age: int = person_data["age"] + spouse_name: Optional[str] = \ + person_data.get("wife") or person_data.get("husband") + + spouse: Optional[Person] = Person.people.get(spouse_name) \ + if spouse_name else None + + person = Person(name, age, spouse) + person_list.append(person) + if spouse: + if "wife" in person_data: + person.spouse = spouse + else: + person.husband = spouse -def create_person_list(people: list) -> list: - # write your code here - pass + return person_list From 7c7c12fced390847fbfd19ea14b378693d05f5ad Mon Sep 17 00:00:00 2001 From: Olga Dolnyk Date: Fri, 20 Oct 2023 13:59:36 +0300 Subject: [PATCH 2/3] 'Solution' --- app/main.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/main.py b/app/main.py index f09f1912a..8278f3aa8 100644 --- a/app/main.py +++ b/app/main.py @@ -1,4 +1,4 @@ -from typing import List, Optional +from typing import Optional, List class Person: @@ -11,6 +11,8 @@ def __init__(self, name: str, self.age = age self.spouse = spouse Person.people[name] = self + self.wife = None + self.husband = None def create_person_list(people_data: List[dict]) -> List[Person]: @@ -19,18 +21,18 @@ def create_person_list(people_data: List[dict]) -> List[Person]: for person_data in people_data: name: str = person_data["name"] age: int = person_data["age"] - spouse_name: Optional[str] = \ - person_data.get("wife") or person_data.get("husband") + spouse_name: Optional[str] = (person_data.get("wife") + or person_data.get("husband")) - spouse: Optional[Person] = Person.people.get(spouse_name) \ - if spouse_name else None + spouse: Optional[Person] = (Person.people.get(spouse_name) + if spouse_name else None) person = Person(name, age, spouse) person_list.append(person) if spouse: if "wife" in person_data: - person.spouse = spouse + person.wife = spouse else: person.husband = spouse From a94f9c4a6ad432afc8a46cb94bf8756f21b82710 Mon Sep 17 00:00:00 2001 From: Olga Dolnyk Date: Mon, 23 Oct 2023 17:10:22 +0300 Subject: [PATCH 3/3] 'Solution' --- app/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 8278f3aa8..b6a8ac995 100644 --- a/app/main.py +++ b/app/main.py @@ -33,7 +33,9 @@ def create_person_list(people_data: List[dict]) -> List[Person]: if spouse: if "wife" in person_data: person.wife = spouse - else: + spouse.husband = person + elif "husband" in person_data: person.husband = spouse + spouse.wife = person return person_list