2013-03-13 3 views
0

나는 Sevone의 최근 인터뷰에서 질문을 받았지만 나는 어떻게 행동했는지에 대한 답변을 얻지 못했습니다. 도전 과제를 완료하는 데 2 ​​시간 밖에 안 걸렸으므로 성별로 분류 할 수 없었습니다. 여기에 내가 시작하고, 더욱 아래 여기C CSV GLIB 정렬 optmization

// Sevone Programming Challenge! 

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <glib.h> 

//! This is the sex of a human. 
typedef enum { MALE, FEMALE } Gender; 

//! This is a human person. 
typedef struct Person { 
    //! The given name. 
    char* firstName; 
    //! The family name. 
    char* lastName; 
    //! The age, in calendar years. 
    int age; 
    //! The sex (see above). 
    Gender gender; 
} Person; 

/** This is the bonus function. 
    **/ 
void bonusFunction(); 

/** This is the core of the program. 
    **/ 
int main(int argc, char** argv) { 

    // INSTRUCTIONS: 
    // Please refer to: http://developer.gnome.org/glib/2.30/glib-Double-ended-Queues.html 
    // 1. Open "people.csv". 
    // 2. Read in the list of people (format is "firstName lastName,age,{male|female}"). 
    // 3. Create a "Person" for each one read. 
    // 4. Insert each "Person" into a GLib GQueue (this is a "double-ended queue", but think of it like a list). 
    // 5. Sort the list (by using "g_queue_sort") by: 
    //  1. Gender 
    //  2. Last name 
    // 6. Print out the list (by using "g_queue_foreach"). The format should be: 
    //  (male/female) Last name, First name (age) 
    // 7. Free up all memory (we're gonna valgrind this afterward). 


// Ready for the bonus? 
    bonusFunction(); 

    // KTHXBYE 
    return(0); 
} 

/** This is the bonus function. 
    **/ 
void bonusFunction() { 
    //! This is the bonus array! 
    int arrayOfInts[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; 


    // BONUS! 
    // 1. Loop through the bonus array and print it without using square brackets ("[" and "]"). 


    // All done. 
    return; 
} 

내 솔루션은가, 나 성별이나 다른 최적화 기준으로 정렬하는 방법을 알려 주시기 바랍니다 내 솔루션 무슨이다.

// Get gendor 
    token = strtok(NULL,",\n"); 
    if(strcasecmp(token, "female") == 0) 
     ptr_one->gender = FEMALE; 
    else 
     ptr_one->gender = MALE; 

그런 다음 적절한 비교 함수를 사용합니다 :

// Programming Challenge! 

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <glib.h> 

//! This is the sex of a human. 
typedef enum { MALE, FEMALE } Gender; 

//! This is a human person. 
typedef struct Person { 
    //! The given name. 
    char* firstName; 
    //! The family name. 
    char* lastName; 
    //! The age, in calendar years. 
    int age; 
    //! The sex (see above). 
    Gender gender; 
} Person; 

/** This is the bonus function. 
    **/ 
void bonusFunction(); 

gint sort_lastName(gconstpointer a, gconstpointer b, gpointer data) { 
    return strcmp(((Person*)a)->lastName, ((Person*)b)->lastName); 
} 
void prt(gpointer per) { 
printf("%s, %s %d \n ", ((Person*)per)->lastName, ((Person*)per)->firstName,((Person*)per)->age); 
} 

/** This is the core of the program. 
    **/ 
int main(int argc, char** argv) { 
    puts("Starting SevOne Test\n"); 
    // INSTRUCTIONS: 
    // Please refer to: http://developer.gnome.org/glib/2.30/glib-Double-ended-Queues.html 
    // 1. Open "people.csv". 
    // 2. Read in the list of people (format is "firstName lastName,age,{male|female}"). 
    // 3. Create a "Person" for each one read. 
    // 4. Insert each "Person" into a GLib GQueue (this is a "double-ended queue", but think of it like a list). 
    // 5. Sort the list (by using "g_queue_sort") by: 
    //  1. Gender 
    //  2. Last name 
    // 6. Print out the list (by using "g_queue_foreach"). The format should be: 
    //  (male/female) Last name, First name (age) 
    // 7. Free up all memory (we're gonna valgrind this afterward). 


    char buffer[100]; 
    int counter=0; 
    char * token; 
    GQueue* q = g_queue_new(); 

    FILE *fp; 
    fp=fopen("people.csv", "r"); 

    if(fp == NULL) 
    { 
     puts("Failed to open file"); 
     return 0; 
    } 



    while(fgets(buffer, sizeof(buffer), fp) != NULL) 
    { 
     Person *ptr_one; 
     ptr_one = (Person *) malloc (sizeof(Person)); 

     // Get first name 
     token = strtok(buffer," "); 
     ptr_one->firstName=(char *)malloc(sizeof(char)*sizeof(token)); 
     strcpy(ptr_one->firstName,token); 

     // Get last name 
     token = strtok(NULL,","); 
     ptr_one->lastName=(char *)malloc(sizeof(char)*sizeof(token)); 
     strcpy(ptr_one->lastName,token); 

     // Get age 
     token = strtok(NULL, ","); 
     ptr_one->age=(int *)malloc(sizeof(int)); 
     sscanf (token, "%d", &ptr_one->age); 

     // Get gender 
     token = strtok(NULL,",\n"); 


     g_queue_push_tail(q, ptr_one); 

    } 

    // Sort list by last name 
    g_queue_sort(q, (GCompareDataFunc)sort_lastName, NULL); 

    // print the list 
    g_queue_foreach(q, (GFunc)prt, NULL); 
    if(fclose(fp) != 0) 
    { 
     puts("Failed to close file."); /* prints !!!Hello World!!! */ 
     return 0; 
    } 
    g_queue_free(q); 




// Ready for the bonus? 
    bonusFunction(); 

    // KTHXBYE 
    return(0); 
} 

/** This is the bonus function. 
    **/ 
void bonusFunction() { 
    //! This is the bonus array! 
    int arrayOfInts[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; 


    // BONUS! 
    // 1. Loop through the bonus array and print it without using square brackets ("[" and "]"). 


    // All done. 
    return; 
} 

그리고

Brad Fawcett,24,male 
Steve Settlemyre,29,male 
Dave Hegenbarth,44,male 
Cathy Colapiertro,41,female 
Steve Mahoney,23,male 
Dave Mulford,26,male 
Doug Manley,24,male 
Steve Carrington,24,male 
Lauren Jordan,31,female 
Tanya Bakalov,26,female 

답변

1

먼저 아래의 CSV 제대로 성별을 분석 할 필요가

gint sort_gender(gconstpointer a, gconstpointer b, gpointer data) { 
    return ((Person*)a)->gender - ((Person*)b)->gender; 
} 

을 그리고 보너스 기능 :

void bonusFunction() { 
    //! This is the bonus array! 
    int arrayOfInts[] = { 1, 1, 2, 3, 5, 8, 13, 21, 34 }; 

    // BONUS! 
    // 1. Loop through the bonus array and print it without using square brackets ("[" and "]"). 
    int i; 
    int *n = arrayOfInts; 
    for(i = 0; i < sizeof(arrayOfInts)/sizeof(int); i++) 
     printf("%d\n", *n++); 

    return; 
}