Solución

@maiquez·27/10/2026TypeScript
solution.tsTypeScript
from typing import Callable

SortStrategy = Callable[[list], list]

def asc_sort(items: list) -> list:
    return sorted(items)

def desc_sort(items: list) -> list:
    return sorted(items, reverse = True)

def by_length_sort(items: list) -> list:
    return sorted(items, key=len)

class Sorter:
    def __init__(self, strategy: SortStrategy = asc_sort):
        self.strategy = strategy
    
    def sort(self, data: list) -> list:
        return self.strategy(data)
    
    def set_strategy(self, strategy: SortStrategy) -> None:
        self.strategy = strategy 

def sort_with_strategy(items: list[str], strategy: str) -> list[str]:
    # Define un diccionario de estrategias de ordenación
    # y aplica la que corresponde al parámetro strategy.
    match strategy:
        case "asc":
            sorter = Sorter(asc_sort)
        case "desc":
            sorter = Sorter(desc_sort)
        case "by_length":
            sorter = Sorter(by_length_sort)
        case _:
            return None

    return sorter.sort(items)
0respuestas
Respuestas

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.