-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.c
More file actions
163 lines (155 loc) · 3.16 KB
/
solution.c
File metadata and controls
163 lines (155 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include "list.h"
#include "tree.h"
#include "solution.h"
#include "util.h"
void remove_every( List_t* head, size_t n )
{
if ( head == NULL )
return;
if ( n < 2u )
return;
if ( head->next == NULL )
return;
size_t ctr = 1;
List_t* ptr = head->next;
List_t* prev = head;
while ( ptr->next != NULL )
{
ctr++;
if ( ctr % n == 0u )
{
prev->next = ptr->next;
free( ptr );
ptr = prev->next;
continue;
}
prev = ptr;
ptr = ptr->next;
}
ctr++;
if ( ctr % n == 0u )
{
prev->next = NULL;
free( ptr );
ptr = NULL;
}
}
void print_min_max( uint32_t value )
{
uint32_t max = 0u;
uint32_t min = 0u;
if( value > 0 )
{
unsigned ctr = 0u;
const uint32_t bit = 1;
for( unsigned i = 0u; i < sizeof( value ) * 8u; i++ )
{
if( value & ( bit << i ) ) ctr++;
}
for( unsigned i = 0u; i < ctr; i++ )
{
min |= bit << i;
max |= bit << ( 31u - i );
}
}
printf( "min = %u\nmax = %u\n", min, max );
}
List_t* count_str( const char* str )
{
size_t len = strlen( str );
if( str == NULL || len == 0u )
return NULL;
List_t* head = NULL;
unsigned word_ctr = 0u;
for( size_t i = 0u; i <= len; i++ )
{
if( isprint( str[i] ) && isgraph( str[i] ) )
{
word_ctr++;
continue;
}
else
{
if( word_ctr == 0u )
continue;
str_sz_t ss;
memset( &ss, 0, sizeof( str_sz_t ) );
List_t* ptr = find_by_key( head, word_ctr );
if( ptr == NULL )
{
ss.key = word_ctr;
ss.value = 1;
uint32_t val = 0u;
memcpy( &val, &ss, sizeof( str_sz_t ) );
list_add( &head, val );
word_ctr = 0u;
continue;
}
else
{
str_sz_t* sptr = ( str_sz_t* )( &ptr->payload );
sptr->value++;
word_ctr = 0u;
continue;
}
}
}
return head;
}
int tree_height( const tree_node_t* root )
{
if( root == NULL )
return 0;
int l = 0, r = 0;
if( root->leftChild != NULL )
{
l = tree_height( root->leftChild );
}
else
{
l = -1;
}
if( root->rightChild != NULL )
{
r = tree_height( root->rightChild );
}
else
{
r = -1;
}
int max = l > r ? l : r;
return max + 1;
}
void get_primes( unsigned long N, unsigned long* buf )
{
if( buf == NULL )
return;
uint32_t idx = 1u;
buf[0] = 2u;
for( unsigned long n = 3u; n <= ULONG_MAX; n += 2u )
{
int prime = 1;
for( unsigned long m = 3u; m <= ( unsigned long )sqrt( n ); m += 2u )
{
if( n % m == 0u )
{
prime = 0;
break;
}
}
if( prime != 0 )
{
buf[idx] = n;
idx++;
if( idx >= N )
break;
}
}
}