Skip to content
 
 

Latest commit

 

History

36 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Class Person

You have a list of dicts people, every dict means a person, it has keys: name, age, wife/husband - depends on person is male or female. All names are different. Key wife/husband can be either None or name of another person.

  1. Define a Class Person

  2. The __init__ method should take two parameters

    • name: A string representing the name of the person.
    • age: An integer representing the age of the person.
  3. Define a class attribute people in the Person class to store instances by their name.

    • The keys are the name values of instances.
    • The values are references to the Person instances themselves.
    • Within the __init__ method, add each new Person instance to the people dictionary.

Write function create_person_list, this function takes list people and return list with Person instances instead of dicts.

Note:

If person's key wife/husband is not None - create_person_list should add attribute wife/husband respectively to its instance. This attribute should be a link to a Person instance with name the same as wife/husband key in person's dict.

Exemplo:

people = [
    {"name": "Ross", "age": 30, "wife": "Rachel"},
    {"name": "Joey", "age": 29, "wife": None},
    {"name": "Rachel", "age": 28, "husband": "Ross"}
]

person_list = create_person_list(people) 
isinstance(person_list[0], Person) # True
person_list[0].name == "Ross"
person_list[0].wife is person_list[2] # True
person_list[0].wife.name == "Rachel"

person_list[1].name == "Joey"
person_list[1].wife
# AttributeError

isinstance(person_list[2], Person) # True
person_list[2].name == "Rachel"
person_list[2].husband is person_list[0] # True
# The same as person_list[0]
person_list[2].husband.name == "Ross"
person_list[2].husband.wife is person_list[2]  # True

Person.people == {
    "Ross": <__main__.Person object at 0x10c20ca60>,
    "Joey": <__main__.Person object at 0x10c180a00>,
    "Rachel": <__main__.Person object at 0x10c1804f0>
}

class Person: people = {}

def __init__(self, name, age):
    self.name = name
    self.age = age
    Person.people[name] = self

def create_person_list(people): result = [] for person in people: result.append(Person(person['name'], person['age']))

for person, obj in zip(people, result):
    if person.get('wife') is not None:
        obj.wife = Person.people[person['wife']]
    elif person.get('husband') is not None:
        obj.husband = Person.people[person['husband']]

return result

people = [ {"name": "Ross", "age": 30, "wife": "Rachel"}, {"name": "Joey", "age": 29, "wife": None}, {"name": "Rachel", "age": 28, "husband": "Ross"} ]

person_list = create_person_list(people)

teste

print(isinstance(person_list[0], Person)) print(person_list[0].name == "Ross") print(person_list[0].wife is person_list[2]) print(person_list[0].wife.name == "Rachel") print(person_list[1].name == "Joey") person_list[1].wife print("Erro") except AttributeError: print("AttributeError OK") print(person_list[2].husband is person_list[0]) print(person_list[2].husband.name == "Ross") print(person_list[2].husband.wife is person_list[2])

Note: Check your code using this checklist before pushing your solution.

About

mate_academy_tarefa

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages