Querying the Document

Sort by

recency

|

161 Discussions

|

  • + 0 comments

    I built https://www.volealo.com/ using C language. I'm not a professional developer, so there are still some issues I haven't been able to fix. If anyone here can help or offer guidance,

    I’d really appreciate it!

  • + 0 comments

    Here is Querying the Document solution in C - https://programmingoneonone.com/hackerrank-querying-the-document-solution-in-c.html

  • + 0 comments
    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**** get_document(char* text) {
        int i=0,j=0,k=0,n_para = 0, n_sent[MAX_PARAGRAPHS] = {0}, n_word[MAX_PARAGRAPHS][MAX_CHARACTERS] = {{0}};
        char ****doc = NULL;
        text[strlen(text)] = '\n';
        text[strlen(text)] = '\0';
        //printf("%s\n", text);
        while(text[i] != '\0') {
            if(text[i] == ' ') {
                n_word[n_para][n_sent[n_para]]++;
                i++;
                continue;
            }
            else if(text[i] == '.') {
                n_word[n_para][n_sent[n_para]]++;
                n_sent[n_para]++;
                i++;
                continue;
            }
            else if(text[i] == '\n') {
                n_para++;
                i++;
            } else {
                i++;
            }
        }
        doc = (char ****)calloc(n_para, sizeof(char ***));
        char *str = text;
        char *reserve = NULL;
        for(i = 0; i < n_para; i++) {
            *(doc+i) = (char ***) calloc(n_sent[i], sizeof(char **));
            //printf("n_sent[%d] = %d\n", i, n_sent[i]);
            for(j = 0; j < n_sent[i]; j++) {
                *(*(doc+i)+j) = (char **) calloc(n_word[i][j], sizeof(char *));
                //printf("n_word[%d][%d] = %d\n", i, j, n_word[i][j]);
                for(k = 0; k < n_word[i][j]; k++) {
                    *(*(*(doc+i)+j)+k) = (char *)calloc(100, sizeof(char));
                    doc[i][j][k] = strtok_r(str, " .\n", &reserve);
                    if(i == 0 && j == 0 && k == 0)
                        str = NULL;
                }
            }
        }
        return doc;
    }
    
  • + 0 comments
    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**** get_document(char* text) {
        char**** document = (char****)malloc(sizeof(char***)*5);
        for (int i = 0; i < 5; i++){
            document[i] = (char***)malloc(sizeof(char**)*10);
            for (int j = 0; j < 10; j++){
                document[i][j] = (char**)malloc(sizeof(char*)*50);
                for(int k = 0; k < 50; k++){
                    document[i][j][k] = (char*)malloc(sizeof(char)*100);
                }
            }
        } 
        char ch = ' ';
        int par = 0, sen = 0, wor = 0, let = 0;
        for (int i = 0; i < strlen(text); i++){
            ch = text[i];
            if (ch == ' '){
                wor++;
                let = 0;
            } else if (ch == '.'){
                sen++;
                wor = 0;
                let = 0;
            } else if (ch == '\n'){
                par++;
                sen = 0;
                wor = 0;
                let = 0;
            } else{
                document[par][sen][wor][let] = ch;
                let++;
            }
        }
        return document;
    }
    
  • + 0 comments

    This test measures nothing other than the candidate's tolerance for torture.

    There would only be two realistic approaches to this problem in real life.

    1) Rewrite the entire thing, and if not allowed to do that: 2) Quit the job.