From 0ef8c91a2647a621b21e447d7814890bda6a961f Mon Sep 17 00:00:00 2001 From: Brais Moure Date: Mon, 1 Jul 2024 21:49:31 +0200 Subject: [PATCH] =?UTF-8?q?Correcci=C3=B3n=20Roadmap=2026=20+=20Nuevo=20ej?= =?UTF-8?q?ercicio=2027?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +- Roadmap/26 - SOLID SRP/python/mouredev.py | 147 ++++++++++++++++++++++ Roadmap/27 - SOLID OCP/ejercicio.md | 28 +++++ 3 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 Roadmap/26 - SOLID SRP/python/mouredev.py create mode 100644 Roadmap/27 - SOLID OCP/ejercicio.md diff --git a/README.md b/README.md index 157f57f6a4..0ed8904750 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,8 @@ ## Corrección y próximo ejercicio -> #### Lunes 1 de julio de 2024 a las 20:00 (hora España) desde **[Twitch](https://twitch.tv/mouredev)** -> #### Consulta el **[horario](https://discord.gg/CPKcDD9d?event=1252321976027054111)** por país y crea un **[recordatorio](https://discord.gg/CPKcDD9d?event=1252321976027054111)** +> #### Lunes 8 de julio de 2024 a las 20:00 (hora España) desde **[Twitch](https://twitch.tv/mouredev)** +> #### Consulta el **[horario](https://discord.gg/4azkvPUJ?event=1254974320136949871)** por país y crea un **[recordatorio](https://discord.gg/4azkvPUJ?event=1254974320136949871)** ## Roadmap @@ -60,7 +60,8 @@ |23|[SINGLETON](./Roadmap/23%20-%20SINGLETON/ejercicio.md)|[📝](./Roadmap/23%20-%20SINGLETON/python/mouredev.py)|[▶️](https://youtu.be/cOIcFo_w9hA)|[👥](./Roadmap/23%20-%20SINGLETON/) |24|[DECORADORES](./Roadmap/24%20-%20DECORADORES/ejercicio.md)|[📝](./Roadmap/24%20-%20DECORADORES/python/mouredev.py)|[▶️](https://youtu.be/jxJOjg7gPG4)|[👥](./Roadmap/24%20-%20DECORADORES/) |25|[LOGS](./Roadmap/25%20-%20LOGS/ejercicio.md)|[📝](./Roadmap/25%20-%20LOGS/python/mouredev.py)|[▶️](https://youtu.be/y2O6L1r_skc)|[👥](./Roadmap/25%20-%20LOGS/) -|26|[SOLID: PRINCIPIO DE RESPONSABILIDAD ÚNICA](./Roadmap/26%20-%20SOLID%20SRP/ejercicio.md)|[🗓️ 01/07/24](https://discord.gg/CPKcDD9d?event=1252321976027054111)||[👥](./Roadmap/26%20-%20SOLID%20SRP/) +|26|[SOLID: PRINCIPIO DE RESPONSABILIDAD ÚNICA](./Roadmap/26%20-%20SOLID%20SRP/ejercicio.md)|[📝](./Roadmap/26%20-%20SOLID%20SRP/python/mouredev.py)||[👥](./Roadmap/26%20-%20SOLID%20SRP) +|27|[SOLID: PRINCIPIO ABIERTO-CERRADO](./Roadmap/27%20-%20SOLID%20OCP/ejercicio.md)|[🗓️ 08/07/24](https://discord.gg/4azkvPUJ?event=1254974320136949871)||[👥](./Roadmap/27%20-%20SOLID%20OCP/) ## Instrucciones diff --git a/Roadmap/26 - SOLID SRP/python/mouredev.py b/Roadmap/26 - SOLID SRP/python/mouredev.py new file mode 100644 index 0000000000..253b17807e --- /dev/null +++ b/Roadmap/26 - SOLID SRP/python/mouredev.py @@ -0,0 +1,147 @@ +""" +Ejercicio +""" + +# Incorrecto + + +class User: + + def __init__(self, name, email) -> None: + self.name = name + self.email = email + + def save_to_database(self): + pass + + def send_email(self): + pass + +# Correcto + + +class User: + + def __init__(self, name, email) -> None: + self.name = name + self.email = email + + +class UserService: + + def save_to_database(self, user): + pass + + +class EmailService: + + def send_email(self, email, message): + pass + + +""" +Extra +""" + +# Incorrecto + + +class Library: + + def __init__(self) -> None: + self.books = [] + self.users = [] + self.loans = [] + + def add_book(self, title, author, copies): + self.books.append({"title": title, "author": author, "copies": copies}) + + def add_user(self, name, id, email): + self.users.append({"name": name, "id": id, "email": email}) + + def loan_book(self, user_id, book_title): + for book in self.books: + if book["title"] == book_title and book["copies"] > 0: + book["copies"] -= 1 + self.loans.append( + {"user_id": user_id, "book_title": book_title}) + return True + return False + + def return_book(self, user_id, book_title): + for loan in self.loans: + if loan["user_id"] == user_id and loan["book_title"] == book_title: + self.loans.remove(loan) + for book in self.books: + if book["title"] == book_title: + book["copies"] += 1 + return True + return False + +# Correcto + + +class Book: + + def __init__(self, title, author, copies): + self.title = title + self.author = author + self.copies = copies + + +class User: + + def __init__(self, name, id, email): + self.name = name + self.id = id + self.email = email + + +class Loan: + + def __init__(self): + self.loans = [] + + def loan_book(self, user, book): + if book.copies > 0: + book.copies -= 1 + self.loans.append( + {"user_id": user.id, "book_title": book.title}) + return True + return False + + def return_book(self, user, book): + for loan in self.loans: + if loan["user_id"] == user.id and loan["book_title"] == book.title: + self.loans.remove(loan) + book.copies += 1 + return True + return False + + +class Library: + + def __init__(self) -> None: + self.books = [] + self.users = [] + self.loans_service = Loan() + + def add_book(self, book): + self.books.append(book) + + def add_user(self, user): + self.users.append(user) + + def loan_book(self, user_id, book_title): + user = next((u for u in self.users if u.id == user_id), None) + book = next((b for b in self.books if b.title == book_title), None) + if user and book: + return self.loans_service.loan_book(user, book) + return False + + def return_book(self, user_id, book_title): + user = next((u for u in self.users if u.id == user_id), None) + book = next((b for b in self.books if b.title == book_title), None) + if user and book: + return self.loans_service.return_book(user, book) + return False diff --git a/Roadmap/27 - SOLID OCP/ejercicio.md b/Roadmap/27 - SOLID OCP/ejercicio.md new file mode 100644 index 0000000000..871706389c --- /dev/null +++ b/Roadmap/27 - SOLID OCP/ejercicio.md @@ -0,0 +1,28 @@ +# #27 SOLID: PRINCIPIO ABIERTO-CERRADO (OCP) +> #### Dificultad: Media | Publicación: 01/07/24 | Corrección: 08/07/24 + +## Ejercicio + +``` +/* + * EJERCICIO: + * Explora el "Principio SOLID Abierto-Cerrado (Open-Close Principle, OCP)" + * y crea un ejemplo simple donde se muestre su funcionamiento + * de forma correcta e incorrecta. + * + * DIFICULTAD EXTRA (opcional): + * Desarrolla una calculadora que necesita realizar diversas operaciones matemáticas. + * Requisitos: + * - Debes diseñar un sistema que permita agregar nuevas operaciones utilizando el OCP. + * Instrucciones: + * 1. Implementa las operaciones de suma, resta, multiplicación y división. + * 2. Comprueba que el sistema funciona. + * 3. Agrega una quinta operación para calcular potencias. + * 4. Comprueba que se cumple el OCP. + */ +``` +#### Tienes toda la información extendida sobre el roadmap de retos de programación en **[retosdeprogramacion.com/roadmap](https://retosdeprogramacion.com/roadmap)**. + +Sigue las **[instrucciones](../../README.md)**, consulta las correcciones y aporta la tuya propia utilizando el lenguaje de programación que quieras. + +> Recuerda que cada semana se publica un nuevo ejercicio y se corrige el de la semana anterior en directo desde **[Twitch](https://twitch.tv/mouredev)**. Tienes el horario en la sección "eventos" del servidor de **[Discord](https://discord.gg/mouredev)**. \ No newline at end of file