Skip to content

- #1

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

- #1

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,59 @@
# Python-Challenges
In this repository will be uploaded python code based on programming challenges.
# 🌳 Family Tree

This challenge consists of implementing a **family tree management system** in Python, allowing users to add, remove, and modify family relationships interactively via the terminal 🦝.

## 📌 Features

- 📌 **Add people** with a unique identifier.
- 💍 **Assign partners** to registered individuals.
- 👶 **Add children** to existing parents.
- ❌ **Remove people or relationships** (partners and children).
- 📜 **Visualize the family tree** in the terminal with enhanced formatting using `rich`.

## 🛠️ Requirements

- Python 3.x
- `rich` library for styled terminal output:

```bash
pip install rich
```

## 🚀 Usage

1. **Run the program**:

```bash
python main.py
```

2. **Select an option** from the menu to manage the family tree:

```
[1] Add Person
[2] Remove Person
[3] Modify Partner
[4] Add Child
[5] Remove Child
[6] Print Family Tree
[7] Exit
```

3. **Example workflow**:

- Add a person with ID 1 named "Carlos."
- Add a person with ID 2 named "Ana" and assign her as Carlos' partner.
- Add a person with ID 3 named "Luis" and assign him as Carlos' child.

4. **Expected output when printing the tree**:

```
--- Family Tree ---
Carlos (ID: 1)
Partner: Ana
Child: Luis (ID: 3)
Ana (ID: 2)
Partner: Carlos
Luis (ID: 3)
Partner: None
```
101 changes: 101 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt

console = Console()

class Person:
def __init__(self, id, name, partner=None):
self.id = id
self.name = name
self.partner = partner
self.children = []

def add_child(self, child):
self.children.append(child)

class FamilyTree:
def __init__(self):
self.people = {}

def add_person(self, id, name, partner_id=None):
person = Person(id, name)
self.people[id] = person
if partner_id and partner_id in self.people:
partner = self.people[partner_id]
person.partner = partner
partner.partner = person

def remove_person(self, id):
if id in self.people:
if self.people[id].partner:
self.people[id].partner.partner = None
del self.people[id]

def modify_partner(self, id, partner_id):
if id in self.people and partner_id in self.people:
self.people[id].partner = self.people[partner_id]
self.people[partner_id].partner = self.people[id]

def add_child(self, parent_id, child_id):
if parent_id in self.people and child_id in self.people:
self.people[parent_id].add_child(self.people[child_id])

def remove_child(self, parent_id, child_id):
if parent_id in self.people and child_id in self.people:
self.people[parent_id].children = [
child for child in self.people[parent_id].children if child.id != child_id
]

def print_tree(self):
console.print("[bold cyan]--- Árbol Genealógico ---[/bold cyan]", style="bold yellow")
for id, person in self.people.items():
partner_name = person.partner.name if person.partner else "Ninguna"
console.print(f"[bold green]{person.name}[/bold green] (ID: {id})")
console.print(f" Pareja: [bold red]{partner_name}[/bold red]")
for child in person.children:
console.print(f" Hijo: [bold blue]{child.name}[/bold blue] (ID: {child.id})")

def show_menu():
while True:
console.print(Panel.fit(
"[1] [green]Añadir Persona[/green]\n"
"[2] [green]Eliminar Persona[/green]\n"
"[3] [green]Modificar Pareja[/green]\n"
"[4] [green]Añadir Hijo[/green]\n"
"[5] [green]Eliminar Hijo[/green]\n"
"[6] [green]Imprimir Árbol[/green]\n"
"[7] [red]Salir[/red]",
title="Menú Árbol Genealógico",
border_style="bold cyan"
))
option = Prompt.ask("[bold yellow]Elige una opción[/bold yellow]", choices=["1", "2", "3", "4", "5", "6", "7"])

if option == "1":
id = int(Prompt.ask("ID de la Persona"))
name = Prompt.ask("Nombre de la Persona")
partner_id = Prompt.ask("ID de la Pareja (opcional)", default=None)
tree.add_person(id, name, int(partner_id) if partner_id else None)
elif option == "2":
id = int(Prompt.ask("ID de la Persona a eliminar"))
tree.remove_person(id)
elif option == "3":
id = int(Prompt.ask("ID de la Persona"))
partner_id = int(Prompt.ask("ID de la nueva Pareja"))
tree.modify_partner(id, partner_id)
elif option == "4":
parent_id = int(Prompt.ask("ID del Padre o Madre"))
child_id = int(Prompt.ask("ID del Hijo"))
tree.add_child(parent_id, child_id)
elif option == "5":
parent_id = int(Prompt.ask("ID del Padre o Madre"))
child_id = int(Prompt.ask("ID del Hijo a eliminar"))
tree.remove_child(parent_id, child_id)
elif option == "6":
tree.print_tree()
elif option == "7":
console.print("[bold red]Saliendo...[/bold red]")
break

tree = FamilyTree()
show_menu()