1
+ """
2
+ EJERCICIO:
3
+ ¡La Casa del Dragón ha finalizado y no volverá hasta 2026!
4
+ ¿Alguien se entera de todas las relaciones de parentesco
5
+ entre personajes que aparecen en la saga?
6
+ Desarrolla un árbol genealógico para relacionarlos (o invéntalo).
7
+ Requisitos:
8
+ 1. Estará formado por personas con las siguientes propiedades:
9
+ - Identificador único (obligatorio)
10
+ - Nombre (obligatorio)
11
+ - Pareja (opcional)
12
+ - Hijos (opcional)
13
+ 2. Una persona sólo puede tener una pareja (para simplificarlo).
14
+ 3. Las relaciones deben validarse dentro de lo posible.
15
+ Ejemplo: Un hijo no puede tener tres padres.
16
+ Acciones:
17
+ 1. Crea un programa que permita crear y modificar el árbol.
18
+ - Añadir y eliminar personas
19
+ - Modificar pareja e hijos
20
+ 2. Podrás imprimir el árbol (de la manera que consideres).
21
+
22
+ NOTA: Ten en cuenta que la complejidad puede ser alta si
23
+ se implementan todas las posibles relaciones. Intenta marcar
24
+ tus propias reglas y límites para que te resulte asumible.
25
+
26
+ by adra-dev
27
+ """
28
+
29
+ class Person :
30
+
31
+ def __init__ (self , id : int , name : str ):
32
+ self .id = id
33
+ self .name = name
34
+ self .partner = None
35
+ self .children = []
36
+ self .has_parents = False
37
+
38
+
39
+ def add_partner (self , partner ):
40
+ if self .partner is not None :
41
+ print (f"{ self .name } ya tiene una pareja: { self .partner .name } ." )
42
+ else :
43
+ self .partner = partner
44
+ partner .partner = self
45
+ print (f"{ self .name } es pareja de { partner .name } " )
46
+
47
+ def add_child (self , child ):
48
+ if child not in self .children :
49
+ self .children .append (child )
50
+ print (f"{ self .name } ha tenido un hijo: { child .name } " )
51
+ else :
52
+ print (f"{ child .name } ya es hijo de { self .name } " )
53
+
54
+
55
+ class FamilyTree :
56
+
57
+ def __init__ (self ):
58
+ self .people = {}
59
+
60
+ def add_person (self , id , name ):
61
+ if id in self .people :
62
+ print (f"La persona con ID:{ id } ya existe." )
63
+ else :
64
+ person = Person (id , name )
65
+ self .people [id ] = person
66
+ print (f"La perosna con nombre { name } [ID: { id } ] ha sido agregada al arbol." )
67
+
68
+ def remove_person (self , id ):
69
+ if id in self .people :
70
+ person = self .people [id ]
71
+ del self .people [id ]
72
+ print (f"La persona con nombre { person .name } [ID: { id } ] ha sido eliminada del arbol." )
73
+ else :
74
+ print (f"La persona con [ID: { id } ] no existe en el arbol." )
75
+
76
+ def set_partner (self , id1 , id2 ):
77
+ if id1 in self .people and id2 in self .people :
78
+ person1 = self .people [id1 ]
79
+ person2 = self .people [id2 ]
80
+ person1 .add_partner (person2 )
81
+ else :
82
+ print ("Algun ID no existe en el arbol." )
83
+
84
+ def add_child (self , parent_id , child_id ):
85
+ if parent_id in self .people and child_id in self .people :
86
+ if parent_id == child_id :
87
+ print ("Los ID no pueden ser iguales a la hora de asignar un hijo." )
88
+ else :
89
+ parent = self .people [parent_id ]
90
+ if parent .partner is None :
91
+ print (f"Se necesita una pareja para poder tener un hijo." )
92
+ else :
93
+ child = self .people [child_id ]
94
+ if child .has_parents :
95
+ print (f"{ child .name } [ID: { id } ] ya tiene padres" )
96
+ else :
97
+ child .has_parents = True
98
+ parent .add_child (child )
99
+ parent .partner .add_child (child )
100
+ else :
101
+ print ("Algun ID no existe en el arbol." )
102
+
103
+ def pint_tree (self ):
104
+
105
+ visited = set ()
106
+
107
+ def print_person (person , level = 0 ):
108
+
109
+ if person .id in visited :
110
+ return
111
+
112
+ visited .add (person .id )
113
+
114
+ indent = "\t " * level
115
+
116
+ print (f"{ indent } - { person .name } [ID: { person .id } ]" )
117
+
118
+ if person .partner :
119
+ visited .add (person .partner .id )
120
+ print (
121
+ f"{ indent } Pareja: { person .partner .name } [ID: { person .partner .id } ]" )
122
+
123
+ if person .children :
124
+ print (f"{ indent } Hijos:" )
125
+ for child in person .children :
126
+ print_person (child , level + 1 )
127
+
128
+
129
+ for person in self .people .values ():
130
+ is_child = person .has_parents
131
+ if not is_child :
132
+ print_person (person )
133
+
134
+
135
+ tree = FamilyTree ()
136
+
137
+ tree .add_person (1 , "Jocelyn" )
138
+ tree .add_person (2 , "Aemon" )
139
+
140
+ tree .set_partner (1 , 2 )
141
+
142
+ tree .add_person (3 , "Rhaenys" )
143
+
144
+ tree .add_child (1 , 3 )
145
+
146
+ tree .add_person (4 , "Corlys" )
147
+
148
+ tree .set_partner (3 , 4 )
149
+
150
+ tree .add_person (5 , "Laena" )
151
+ tree .add_person (6 , "Laenor" )
152
+
153
+ tree .add_child (3 , 5 )
154
+ tree .add_child (3 , 6 )
155
+
156
+ tree .add_person (7 , "Baelon" )
157
+ tree .add_person (8 , "Alyssa" )
158
+
159
+ tree .set_partner (7 ,8 )
160
+
161
+
162
+ tree .add_person (9 , "Viserys I" )
163
+ tree .add_person (10 , "Daemon" )
164
+
165
+ tree .add_child (7 , 9 )
166
+ tree .add_child (8 , 10 )
167
+
168
+ tree .add_person (11 , "Aemma" )
169
+
170
+ tree .set_partner (9 , 11 )
171
+
172
+ tree .add_person (12 , "Rhaenyra" )
173
+
174
+ tree .add_child (9 , 12 )
175
+
176
+ tree .set_partner (10 , 12 )
177
+
178
+ tree .add_person (13 , "Aegon" )
179
+ tree .add_person (14 , "Viserys" )
180
+
181
+ tree .add_child (12 , 13 )
182
+ tree .add_child (12 , 14 )
183
+
184
+ tree .pint_tree ()
0 commit comments