Skip to content

Commit bd208f0

Browse files
committed
charFreq is almost done.
1 parent 3207811 commit bd208f0

File tree

4 files changed

+308
-30
lines changed

4 files changed

+308
-30
lines changed

biggest_seq.c

+97-25
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void printer(NODE *head)
4444
printf("Fim da Lista!\n");
4545
}
4646

47-
NODE *search(NODE *head, int item)
47+
NODE *search(NODE *head, char item)
4848
{
4949
// printf("to dentro do search\n");
5050
while (head != NULL)
@@ -59,58 +59,130 @@ NODE *search(NODE *head, int item)
5959
return NULL;
6060
}
6161

62-
int search_2(NODE *head, int item)//função para procurar a melhor sequencia
62+
int search_2(NODE *head, char item)//função para procurar a melhor sequencia
6363
{
64+
DEBUG printf("######### Procurando por: [%c] ##########\n", item);
6465
int count = 0;
6566
while (head != NULL)
6667
{
68+
DEBUG printf("head->item = [%c]\t",head->item);
6769
if (head->item == item)
6870
{
69-
// printf("ENCONTREI MAIS UM!!! [%d]\n", item);
71+
DEBUG printf("checagem: [%d]\n",count);
7072
return count; //retorna o endereço que contém o item
7173
}
74+
7275
head = head->next;
73-
count++;
76+
count = count+1;
77+
DEBUG printf("voltas: [%d]\n",count);
7478
}
7579
return 0;
7680
}
7781

78-
void func(NODE *head, int size, int beg, int end)
82+
int search_3(NODE *head, char item)//devolve a contagem até o fim, nao importa oq for
7983
{
80-
NODE* first_zero = search(head,0);//recebo o endereço do primeiro zero.
81-
beg = search_2(head,0);
82-
int size1 = search_2(first_zero,1); //a partir do endereço do primeiro zero, procuro o proximo 1 e recebo o tamanho deste intervalo
83-
end = size1
84-
if(first_zero == NULL)//break
84+
//DEBUG printf("######### Procurando por: [%c] ##########\n", item);
85+
int count = 0;
86+
while (head != NULL)
8587
{
86-
printf("0 0\n");
87-
return;
88+
//DEBUG printf("head->item = [%c]\t",head->item);
89+
if (head->item == item)
90+
{
91+
count++;
92+
//DEBUG printf("ENCONTREI MAIS UM!!! [%d]\n", item);
93+
//DEBUG printf("voltas: [%d]\n",count);
94+
return count; //retorna o endereço que contém o item
95+
}
96+
head = head->next;
97+
count++;
98+
//DEBUG printf("voltas: [%d]\n",count);
8899
}
89-
NODE* first_one = search(first_zero, 1);
100+
return count;
90101
}
91-
int main()
102+
void func(NODE *head, int size, int beg, int end)//beg e end são o começo e o fim da melhor sequencia
92103
{
93-
char input[MAX];
94-
scanf("%s", input);
95-
int q = strlen(input);
96-
NODE *lista1 = create_list(); //nó que aponta para nulo
104+
NODE* first_zero = search(head,'0');//recebo o endereço do primeiro zero.
105+
int pos_inicial = search_2(head,'0')+beg;//recebe a posição do primeiro zero
106+
DEBUG printf("\tComeço encontrado: [%d]\n",pos_inicial);
97107

98-
for (int i = 0; i < q; i++)
108+
NODE* first_one = search(first_zero,'1'); //will be my new head
109+
int size1 = search_2(first_zero, '1');
110+
int pos_final = size1+pos_inicial-1; //posição final do primeiro intervalo
111+
// if(first_one == NULL)
112+
// {
113+
// pos_final = pos_inicial + search_3(first_zero,'1')-1;
114+
// size1 = pos_final - pos_inicial;
115+
// }
116+
// else
117+
// {
118+
// size1 = search_2(first_zero,'1')-1; //a partir do endereço do primeiro zero, procuro o proximo 1 e recebo o tamanho deste intervalo
119+
// pos_final = pos_inicial+size1-1;//end é a posição final
120+
// }
121+
DEBUG printf("\tFim encontrado: [%d]\n",pos_final);
122+
DEBUG printf("TAMANHO: [%d]\n",size1);
123+
124+
if(first_zero == NULL || first_one == NULL)//break
99125
{
100-
lista1 = add(lista1, input[i]);
101-
126+
if (first_zero == NULL)
127+
{
128+
printf("%d %d\n",pos_inicial,pos_final);
129+
}
130+
printf("%d %d\n",pos_inicial, pos_final);
131+
return;
102132
}
133+
134+
if (size1>size)
135+
{
136+
DEBUG printf("\n------- FUNC 1 --------\n");
137+
func(first_one, size1, pos_inicial, pos_final);
138+
}
139+
else{
140+
DEBUG printf("\n------- FUNC 2 --------\n\n");
141+
func(first_zero,size,beg,end);
142+
}
143+
144+
}
145+
// void func2 (NODE* head)
146+
// {
147+
// int size;
148+
// int pos = 0; //posição da iteração
149+
// int beg = 0;
150+
// int end = 0;
103151

104-
func(lista1);
152+
// int state = -1;
153+
154+
// NODE* temp;
155+
// while (head != NULL)
156+
// {
157+
// if(head->item == '0')
158+
// {
159+
// state = 0;
160+
// beg = pos;
161+
// }
162+
// if(head->next->item == '1')
163+
// {
164+
165+
// }
166+
167+
// pos++
168+
// }
169+
170+
// }
171+
int main()
172+
{
173+
char input[MAX];
174+
NODE *lista1 = create_list(); //nó que aponta para nulo
105175
do
106176
{
107177
scanf("%s", input);
108178
int q = strlen(input);
109-
110-
for (int i = 0; i < q; i++)
179+
if(q == 1 && input[0] == '0') break;
180+
for (int i = q-1; i >= 0 ; i--)
111181
{
112-
lista1 = add(lista1, input[i]);
182+
//DEBUG printf("Adicionado [%c].\n",input[i]);
183+
lista1 = add(lista1, input[i]);
113184
}
185+
func(lista1, 0,0,0);
114186
} while (1);
115187

116188

charFreq.c

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdio.h>
22
#include <string.h>
33
#include <stdlib.h>
4-
#define debug if(0)
4+
#define debug if(1)
55

66
typedef struct node{
77
char a;
@@ -25,7 +25,7 @@ node* add(node* head, char item, int freq)
2525
return new_node;
2626
}
2727

28-
void printer(node*head)
28+
void printer(node* head)
2929
{
3030
while (head != NULL)
3131
{
@@ -45,6 +45,7 @@ node* search(node* head, char data)
4545
}
4646
return NULL;
4747
}
48+
4849
int main()
4950
{
5051
char str[1000];
@@ -54,6 +55,9 @@ int main()
5455
char* pch;
5556
int freq;
5657
node* lista = init();
58+
59+
int ch[256]; //index == ascii, value == freq
60+
5761
for (int i = 0; i < max ; i++)
5862
{
5963
freq = 0;
@@ -66,10 +70,22 @@ int main()
6670
}
6771
}
6872
debug printf("[%c] [%d]\n",str[i],freq);
69-
if (search(lista,tmp) == NULL)
73+
if (ch[(int)str[i]] == 0) //adds frequency values to the correspondent ascii index
74+
{
75+
//TODO #3 it's not computing the char ' ' or ',' or '.'
76+
ch[(int)str[i]] = freq;
77+
debug printf("\t\tch[%d] = %d\n",(int)str[i],ch[(int)str[i]]);
78+
}
79+
80+
}
81+
82+
for (int aux = 0; aux < max ; aux++)
83+
{
84+
if (ch[str[aux]] != 0)
7085
{
71-
lista = add(lista,tmp,freq);
72-
}
86+
lista = add(lista,(char)aux,freq);
87+
}
88+
7389
}
7490
printer(lista);
7591
return 0;

crescent_list.c

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//https://thehuxley.com/problem/266?quizId=6096
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <stdlib.h>
5+
6+
#define DEBUG if (0)
7+
#define MAX 100001
8+
9+
typedef struct node
10+
{
11+
int item;
12+
struct node *next;
13+
}NODE;
14+
15+
NODE * create_list()
16+
{
17+
return NULL;
18+
}
19+
NODE *add(NODE *head, int item)
20+
{
21+
NODE *new_node = (NODE *)malloc(sizeof(NODE));
22+
new_node->item = item;
23+
new_node->next = head;
24+
return new_node;
25+
}
26+
int is_empty(NODE *head)
27+
{
28+
return (head == NULL);
29+
}
30+
NODE* bubbleSort(NODE* head, int num)
31+
{
32+
NODE* pos;
33+
NODE* tmp = NULL;
34+
int swap,t;
35+
36+
if (is_empty(head))
37+
{
38+
return NULL;
39+
}
40+
41+
do
42+
{
43+
DEBUG printf("COMEÇAAAAAAAAAAAAA\n");
44+
swap = 0;
45+
pos = head;
46+
47+
// if (pos->next == NULL)
48+
// {
49+
// printf("FIM\n");
50+
// continue;
51+
// }
52+
DEBUG printf("o proximo nao é nulo\n");
53+
54+
while(pos->next != tmp)
55+
{
56+
if(pos->item > pos->next->item)
57+
{
58+
//tmp = pos; // isso aqui copia os structs ou só copia os endereços?
59+
60+
DEBUG printf("%d > %d\n",pos->item , pos->next->item);
61+
t = pos->item;
62+
pos->item = pos->next->item;
63+
pos->next->item = t;
64+
65+
swap = 1;
66+
67+
68+
DEBUG printf("swap rolando \t");
69+
}
70+
pos = pos->next;
71+
DEBUG printf("analisando o proximo\n");
72+
}
73+
tmp = pos;
74+
}while(swap);
75+
76+
return pos;
77+
}
78+
void printer(NODE *head)
79+
{
80+
if (head == NULL)
81+
{
82+
DEBUG printf("Lista vazia!\n");
83+
}
84+
while (head != NULL)
85+
{
86+
printf("[%d]->", head->item);
87+
head = head->next;
88+
}
89+
printf("\n");
90+
DEBUG printf("\nFim da Lista!\n");
91+
}
92+
int main()
93+
{
94+
NODE* lista = create_list();
95+
int n;
96+
int i = 0;
97+
while (scanf("%d",&n)!= EOF)
98+
{
99+
lista = add(lista,n);
100+
DEBUG printf("Elemento %d adicionado\n",n);
101+
i++;
102+
}
103+
order(lista, i);
104+
//bubbleSort(lista);
105+
printer(lista);
106+
return 0;
107+
}

0 commit comments

Comments
 (0)