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
164
165
166
167
168
169
170
171
172
173
174
175
176
|
//
// Task 1: Minimum Index Sum
//
// C version.
//
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "args.h"
#include "csvsplit.h"
typedef struct
{
char **arr; // array of 256 char *s
int nel;
} strlist;
void process1( char *str, void *extra )
{
strlist *e = (strlist *)extra;
char *s = strdup( str );
assert( s != NULL );
e->arr[ e->nel++ ] = s;
}
void print_strlist( strlist *list )
{
bool first = true;
for( int i=0; i<list->nel; i++ )
{
if( ! first ) putchar( ',' );
fputs( list->arr[i], stdout );
first = false;
}
}
// bool isin = in_strlist( word, &list );
bool in_strlist( char *word, strlist *list )
{
for( int i=0; i<list->nel; i++ )
{
if( strcmp( word, list->arr[i] ) == 0 )
{
return true;
}
}
return false;
}
// int sum = indexsum( word, &list1, &list2 );
// Calculate and return the index sum of word in the two lists.
int indexsum( char *word, strlist *list1, strlist *list2 )
{
int sum = 0;
for( int pos=0; pos < list1->nel; pos++ )
{
if( strcmp( list1->arr[pos], word ) == 0 )
{
sum += pos;
}
}
for( int pos=0; pos < list2->nel; pos++ )
{
if( strcmp( list2->arr[pos], word ) == 0 )
{
sum += pos;
}
}
return sum;
}
int main( int argc, char **argv )
{
int argno = process_flag_n_args( "mis", argc, argv,
2, "wordlist wordlist" );
strlist list1;
list1.arr = malloc( 256*sizeof(char *) );
assert( list1.arr != NULL );
list1.nel = 0;
csvForeach( argv[argno++], &process1, &list1 );
strlist list2;
list2.arr = malloc( 256*sizeof(char *) );
assert( list2.arr != NULL );
list2.nel = 0;
csvForeach( argv[argno], &process1, &list2 );
if( debug )
{
printf( "list1: " );
print_strlist( &list1 );
putchar( '\n' );
printf( "list2: " );
print_strlist( &list2 );
putchar( '\n' );
}
strlist common;
common.arr = malloc( 256*sizeof(char *) );
assert( common.arr != NULL );
common.nel = 0;
for( int i=0; i<list1.nel; i++ )
{
// look for list1.arr[i] in list2
if( in_strlist( list1.arr[i], &list2 ) )
{
common.arr[common.nel++] = list1.arr[i];
}
}
if( debug )
{
printf( "There are %d common strings: ", common.nel );
print_strlist( &common );
putchar( '\n' );
}
int mis = list1.nel + list2.nel;
strlist best;
best.arr = malloc( 256*sizeof(char *) );
assert( best.arr != NULL );
best.nel = 0;
for( int pos=0; pos<common.nel; pos++ )
{
char *w = common.arr[pos];
int s = indexsum( w, &list1, &list2 );
if( debug )
{
printf( "The index sum of %s is %d\n", w, s );
}
if( s < mis )
{
mis = s;
best.arr[0] = w;
best.nel = 1;
} else if( s == mis )
{
best.arr[best.nel++] = w;
}
}
putchar( '(' );
print_strlist( &best );
putchar( ')' );
putchar( '\n' );
for( int i=0; i<list1.nel; i++ )
{
free( list1.arr[i] );
}
free( list1.arr );
for( int i=0; i<list2.nel; i++ )
{
free( list2.arr[i] );
}
free( list2.arr );
free( common.arr );
free( best.arr );
return 0;
}
|