01/10/2018, 14:07
Hỏi về các test ẩn của bài CountAPI trên Codefight
Dear all,
Moi nguời đã từng làm bài countAPI trên CodeFiligt cho mình hỏi các test case ẩn còn lại là như thế nào mình không tìm đuợc lỗi.
Đây là đề bài

Còn đây là code của mình:
struct listmod{
int size;
char name[64];
struct listmod * next;
};
struct listsub{
int size;
char name[64];
struct listmod * h_mod;
struct listmod * t_mod;
struct listsub * next;
};
struct listAPI{
int size;
char name[64];
struct listsub * h_sub;
struct listsub * t_sub;
struct listAPI * next;
}*pro_head = NULL, *pro_tail = NULL;
struct listAPI* find_pro_name(char * name)
{
struct listAPI * api = pro_head;
while(api)
{
if(strcmp(name, api->name) == 0)
{
api->size += 1;
return api;
}
api = api->next;
}
return NULL;
}
struct listsub* find_sub_name(struct listsub * list_sub, char * sub_name)
{
struct listsub * sub = list_sub;
while(sub)
{
if(strcmp(sub_name,sub->name) == 0)
{
sub->size += 1;
return sub;
}
sub = sub->next;
}
return NULL;
}
struct listmod* find_mod_name(struct listmod * list_mod, char * mod_name)
{
struct listmod * mod = list_mod;
while(mod)
{
if(strcmp(mod_name,mod->name) == 0)
{
mod->size += 1;
return mod;
}
mod = mod->next;
}
return NULL;
}
struct listAPI* insert_pro(char * name)
{
struct listAPI * new_api = (struct listAPI*)calloc(1,sizeof(struct listAPI)*sizeof(char));
strncpy(new_api->name,name,strlen(name));
new_api->size +=1;
if(pro_head == NULL)
{
pro_head = new_api;
}
else
{
pro_tail->next = new_api;
}
pro_tail = new_api;
return new_api;
}
struct listsub* insert_sub(struct listAPI * api, char * name)
{
struct listsub * new_sub = (struct listsub *)calloc(1,sizeof(struct listsub)*sizeof(char));
strncpy(new_sub->name,name,strlen(name));
new_sub->size += 1;
if(api->h_sub == NULL)
{
api->h_sub = new_sub;
}
else
{
api->t_sub->next = new_sub;
}
api->t_sub = new_sub;
return new_sub;
}
struct listmod* insert_mod(struct listsub * sub, char * name)
{
struct listmod * new_mod = (struct listmod *)calloc(1,sizeof(struct listmod)*sizeof(char));
strncpy(new_mod->name,name,strlen(name));
new_mod->size += 1;
if(sub->h_mod == NULL)
{
sub->h_mod = new_mod;
}
else
{
sub->t_mod->next = new_mod;
}
sub->t_mod = new_mod;
return new_mod;
}
#define skip_white_spaces(pos)
({
__typeof__(pos) _p = (pos);
for ( ; isspace(*_p); _p++)
;
_p;
})
char *trimwhitespace(char *str)
{
char *end;
// Trim leading space
while(isspace((unsigned char)*str)) str++;
if(*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace((unsigned char)*end)) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
arr_string countAPI(arr_string calls) {
int i;
arr_string res = alloc_arr_string(100);
for(i = 0; i< 100;i++)
{
res.arr[i] = (char*)calloc(1,64);
}
for (i = 0; i < calls.size; i++)
{
char new_str[64] = {0};
strncpy(new_str,skip_white_spaces(calls.arr[i]),strlen(calls.arr[i]));
char *pro_name = trimwhitespace(strtok(new_str, "/"));
char *sub_name = trimwhitespace(strtok(NULL,"/"));
char *mod_name = trimwhitespace(strtok(NULL,"/"));
struct listAPI * api = find_pro_name(pro_name);
if(api)
{
/* Find subproject*/
struct listsub * sub = find_sub_name(api->h_sub,sub_name);
if(sub)
{
/* Find modproject*/
struct listmod * mod = find_mod_name(sub->h_mod,mod_name);
if(!mod)
{
insert_mod(sub,mod_name);
}
}
else
{
struct listsub* lsub = insert_sub(api,sub_name);
insert_mod(lsub,mod_name);
}
}
else
{
struct listAPI * pro = insert_pro(pro_name);
struct listsub * lsub = insert_sub(pro,sub_name);
insert_mod(lsub,mod_name);
}
}
i = 0;
/*Update to res*/
struct listAPI * api = pro_head;
while(api)
{
sprintf(res.arr[i++],"--%s (%d)",api->name,api->size);
struct listsub * sub = api->h_sub;
while(sub)
{
sprintf(res.arr[i++],"----%s (%d)",sub->name,sub->size);
struct listmod * mod = sub->h_mod;
while(mod)
{
sprintf(res.arr[i++],"------%s (%d)",mod->name, mod->size);
sub->h_mod = sub->h_mod->next;
free(mod);
mod = sub->h_mod;
}
api->h_sub = api->h_sub->next;
free(sub);
sub = api->h_sub;
}
pro_head = pro_head->next;
free(api);
api = pro_head;
}
res.size = i;
return res;
}
Thanks all!
Bài liên quan