Querying the Document || Hacker Rank

 Question 21 || Hacker Rank

Querying the Document || Hacker Rank

Querying the Document


A report is addressed as an assortment passage, a section is addressed as an assortment of sentences, a sentence is addressed as an assortment of words and a word is addressed as an assortment of lower-case ([a-z]) and capitalized ([A-Z]) English characters.

 

You will change over a crude message report into its part sections, sentences and words. To test your outcomes, questions will request that you return a particular section, sentence or word as portrayed beneath.

 

Alicia is concentrating on the C programming language at the College of Dunkirk and she addresses the words, sentences, sections, and archives utilizing pointers:

 

·      A word is portrayed by char*.

·      A sentence is portrayed by char**. The words in the sentence are isolated by one space (" "). The final word doesn't end with a space (" ").

·      A section is portrayed by char***. The sentences in the section are isolated by one period (".").

·      A report is depicted by char****. The passages in the report are isolated by one newline("\n"). The last passage doesn't end with a newline.

 

For instance:

 

Learning C is fun.

Learning pointers is more fun. It is good to have pointers.

 

 

·      The main sentence in the principal passage could be addressed as:

char** first_sentence_in_first_paragraph = {"Learning", "C", "is", "fun"};

 

·      The primary passage itself could be addressed as:

char*** first_paragraph = {{"Learning", "C", "is", "fun"}};

 

·      The main sentence in the subsequent section could be addressed as:

char** first_sentence_in_second_paragraph = {"Learning", "pointers", "is", "more", "fun"};

 

·      The second sentence in the subsequent passage could be addressed as:

char** second_sentence_in_second_paragraph = {"It", "is", "great", "to", "have", "pointers"};

 

·      The subsequent passage could be addressed as:

char*** second_paragraph = {{"Learning", "pointers", "is", "more", "fun"}, {"It", "is", "great", "to", "have", "pointers"}};

 

·      At last, the record could be addressed as:

char**** record = {{{"Learning", "C", "is", "fun"}}, {{"Learning", "pointers", "is", "more", "fun"}, {"It", "is", "great", "to", "have", "pointers"}}};

 

Alicia has sent a record to her companion Teodora as a series of characters, for example addressed by char* not  char****. Help her proselyte the report to char**** shape by finishing the accompanying capabilities:

  • ·       char**** get_document(char* text)

            to return the document represented by char****.

  • ·       char*** kth_paragrapgh(char**** document, int k)

            to return the kth paragraph.

  • ·      char* kth_word_in_mth_sentence_of_nth_paragrapgh(char**** document, int k, int m, int n)

            To return the kth word in the mth sentence of the nth  paragrapgh.

 

 

Input Organization

 

The primary line contains the whole number paragrapgh_count.

Every one of the following lines contains a section as a solitary string.

The following line contains the number paragrapgh_count, the quantity of inquiries.

Every one of the following lines or gatherings of lines contains a question in one of the accompanying configurations:

 

·      1) The main line contains 1 k:

 

o   The following line contains a number x, the quantity of sentences in the kth passage.

o   Every one of the following x lines contains a number a[i], the quantity of words in the kthsentence.

o   This question compares to calling the capability kth_paragraph.

 

 

·      2) The primary line contains 2  k  m:

 

o   The following line contains a whole number x, the quantity of words in the sentence of the kth passage to the  mth.

o   This question compares to calling the capability called kth_sentence_in_mth_paragraph.

 

·      3) The main line contains 3  k  m:

 

o   This question relates to calling the capability

Imperatives

kth_word_on_mth_sentence_of_nth_paragrapgh.

 

Constraints:

 

·      The message which is passed to the get_document has words isolated by a space (" "), sentences isolated by a period (".") and passages isolated by a newline("\n").

·      The final say regarding a sentence doesn't end with a space.

·      The last passage doesn't end with a newline.

·      The words contain just capitalized and lower-case English letters.

·      1 <= number of characters in the whole report <= 1000

·      1 <= number of passages in the whole archive <= 5

 

Yield Arrangement

 

Print the section, sentence or the word comparing to the question to actually look at the rationale of your code.

 

Test Info 0

 

2

Learning C is fun.

Learning pointers is more fun.It is good to have pointers.

3

1    2

2

5

6

2    1    1

4

3 1 1 1

 

Test Result 0

 Learning pointers is more fun.It is good to have pointers.

Learning C is fun.

Learning

 

Clarification 0

 The primary inquiry relates to returning the second section with 2 sentences of lengths 5 and 6 words.

The subsequent question compare to returning the principal sentence of the primary section. It contains 4 words.

The third question relates to returning the main expression of the primary sentence of the principal section.



Source Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<assert.h>
#define MAX_CHARACTERS 1005
#define MAX_PARAGRAPHS 5

char* kth_word_in_mth_sentence_of_nth_
paragraph(char**** document, int k, int m, int n) 
{
    return document[n-1][m-1][k-1];
}

char** kth_sentence_in_mth_paragraph(char**** document, int k, int m) 
{
    return document[m-1][k-1];
}

char*** kth_paragraph(char**** document, int k) 
{
    return document[k-1];
}

char** split_string(char* text, char delim) 
{
    assert(text != NULL);
    char** result = malloc(1*sizeof(char*));
    int size = 1;
    
    char* temp = strtok(text, &delim);
    *result = temp;
    
    while(temp != NULL) 
    {
        size++;
        result = realloc(result,size*sizeof(char*));
        temp = strtok(NULL, &delim);
        result[size-1] = temp;
    }
    return result;
}

char**** get_document(char* text) 
{
    assert(text != NULL);
    
    char** paragraphs = split_string(text, '\n');
    int npar = 0;
    while (paragraphs[npar] != NULL) 
    {
        npar++;
    }
    char**** doc = malloc((npar+1)*sizeof(char***));
    doc[npar] = NULL; 
    
    int i = 0;
    while (paragraphs[i] != NULL) 
    {
        
        char** sentences = split_string(paragraphs[i], '.');
        int nsen = 0;
        while(sentences[nsen] != NULL) 
        {
            nsen++;
        }
        
        doc[i] = malloc((nsen+1)*sizeof(char**));
        doc[i][nsen] = NULL; 
        
        int j = 0;
        while (sentences[j] != NULL) 
        {
            
            doc[i][j] = split_string(sentences[j], ' ');
            j++;
        }
        i++;
    }
    
    return doc;
}


char* get_input_text() {    
    int paragraph_count;
    scanf("%d", &paragraph_count);

    char p[MAX_PARAGRAPHS][MAX_CHARACTERS], doc[MAX_CHARACTERS];
    memset(doc, 0sizeof(doc));
    getchar();
    for (int i = 0; i < paragraph_count; i++) {
        scanf("%[^\n]%*c", p[i]);
        strcat(doc, p[i]);
        if (i != paragraph_count - 1)
            strcat(doc, "\n");
    }

    char* returnDoc = (char*)malloc((strlen (doc)+1) * (sizeof(char)));
    strcpy(returnDoc, doc);
    return returnDoc;
}

void print_word(char* word) {
    printf("%s", word);
}

void print_sentence(char** sentence) {
    int word_count;
    scanf("%d", &word_count);
    for(int i = 0; i < word_count; i++){
        printf("%s", sentence[i]);
        if( i != word_count - 1)
            printf(" ");
    }

void print_paragraph(char*** paragraph) {
    int sentence_count;
    scanf("%d", &sentence_count);
    for (int i = 0; i < sentence_count; i++) {
        print_sentence(*(paragraph + i));
        printf(".");
    }
}

int main() 
{
    char* text = get_input_text();
    char**** document = get_document(text);

    int q;
    scanf("%d", &q);

    while (q--) {
        int type;
        scanf("%d", &type);

        if (type == 3){
            int k, m, n;
            scanf("%d %d %d", &k, &m, &n);
            char* word = kth_word_in_mth_sentence_of_nth_
paragraph(document, k, m, n);
            print_word(word);
        }

        else if (type == 2){
            int k, m;
            scanf("%d %d", &k, &m);
            char** sentence = kth_sentence_in_mth_paragraph(document, k, m);
            print_sentence(sentence);
        }

        else{
            int k;
            scanf("%d", &k);
            char*** paragraph = kth_paragraph(document, k);
            print_paragraph(paragraph);
        }
        printf("\n");
    }     
}



Output:

Querying the Document



Post a Comment

0 Comments