3280
Posted: Wed Apr 13, 2011 10:49 pm
Anyone did it in a better way?!?! SUre you did!
Hi guys, this is pretty much my first post here! Found this site yesterday, and made a lot of the challenge in some hours!
I did the RFC 2280 one like that:
On KDE Kate i replaced all spaces for break lines, and got a line by line text file, then removed all the garbage, like " / , . ; : { } and others, then put it all in lowercase, easy stuff, i made this for the other ones befores this challenge as well
Then i made this "MacGyver Oriented Program", it don't accurate count the times(as i tested it with cat wordlist | grep -c answer), but it gave the answer, that's what i needed, so i'll think why it don't give the accurate count later, or you could point it to me!
Well... here it is.. MacGyver Style C programming!
(I'm brazilian, sorry if the variables/comments don't make any sense...)
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
#define MAXPALAVRA 99000
//alterar esses defines abaixo para o seu uso
#define ARQUIVO "wordlist" //o nome do arquivo, tem que estar na mesma pasta
#define TAMPALAVRA 9 //define o tamanho em caracteres da palavra
typedef struct
{
char palavra[SIZE];
int count;
}WORD;
void criaPalavra(WORD *novaPal, char* word)
{
strcpy(novaPal->palavra, word);
novaPal->count=0;
}
//cria uma lista com a palavra e um contador
typedef struct
{
WORD lista[MAXPALAVRA];
int len;
}LISTADEPALAVRAS;
void addPalavra(LISTADEPALAVRAS *list, WORD pal, int posi)
{
list->lista[posi] = pal;
list->len = 0;
}
void maisAparece(LISTADEPALAVRAS *list)
{
WORD atual, comparada, maisAparece;
int i = 0,
j = 0;
while (i < list->len)
{
atual = list->lista;
j = i + 1;
while(j < list->len)
{
comparada=list->lista[j];
if (!strcmp(atual.palavra,comparada.palavra))
{
list->lista[j].count++;
}
j++;
}
i++;
}
maisAparece = list->lista[0];
i = 1;
while (i < list->len)
{
atual = list->lista;
if (atual.count > maisAparece.count)
{
maisAparece = atual;
}
i++;
}
printf("%d - %s",maisAparece.count,maisAparece.palavra);
}
//cria uma lista com a lista anterior
main()
{
FILE *arquivo;
int index = 0;
LISTADEPALAVRAS listap;
arquivo=fopen(ARQUIVO,"r");
while (!feof(arquivo))
{
char lida[100] = "";
fscanf(arquivo, "%s\n", &lida);
if(strlen(lida) == TAMPALAVRA)
{
WORD pal;
criaPalavra(&pal, lida);
addPalavra(&listap, pal, index);
//printf("%d - %s\n",index, listap.lista[index].palavra);
index++;
}
}
listap.len = --index;
maisAparece(&listap);
//printf("%d",index);
}
Hi guys, this is pretty much my first post here! Found this site yesterday, and made a lot of the challenge in some hours!
I did the RFC 2280 one like that:
On KDE Kate i replaced all spaces for break lines, and got a line by line text file, then removed all the garbage, like " / , . ; : { } and others, then put it all in lowercase, easy stuff, i made this for the other ones befores this challenge as well
Then i made this "MacGyver Oriented Program", it don't accurate count the times(as i tested it with cat wordlist | grep -c answer), but it gave the answer, that's what i needed, so i'll think why it don't give the accurate count later, or you could point it to me!
Well... here it is.. MacGyver Style C programming!
(I'm brazilian, sorry if the variables/comments don't make any sense...)
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
#define MAXPALAVRA 99000
//alterar esses defines abaixo para o seu uso
#define ARQUIVO "wordlist" //o nome do arquivo, tem que estar na mesma pasta
#define TAMPALAVRA 9 //define o tamanho em caracteres da palavra
typedef struct
{
char palavra[SIZE];
int count;
}WORD;
void criaPalavra(WORD *novaPal, char* word)
{
strcpy(novaPal->palavra, word);
novaPal->count=0;
}
//cria uma lista com a palavra e um contador
typedef struct
{
WORD lista[MAXPALAVRA];
int len;
}LISTADEPALAVRAS;
void addPalavra(LISTADEPALAVRAS *list, WORD pal, int posi)
{
list->lista[posi] = pal;
list->len = 0;
}
void maisAparece(LISTADEPALAVRAS *list)
{
WORD atual, comparada, maisAparece;
int i = 0,
j = 0;
while (i < list->len)
{
atual = list->lista;
j = i + 1;
while(j < list->len)
{
comparada=list->lista[j];
if (!strcmp(atual.palavra,comparada.palavra))
{
list->lista[j].count++;
}
j++;
}
i++;
}
maisAparece = list->lista[0];
i = 1;
while (i < list->len)
{
atual = list->lista;
if (atual.count > maisAparece.count)
{
maisAparece = atual;
}
i++;
}
printf("%d - %s",maisAparece.count,maisAparece.palavra);
}
//cria uma lista com a lista anterior
main()
{
FILE *arquivo;
int index = 0;
LISTADEPALAVRAS listap;
arquivo=fopen(ARQUIVO,"r");
while (!feof(arquivo))
{
char lida[100] = "";
fscanf(arquivo, "%s\n", &lida);
if(strlen(lida) == TAMPALAVRA)
{
WORD pal;
criaPalavra(&pal, lida);
addPalavra(&listap, pal, index);
//printf("%d - %s\n",index, listap.lista[index].palavra);
index++;
}
}
listap.len = --index;
maisAparece(&listap);
//printf("%d",index);
}