“Histogram” Kod odpowiedzi

Wykres histogramu

import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="total_bill", nbins=20)
fig.show()
Disgusted Duck

Histogram

//
// Created by merom on 5/17/2022.
//
#include "Hist.h"
#include <stdlib.h>
#include <stdio.h>
#define SIZE 100
struct Hist  {
    Set setE;
    int * arrIn;
    unsigned int arrSize;
    Element (*clone_func)(Element);
    void (*free_func)(Element);
    bool (*cmp_func)(Element, Element);
};

Hist HistCreate(Element (*clone_func)(Element),
                void (*free_func)(Element),
                bool (*cmp_func)(Element, Element)){

    Hist hist = calloc(1, sizeof(struct Hist));
    if(hist == NULL) {
        fprintf(stderr, "%s/%u: failed to allocate %lu bytes\n\n",
                __FILE__, __LINE__, sizeof(struct Hist));
        exit(-1);
    }
    hist->arrIn = calloc(SIZE, sizeof(int));
    hist->arrSize = SIZE;

    if(hist->arrIn == NULL){
        fprintf(stderr, "%s/%u: failed to allocate %lu bytes\n\n",
                __FILE__, __LINE__, SIZE*sizeof(int));
        exit(-1);
    }
    hist->setE= SetCreate(clone_func, free_func, cmp_func);
    hist->clone_func = clone_func;
    hist->cmp_func = cmp_func;
    hist->free_func = free_func;

    return hist;

}


void HistDestroy(Hist hist){
    SetDestroy(hist->setE);
    free(hist->arrIn);
    free(hist);
}



unsigned int HistSize(Hist hist){
    Set my_set = hist->setE;
    Element e = SetFirst(my_set);
    int i = 0;
    while(e){
        i ++;
        e = SetNext(my_set);
    }

    return i ;
}




// Get the count of element e. If e is not in hist, returns 0.
int HistGetCount(Hist hist, Element e){
    Set mySet = hist->setE;

    Element elem = SetFirst(mySet);
    int i = 0;
    while(hist->cmp_func(elem,e) == false){
        elem = SetNext(mySet);
        i++;
    }
    return hist->arrIn[i];
}





// Increment the count of e by one.
// If e is not in hist, create a new entry with a clone of e and a count of 1.
void HistInc(Hist hist, Element e){
    Set mySet = hist->setE;

    Element FIRST = SetFirst(mySet);
    unsigned int index = 0;
    bool is_in = SetIsIn(mySet, e);


    if(!is_in){
        /* If the element does not exist inside the Hist add the element to the end of the set
         * And set the value of the array index to 1
         *
         * */
        while(true){
            if(FIRST == NULL){
                if(hist->arrSize >0){
                    SetAdd(mySet,e);
                    hist->arrIn[index] = 1;
                    hist->arrSize -=1;
                    break;
                }
                /* If the array of index size has reached 0, reallocate new array with double size and copy the previous values*/
                if(hist->arrSize == 0){

                    static unsigned int up = 2;
                    hist->arrSize= SIZE * up;
                    hist->arrIn = realloc(hist->arrIn, hist->arrSize*sizeof(unsigned int));

                    if(!hist->arrIn) {
                        fprintf(stderr, "%s/%u: failed to re-allocated %lu bytes\n\n",
                                __FILE__, __LINE__, hist->arrSize*sizeof(Element));
                        exit(-1);
                    }

                    up++;

                }

            }
            FIRST = SetNext(mySet);
            index++;
        }
    }else{
        /* If it does exist, increase the element count by one */
        while( (FIRST)  ){

            if(hist->cmp_func(FIRST,e)){
                hist->arrIn[index] += 1;
                break;
            }
            FIRST = SetNext(mySet);
            index ++;
        }
    }
}



// Gets (a clone of) the element at given index.
// If index<0 or index >= HistSize(hist) then NULL is returned.
Element HistGetElement(Hist hist, unsigned int index){
    unsigned int size = HistSize(hist);
    if( (0>index) || (index >= size )) return NULL;

    Element elem = SetFirst(hist->setE);
    int i =0;

    while( (elem) && (i< index) ) {
        elem = SetNext(hist->setE);
        i++;
    }

    return hist->clone_func(elem);
}


Mero

Histogram

//
// Created by merom on 4/30/2022.
//

#ifndef HOMEWORK_4_10_05_2022_ME_HIST_H
#define HOMEWORK_4_10_05_2022_ME_HIST_H

#include <stdbool.h>
#include "Set.h"


typedef struct Hist *Hist;

// An ADT for the Hist object. Such object maintains and unordered set of elements, along with a count for each one.

// Create a new histogram object, with element manipulation functions:
// clone_func - return a pointer to the cloned element.
// free_func - free an element returned by clone_func
// cmp_func - return true if the two elements are equal

Hist HistCreate(Element (*clone_func)(Element),
                void (*free_func)(Element),
                bool (*cmp_func)(Element, Element));



// Destroy a histogram object (along with all its elements)
void HistDestroy(Hist hist);

// Return the number of elements in the hist object
unsigned int HistSize(Hist hist);

// Get the count of element e. If e is not in hist, returns 0.
int HistGetCount(Hist hist, Element e);

// Increment the count of e by one.
// If e is not in hist, create a new entry with a clone of e and a count of 1.
void HistInc(Hist hist, Element e);

// Gets (a clone of) the element at given index.
// If index<0 or index >= HistSize(hist) then NULL is returned.
Element HistGetElement(Hist hist, unsigned int index);

#endif //HOMEWORK_4_10_05_2022_ME_HIST_H
Mero

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu