POO-ITBA / 2024_01

Consultas 1C 2024
0 stars 0 forks source link

Recup 2 2023 1C Ej 3 #56

Closed whatisthisgit closed 1 month ago

whatisthisgit commented 1 month ago

Hola, queria saber si mi resolucion del ej 3 esta bien. Presumo que es muy diferente a la resolucion dada por que no se podia usar Sequenced Collection (y TreeSet) aun. Rec2_23_01.pdf

public interface SpecialCollection<T extends Comparable<? super T>> extends SortedSet<T>
{
    int count (Predicate<T> condition);
    int count (T first, T second, Predicate<T> condition);
}

public class SpecialCollectionImpl<T extends Comparable<? super T>> extends TreeSet<T>
{
    public int count (Predicate<T> condition)
    {
        return count(first(), last(), condition);
    }

    public int count (T first, T second, Predicate<T> condition)
    {
        T f1 = ceiling(first);
        T f2 = floor(second);
        int accum = 0;
        for( T thing : subSet(f1, f2))
        {
            if (condition.test(thing))
            {
                accum++;
            }
        }
        return accum;
    }
}
fmeola commented 1 month ago

Hola Jorge. Hay que tener cuidado al usar subset. Tenés dos problemas:

       System.out.println(sCol.count("hello", "hello", sPred)); // 1

vas a obtener 0 y no uno. Y no hay una manera de corregirlo, habría que implementarlo sin invocar a subset. No te olvides de que SpecialCollectionImpl implemente la interfaz SpecialCollection

fmeola commented 1 month ago

Ahí viendo en la documentación hay otra versión de subset donde podés indicar ambos extremos inclusive. Es así:

        for( T thing : subSet(f1, true, f2, true))

Y agrego que habría que asegurarse que from es menor a to para invocar a subset, de lo contrario arrojará una excepción. Eso te puede traer problemas en

       System.out.println(sCol.count("Z", "A", sPred)); // 0

Se puede corregir así:

        if(first.compareTo(second) > 0) {
            return 0;
        }
whatisthisgit commented 1 month ago

Gracias Franco, clarisimo.