Structuring the Document

Sort by

recency

|

131 Discussions

|

  • + 1 comment

    struct document Doc = {0};

    void initialise_document(char *text){
    char text_buffer[MAX_CHARACTERS]; strcpy(text_buffer, text); int p_count = 0;

    // number of paragraphs
    char *paragraphs[MAX_PARAGRAPHS]; // temporary storage    
    char *para_tok = strtok(text_buffer, "\n");
    while(para_tok != NULL){
        paragraphs[p_count++] = para_tok;                   
        para_tok = strtok(NULL, "\n");   
    }
    
    // Initializing doc structure  
    //doc = malloc(sizeof(struct document));
    Doc.paragraph_count = p_count;
    Doc.data = malloc(sizeof(struct paragraph) * p_count );
    

    // for each paragraph sentence count

    for(int i = 0; i < p_count ; i++){
        char para_buffer[MAX_CHARACTERS];
        strcpy(para_buffer, paragraphs[i]);
        int s_count = 0;
        char *sentences[100];
    
        char *sen_tok = strtok(para_buffer,".");
        while(sen_tok != NULL){
            sentences[s_count++] = sen_tok;
            sen_tok = strtok(NULL, ".");
        }            
        Doc.data[i].sentence_count = s_count;
        Doc.data[i].data = malloc(sizeof(struct sentence) * s_count);
    
       // for each sentence word count 
        for(int j = 0; j < s_count; j++){
            char sen_buffer[MAX_CHARACTERS];
            strcpy(sen_buffer, sentences[j]);
    
            //count words
            int w_count = 0;
            char *words[100];
    
            char *word_tok = strtok(sen_buffer, " ");
            while(word_tok != NULL){
                words[w_count++] = word_tok;
                word_tok = strtok(NULL," ");            
            }
    
            Doc.data[i].data[j].word_count = w_count;
            Doc.data[i].data[j].data = malloc(sizeof(struct word) * w_count);
    
            for(int k =0 ; k < w_count; k++){
                Doc.data[i].data[j].data[k].data = malloc(strlen(words[k])+1);
                strcpy(Doc.data[i].data[j].data[k].data,words[k]);
            }  
    
        }
    } 
    

    }

    struct document get_document(char* text) {

    // Initialise and allocate.
    initialise_document(text);
    
    return Doc;
    

    }

    struct word kth_word_in_mth_sentence_of_nth_paragraph(struct document Doc, int k, int m, int n) {

    if (k < 1 || k > Doc.paragraph_count) {
        printf("Paragraph index out of bounds\n");
        exit(1);
        }
    if (m < 1 || m > Doc.data[k-1].sentence_count) {
        printf("word  index out of bounds\n");
        exit(1);
        }
    
    if (n < 1 || n > Doc.data[k-1].data[n-1].word_count) {
        printf("word  index out of bounds\n");
        exit(1);
        }
    
    return Doc.data[k-1].data[m-1].data[n-1];
    

    }

    struct sentence kth_sentence_in_mth_paragraph(struct document Doc, int k, int m) { if (k < 1 || k > Doc.paragraph_count) { printf("Paragraph index out of bounds\n"); exit(1); }

    if (m < 1 || m > Doc.data[k-1].sentence_count) {
        printf("Sentence index out of bounds\n");
        exit(1);
    }
    
    return Doc.data[k-1].data[m-1];
    

    }

    struct paragraph kth_paragraph(struct document Doc, int k) { if(k >= 1 || k <= Doc.paragraph_count){ return Doc.data[k-1]; } else{ printf("k is out of bound"); exit(1); } }

  • + 0 comments

    When accessing the k-th/m-th/n-th element, all the test cases are off by one. The arrays are indexed from 1 to n instead of the usual 0 to n-1. This discrepancy caused the problem to take me over 24 hours to solve, whereas it should have taken around 90 to 120 minutes. Detail explaination: https://priceindanger.fr/

  • + 0 comments

    When calling the kth/mth/nth element everything in the test cases is off by one. Instead of going from 0:n-1 the arrays go from 1:n. This made the problem take >24 hours instead of 90:120 minutes for me to solve.

  • + 0 comments

    on you code I find one mistake here is the updated code visit: https://priceindanger.com/

  • + 7 comments

    Here is Structuring the Document solution in c - https://programmingoneonone.com/hackerrank-structuring-the-document-solution-in-c.html