CB100

Notas, resueltos y tps de la materia Algoritmos y Estructuras de Datos
Index Commits Files Refs README
commit d3ab702482715cc3762916f3937bf38581c60432
parent 99ca55c6cfe8c54ff93f347d5b982c74fb58af80
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date:   Thu, 23 May 2024 00:03:38 -0300

fix solution `parciales/2022-2C-1p/2.cpp`

Diffstat:
Mparciales/2022-2C-1p/2.cpp | 213+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 161 insertions(+), 52 deletions(-)
diff --git a/parciales/2022-2C-1p/2.cpp b/parciales/2022-2C-1p/2.cpp
@@ -1,94 +1,203 @@
 #include <iostream>
 #include <string>
 
-class Comentario { 
-    std::string comment;
-    int score;
+template <typename Type>
+class Node {
+private:
+    Node<Type> *next;
+    Type data;
 
-public: 
-    // post: inicializa el Comentario con el contenido y calificación 0.
-    Comentario(std::string contenido) {
-        this->comment = contenido;
+public:
+    Node() {
+        this->next = NULL;
     }
 
-    std::string obtenerContenido() {
-        return comment;
+    Node(Type data) {
+        this->next = NULL;
+        this->data = data;
     }
 
-    // post: devuelve la calificación [1 a 10] asociada,
-    //       o 0 si el Comentario no tiene calificación
-    int obtenerCalificacion() {
-        return this->score;
+    Node<Type> *getNext() {
+        return this->next;
     }
 
-    // pre: calificacion está comprendido entre 1 y 10
-    // post: cambia la calificación del Comentario
-    void calificar(int calificacion) {
-        this->score = calificacion;
+    virtual ~Node() {}
+
+    Type getData() {
+        return this->data;
     }
 };
 
 template <typename Type>
-class Lista {
+class List {
 private:
-    Nodo<Type> *lista;
-    unsigned int length;
+    Node<Type> *first;
+    Node<Type> *last;
+    Node<Type> *cursor;
+    unsigned int len;
 
 public:
-    Lista() {
-        this->current = new Type;
-        this->next = NULL;
+    List() {
+        this->first = new Type;
+        this->last = this->first;
     }
 
-    virtual ~Lista() {
-        delete this->next;
-        this->next = NULL;
+    virtual ~List() {
+        Node<Type> *curr = this->first;
+        Node<Type> *prev = curr;
+        while(curr->getNext() != NULL) {
+            prev = curr;
+            curr = curr->getNext();
+            delete prev;
+        }
+    }
+
+    void Add(Type data) {
+        this->last = new Node<Type>(data);
+    }
+
+    void startCursor() {
+        this->cursor = this->first;
+    }
+
+    bool forwardCursor() {
+        return (((this->cursor = this->cursor->getNext()) == NULL) ? true : false);
+    }
+
+    Node<Type> *getCursor() {
+        return this->cursor;
+    }
+
+    unsigned int size() {
+        return this->len;
+    }
+};
+
+class Comment {
+    std::string comment;
+    int score;
+
+public:
+    /*
+     * post: inicializa el Comentario con el contenido y calificación 0.
+     */
+    Comment(std::string contenido) {
+        this->comment = contenido;
+    }
+
+    std::string getContent() {
+        return this->comment;
+    }
+
+    /*
+     * post: devuelve la calificación [1 a 10] asociada,
+     *       o 0 si el Comentario no tiene calificación
+     */
+    int getScore() {
+        return this->score;
     }
 
-    void Add(Type node) {
-        this->next = new Type;
+    /*
+     * pre: calificacion está comprendido entre 1 y 10
+     * post: cambia la calificación del Comentario
+     */
+    void setScore(int calificacion) {
+        this->score = calificacion;
     }
 };
 
-class Imagen {
+class Image {
 private:
     std::string url;
-public: 
-    // post: inicializa la Imagen alojada en la URL indicada
-    Imagen(std::string url) {
+public:
+    /*
+     * post: inicializa la Imagen alojada en la URL indicada
+     */
+    Image(std::string url) {
         this->url = url;
     }
 
-    // post: devuelve la URL en la que está alojada
-    std::string obtenerUrl(); 
+    virtual ~Image() {}
 
-    // post: devuelve los comentarios asociados
-    Lista<Comentario*> *obtenerComentarios(); 
-
-    virtual ~Imagen() {}
-}; 
+    /*
+     * post: devuelve la URL en la que está alojada
+     */
+    std::string getUrl();
 
+    /*
+     * post: devuelve los comentarios asociados
+     */
+    List<Comment*> *getComments();
+};
 
 class Editor {
-public: 
-    // post: selecciona de ‘imagenesDisponibles’ aquella que tenga por lo
-    //       menos tantos Comentarios como los indicados y
-    //       el promedio de calificaciones sea máximo. Ignora los
-    //       Comentarios sin calificación. 
-    Imagen *seleccionarImagen(Lista<Imagen*> *availableImgs, int commentsLen) {
-        if(commentsLen < 0) {
-            throw "`commentsLen` no puede ser negativo";
+public:
+
+    /*
+     * post: devuelve de `imgComments` aquel comentario que tenga por lo menos 
+     *       `scoreCount` calificaciones y ademas tenga el promedio de
+     *       calificaciones maximo. Debe recorrer toda la lista para hallar el
+     *       el comentario con mayor promedio de calificacion
+     */
+    Comment *getCommentWithHighestScore(List<Comment *> *imgComments) {
+        Comment *returnComment;
+        Comment *currentComment;
+        int currentScore, maxScore;
+
+        returnComment = currentComment = NULL;
+        currentScore = maxScore = 0;
+        imgComments->startCursor();
+
+        while(imgComments->forwardCursor()) {
+            currentComment = imgComments->getCursor()->getData();
+            currentScore = returnComment->getScore();
+            if(currentScore > 0) {
+                if(currentScore > maxScore) {
+                    maxScore = currentScore;
+                    returnComment = currentComment;
+                }
+            }
+        }
+        return returnComment;
+    }
+
+    /*
+     * post: selecciona de `availableImages` aquella que tenga por lo
+     *       menos tantos Comentarios como los indicados en `commentsCount` y
+     *       el promedio de calificaciones sea máximo. Ignora los
+     *       Comentarios sin calificación.
+     */
+    Image *seleccionarImagen(List<Image*> *availableImages, int commentsCount) {
+        if(commentsCount < 0) {
+            throw "la cantidad de comentarios no puede ser negativo";
+        } else if(availableImages == NULL) {
+            throw "puntero `NULL`";
         }
 
-        while(availableImgs->obtenerComentarios() != NULL) {
-            if(availableImgs->obtenerComentarios()) {
-                return NULL;
+        Image *returnImg;
+        Image *currentImg;
+        List<Comment*> *currentImgComments;
+
+        returnImg = currentImg = NULL;
+
+        // se iteran las imagenes
+        availableImages->startCursor();
+        while(availableImages->forwardCursor()) {
+            currentImg = availableImages->getCursor()->getData();
+            currentImgComments = currentImg->getComments();
+            if(currentImgComments->size() > (unsigned int)commentsCount) {
+                // iteran los comentarios de la imagen y se obtiene el 
+                // comentario con mas calificaciones
+                if(this->getCommentWithHighestScore(currentImgComments) != NULL) {
+                    return returnImg = currentImg;
+                }
             }
         }
     }
-}; 
+};
 
 int main () {
-    std::cout << "Hello, World!\n";
+    Editor *e = new Editor;
+    delete e;
     return 0;
 }