Skip to content
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

implement function "create_person_list" for py-person-class #1659

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
39 changes: 35 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
class Person:
# write your code here
pass
people = {}
ihetay marked this conversation as resolved.
Show resolved Hide resolved

def __init__(self, name: str, age: int) -> None:
self.name = name
self.age = age
Person.people[self.name] = self

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line adds the Person instance to the people dictionary using the person's name as the key. Ensure that the name is unique, or previous entries will be overwritten.



def create_person_list(people: list) -> list:
# write your code here
pass
"""
Creates a list of Person objects based on a list of dictionaries.

:param people: A list of dictionaries, where each dictionary
represents a person with the following keys:
- "name" (str): The name of the person.
- "age" (int): The age of the person.
- "wife" (str | None): The name of the wife, if any.
- "husband" (str | None): The name of the husband, if any.
:return: A list of Person objects, where each dictionary is converted
into a corresponding Person object.
If "wife" or "husband" is specified, links to the corresponding
Person objects are established.
"""
person_list = []
# creating person instances and adding to person_list
for person in people:
person_list.append(Person(name=person["name"], age=person["age"]))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The person_list is being populated with Person instances, but this list is not used later in the function. Consider returning this list or using it in some way.

# checking for the presence of the key wife or husband and if found,
# assign the value wife or husband to the instance
Comment on lines +29 to +30

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to add such comments in the future since the code is self-descriptive enough

for person in people:
current_person = Person.people[person["name"]]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accessing Person.people[person['name']] assumes that the person already exists in the dictionary. Ensure that all names in the input list are unique and have been added to the dictionary.


if wife_name := person.get("wife"):
current_person.wife = Person.people[wife_name]

if husband_name := person.get("husband"):
current_person.husband = Person.people[husband_name]
return person_list

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The person_list is returned, but it is not clear how it is used. Ensure that the function's return value is utilized effectively in the application.

Loading