1
+ """
2
+ /*
3
+ * EJERCICIO:
4
+ * ¡La Casa del Dragón ha finalizado y no volverá hasta 2026!
5
+ * ¿Alguien se entera de todas las relaciones de parentesco
6
+ * entre personajes que aparecen en la saga?
7
+ * Desarrolla un árbol genealógico para relacionarlos (o invéntalo).
8
+ * Requisitos:
9
+ * 1. Estará formado por personas con las siguientes propiedades:
10
+ * - Identificador único (obligatorio)
11
+ * - Nombre (obligatorio)
12
+ * - Pareja (opcional)
13
+ * - Hijos (opcional)
14
+ * 2. Una persona sólo puede tener una pareja (para simplificarlo).
15
+ * 3. Las relaciones deben validarse dentro de lo posible.
16
+ * Ejemplo: Un hijo no puede tener tres padres.
17
+ * Acciones:
18
+ * 1. Crea un programa que permita crear y modificar el árbol.
19
+ * - Añadir y eliminar personas
20
+ * - Modificar pareja e hijos
21
+ * 2. Podrás imprimir el árbol (de la manera que consideres).
22
+ *
23
+ * NOTA: Ten en cuenta que la complejidad puede ser alta si
24
+ * se implementan todas las posibles relaciones. Intenta marcar
25
+ * tus propias reglas y límites para que te resulte asumible.
26
+ */
27
+ """
28
+
29
+ class Person :
30
+ def __init__ (self , id , name , couple = None , children = None ):
31
+ self .id = id
32
+ self .name = name
33
+ self .couple = couple
34
+ self .children = children if children is not None else []
35
+
36
+ def add_child (self , child ):
37
+ self .children .append (child )
38
+
39
+ def modify_couple (self , couple ):
40
+ self .couple = couple
41
+
42
+ def __repr__ (self ):
43
+ couple_name = self .couple .name if self .couple else "None"
44
+ return f"Person(id={ self .id } , name={ self .name } , couple={ couple_name } , children={ len (self .children )} )"
45
+
46
+ class FamilyTree :
47
+ def __init__ (self , person ):
48
+ self .root = person
49
+ self .people = {person .id : person }
50
+
51
+ def add_person (self , person ):
52
+ if person .id not in self .people :
53
+ self .people [person .id ] = person
54
+ else :
55
+ print (f"La persona con ID { person .id } ya existe en el árbol genealógico." )
56
+
57
+ def delete_person (self , person_id ):
58
+ if person_id in self .people :
59
+ del self .people [person_id ]
60
+ else :
61
+ print (f"No se encontró ninguna persona con ID { person_id } ." )
62
+
63
+ def edit_person (self , id , name = None , couple = None , children = None ):
64
+ if id in self .people :
65
+ person = self .people [id ]
66
+ if name :
67
+ person .name = name
68
+ if couple :
69
+ person .modify_couple (couple )
70
+ if children is not None :
71
+ person .children = children if isinstance (children , list ) else []
72
+ else :
73
+ print (f"No se encontró ninguna persona con ID { id } ." )
74
+
75
+ def display_tree (self , person = None , level = 0 ):
76
+ if person is None :
77
+ person = self .root
78
+
79
+ indent = " " * (level * 4 )
80
+ couple_name = f" & { person .couple .name } " if person .couple else ""
81
+ print (f"{ indent } { person .name } { couple_name } " )
82
+
83
+ if person .children :
84
+ for child in person .children :
85
+ self .display_tree (child , level + 1 )
86
+
87
+ def __repr__ (self ):
88
+ return f"FamilyTree(root={ self .root .name } , total_people={ len (self .people )} )"
89
+
90
+ def main ():
91
+
92
+ jaehaerys = Person (id = 1 , name = "Jaehaerys I Targaryen" )
93
+ alysanne = Person (id = 2 , name = "Alysanne Targaryen" )
94
+
95
+ baelon = Person (id = 3 , name = "Baelon Targaryen" )
96
+ alyssa = Person (id = 4 , name = "Alyssa Targaryen" )
97
+
98
+ viserys = Person (id = 5 , name = "Viserys I Targaryen" )
99
+ aemma = Person (id = 6 , name = "Aemma Arryn" )
100
+
101
+ daemon = Person (id = 7 , name = "Daemon Targaryen" )
102
+ rhea = Person (id = 8 , name = "Rhea Royce" )
103
+ laena = Person (id = 9 , name = "Laena Velaryon" )
104
+ rhaenyra = Person (id = 10 , name = "Rhaenyra Targaryen" )
105
+
106
+ alicent = Person (id = 11 , name = "Alicent Hightower" )
107
+ aegon_ii = Person (id = 12 , name = "Aegon II Targaryen" )
108
+ helaena = Person (id = 13 , name = "Helaena Targaryen" )
109
+ aemond = Person (id = 14 , name = "Aemond Targaryen" )
110
+ daeron = Person (id = 15 , name = "Daeron Targaryen" )
111
+
112
+ jaehaerys .couple = alysanne
113
+ baelon .couple = alyssa
114
+ baelon .children = [viserys , daemon ]
115
+ viserys .couple = aemma
116
+ viserys .children = [rhaenyra ]
117
+ daemon .couple = rhea
118
+ daemon .children = []
119
+
120
+ daemon .couple = laena
121
+ baela = Person (id = 16 , name = "Baela Targaryen" )
122
+ rhaena = Person (id = 17 , name = "Rhaena Targaryen" )
123
+ daemon .children = [baela , rhaena ]
124
+
125
+ rhaenyra .couple = daemon
126
+
127
+ viserys .couple = alicent
128
+ viserys .children += [aegon_ii , helaena , aemond , daeron ]
129
+
130
+ family_tree = FamilyTree (viserys )
131
+ family_tree .add_person (jaehaerys )
132
+ family_tree .add_person (alysanne )
133
+ family_tree .add_person (baelon )
134
+ family_tree .add_person (alyssa )
135
+ family_tree .add_person (daemon )
136
+ family_tree .add_person (rhea )
137
+ family_tree .add_person (laena )
138
+ family_tree .add_person (rhaenyra )
139
+ family_tree .add_person (alicent )
140
+ family_tree .add_person (aegon_ii )
141
+ family_tree .add_person (helaena )
142
+ family_tree .add_person (aemond )
143
+ family_tree .add_person (daeron )
144
+ family_tree .add_person (baela )
145
+ family_tree .add_person (rhaena )
146
+
147
+ family_tree .display_tree ()
148
+
149
+
150
+
151
+ if __name__ == "__main__" :
152
+ main ()
0 commit comments