From 46af3155ffffd9f35b4f4cf8834fe4a88a9a438e Mon Sep 17 00:00:00 2001 From: Ngayka Date: Sun, 1 Dec 2024 16:50:29 +0200 Subject: [PATCH 1/4] solution, but doesn`t work because of wife attribute --- app/main.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index f6bcaa1dd..7dc14e691 100644 --- a/app/main.py +++ b/app/main.py @@ -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.husband = None + self.wife = None + + Person.people[self.name] = self + + def add_husband(self, husband: str) -> None: + self.husband = husband + + def add_wife(self, wife: str) -> None: + self.wife = wife def create_person_list(people: list) -> list: - # write your code here - pass + person_list = [] + + for one in people: + name = one.get("name") + age = one.get("age") + husband = one.get("husband") + wife = one.get("wife") + + person = Person(name=name, age=age) + person_list.append(person) + if husband: + person.add_husband(husband) + if wife: + person.add_wife(wife) + return person_list + From dff7d6fb0f60edc8e681e1b799d5fac91ffcf34a Mon Sep 17 00:00:00 2001 From: Ngayka Date: Sun, 1 Dec 2024 16:54:44 +0200 Subject: [PATCH 2/4] solution with data verification --- app/main.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 7dc14e691..a5cb66bf4 100644 --- a/app/main.py +++ b/app/main.py @@ -25,11 +25,15 @@ def create_person_list(people: list) -> list: husband = one.get("husband") wife = one.get("wife") - person = Person(name=name, age=age) + if name in Person.people: + person = Person.people[name] + else: + person = Person(name=name, age=age) person_list.append(person) - if husband: + if husband and husband != person.name: person.add_husband(husband) - if wife: + + if wife and wife != person.name: person.add_wife(wife) return person_list From bd3cb28576aa787c813a16c4003fe8a15c020581 Mon Sep 17 00:00:00 2001 From: Ngayka Date: Sun, 1 Dec 2024 20:13:11 +0200 Subject: [PATCH 3/4] Can`t find solution because of wifes and husbands --- app/main.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/app/main.py b/app/main.py index a5cb66bf4..c2744194e 100644 --- a/app/main.py +++ b/app/main.py @@ -4,15 +4,13 @@ class Person: def __init__(self, name: str, age: int) -> None: self.name = name self.age = age - self.husband = None - self.wife = None Person.people[self.name] = self - def add_husband(self, husband: str) -> None: + def add_husband(self, husband: "Person") -> None: self.husband = husband - def add_wife(self, wife: str) -> None: + def add_wife(self, wife: "Person") -> None: self.wife = wife @@ -22,18 +20,30 @@ def create_person_list(people: list) -> list: for one in people: name = one.get("name") age = one.get("age") - husband = one.get("husband") - wife = one.get("wife") + husband_name = one.get("husband") + wife_name = one.get("wife") if name in Person.people: person = Person.people[name] else: person = Person(name=name, age=age) - person_list.append(person) - if husband and husband != person.name: + person_list.append(person) + + if husband_name: + if husband_name in Person.people: + husband = Person.people[husband_name] + else: + husband = Person(name=husband_name, age=0) + person_list.append(husband) person.add_husband(husband) - if wife and wife != person.name: + if wife_name: + if wife_name in Person.people: + wife = Person.people[wife_name] + else: + wife = Person(name=wife_name, age=0) + person_list.append(wife) person.add_wife(wife) - return person_list + + return list(Person.people.values()) From b47e5a73a299ad876e38b254dc2058f07b5c67dc Mon Sep 17 00:00:00 2001 From: Ngayka Date: Wed, 11 Dec 2024 19:07:55 +0200 Subject: [PATCH 4/4] find solution, all tests are rigth, but 1 error is --- app/main.py | 54 ++++++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/app/main.py b/app/main.py index c2744194e..4f61852cb 100644 --- a/app/main.py +++ b/app/main.py @@ -4,46 +4,28 @@ class Person: def __init__(self, name: str, age: int) -> None: self.name = name self.age = age + self.wife = None + self.husband = None - Person.people[self.name] = self + Person.people[name] = self - def add_husband(self, husband: "Person") -> None: - self.husband = husband - def add_wife(self, wife: "Person") -> None: - self.wife = wife +def create_person_list(people_list: list) -> list: + persons = [] + for one in people_list: + name = one["name"] + age = one["age"] + person = Person(name, age) + persons.append(person) -def create_person_list(people: list) -> list: - person_list = [] + for one in people_list: + name = one["name"] + person = Person.people[name] - for one in people: - name = one.get("name") - age = one.get("age") - husband_name = one.get("husband") - wife_name = one.get("wife") - - if name in Person.people: - person = Person.people[name] - else: - person = Person(name=name, age=age) - person_list.append(person) - - if husband_name: - if husband_name in Person.people: - husband = Person.people[husband_name] - else: - husband = Person(name=husband_name, age=0) - person_list.append(husband) - person.add_husband(husband) - - if wife_name: - if wife_name in Person.people: - wife = Person.people[wife_name] - else: - wife = Person(name=wife_name, age=0) - person_list.append(wife) - person.add_wife(wife) - - return list(Person.people.values()) + if "wife" in one and one["wife"]: + person.wife = Person.people.get(one["wife"]) + elif "husband" in one and one["husband"]: + person.husband = Person.people.get(one["husband"]) + return persons