-
Notifications
You must be signed in to change notification settings - Fork 1
/
matchlab.c
320 lines (277 loc) · 8.6 KB
/
matchlab.c
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// adam rosenberg
// Objective here is to recognize some regular expressions
int aflag = 0;
int bflag = 0;
int cflag = 0;
int tflag = 0;
void doaflag(char buffer[]){
/**
* a flag
*
* between 4 and 5 repetitions (inclusive) of the letter “k”;
3 or more repetitions of the letter “v”; and
an odd number of uppercase letters.
*/
const int length = strlen(buffer);
int kcount = 0; //k = 107
int vcount = 0; //v = 188
int uppercount = 0; //capitol is 65-90
int othercount = 0;
int order = 1;
int i;
for(i = 0; i < length; i ++){
if(buffer[i] == 107){
kcount++;
}else if(buffer[i] == 118){
vcount++;
}else if(buffer[i] <= 90 && buffer[i] >=65){
uppercount++;
}else{
othercount++;
}
}
if(buffer[0] != 'k')
order = 0;
if(buffer[4] == 'k' && !(buffer[5] == 'v'))
order = 0;
if(buffer[3] == 'k' && !(buffer[4] == 'v'))
order = 0;
if(!(buffer[vcount+kcount] >= 'A' && buffer[vcount+kcount] <= 'Z'))
order = 0;
if((kcount == 4 || kcount == 5) && vcount >=3 && ((uppercount&1)==1) && othercount == 0 && order){
if(tflag){
char first = buffer[0];
for(i = 0; i < length; i++){
buffer[i] = first;
//if t flag...print.
printf("%c", buffer[i]);
}
printf("\n");
}else{
printf("yes\n");
}
}
else{
if(!tflag){
printf("no\n");
}
}
}
void dobflag(char buffer[]){
/**
* any even number (including zero) repetitions of the letter “h”;
between 1 and 3 (inclusive) decimal digits — call this sequence X;
between 3 and 6 repetitions (inclusive) of the letter “n”;
the same character sequence as X, but reversed; and
an odd number of uppercase letters.
For matches, perform the following conversion:
add one “E” before each “G”.
*/
const int length = strlen(buffer);
int order = 1;
int hcount = 0; //h = 104
int deccount = 0; // 48-57 is a num 0-9
int numbers[6];
int ncount = 0; //n = 110
int isdecreversed = 0; //0 false 1 true
int uppercount = 0; //upper is 65-90
int othercount=0;
int i;
for (i = 0; i < length; i++) {
if (buffer[i] == 104) {
hcount++;
} else if (buffer[i] == 110) {
ncount++;
} else if (buffer[i] <= 90 && buffer[i] >= 65) {
uppercount++;
} else if (buffer[i] >= 48 && buffer[i] <= 57) {
if(deccount < 6){
numbers[deccount] = buffer[i];
}
deccount++;
//add constraint..maybe? lol.
}else{
othercount++;
}
}
if(hcount == 0 && !(buffer[0]>= 48 && buffer[0] <= 57 )){
order = 0;
}
if(!(buffer[length-1] >= 'A' && buffer[length-1] <= 'Z'))
order = 0;
if (deccount == 2) {
if(buffer[hcount+1]!='n')
order = 0;
if (numbers[0] == numbers[1])
isdecreversed = 1;
} else if (deccount == 4) {
if(buffer[hcount+2] != 'n')
order = 0;
if (numbers[0] == numbers[3] && numbers[1] == numbers[2])
isdecreversed = 1;
} else if (deccount == 6) {
if(buffer[hcount + 3] != 'n')
order = 0;
if (numbers[0] == numbers[5] && numbers[1] == numbers[4] && numbers[2] == numbers[3])
isdecreversed = 1;
} else {
isdecreversed = 0;
}
// printf("is reversed %d, h count %d, n count %d, uppercount %d, othercount %d, deccount %d, order %d \n", isdecreversed, hcount, ncount, uppercount, othercount, deccount, order);
if((isdecreversed == 1) && (hcount%2==0 || hcount == 0) && (ncount > 2 && ncount < 7) && (uppercount%2 == 1) && othercount==0 && order ){
int extra = 0;
if(tflag){
for(i = 0 ; i < length+extra; i++){
if(buffer[i] == 71){
printf("E");
}
printf("%c", buffer[i]);
}
printf("\n");
}else{
printf("yes\n");
}
}else{
if(!tflag){
printf("no\n");
}
}
}
void docflag(char buffer[]){
/**
* any odd number of repetitions of the letter “a”;
between 1 and 3 (inclusive) decimal digits — call this sequence X;
any odd number of repetitions of the letter “y”;
an odd number of uppercase letters; and
the same characters as the odd-positioned characters in X.
For matches, perform the following conversion:
remove every “C” and “G”.
*/
const int length = strlen(buffer);
int acount = 0; // 97
int deccount = 0; //48-57
int ycount = 0; //121
int uppercount = 0; //65-90
int numbers[5];
int othercount = 0;
int isdecodd=0;
int startsatodd = 0;
int order = 1;
int i;
for (i = 0; i < length; i++) {
if (buffer[i] == 97) {
acount++;
} else if (buffer[i] == 121) {
ycount++;
} else if (buffer[i] <= 90 && buffer[i] >= 65) {
uppercount++;
} else if (buffer[i] >= 48 && buffer[i] <= 57) {
if(deccount < 5){
numbers[deccount] = buffer[i];
// printf("deccount = %d, numbers[deccount] = %d, buffer[%d] = %d\n", deccount, numbers[deccount], i, buffer[i]);
deccount++;
}
}else{
othercount++;
}
}
/**
* any odd number of repetitions of the letter “a”;
between 1 and 3 (inclusive) decimal digits — call this sequence X;
any odd number of repetitions of the letter “y”;
an odd number of uppercase letters; and
the same characters as the odd-positioned characters in X.
For matches, perform the following conversion:
remove every “C” and “G”.
*/
if(buffer[0] != 'a'){
order = 0;
}
if(!(buffer[acount] >= 48 && buffer[acount] <= 57))
order = 0;
if(!(buffer[length-1] >= 48 && buffer[length-1] <= 57))
order = 0;
for(i = 0 ; i < 5; i++) {
if (deccount == 2) { //len 1
isdecodd = 0;
} else if (deccount == 3) { //len 2
if (numbers[1] == numbers[2]){
isdecodd = 1;
}
} else if (deccount == 4) { //len 3
if (numbers[1] == numbers[3])
isdecodd = 1;
} else {
isdecodd = 0;
}
}
// printf("is odd %d, a count %d, y count %d, uppercount %d, othercount %d, deccount %d \n", isdecodd, acount,
// ycount, uppercount, othercount, deccount);
if ((acount & 1) == 1 && isdecodd == 1 && ((ycount & 1) == 1) && (uppercount & 1) == 1 && othercount == 0 && order) {
if(tflag){
for(i =0; i < length; i++){
if(buffer[i] != 'C' && buffer[i] != 'G')
printf("%c", buffer[i]);
}
printf("\n");
}
else{
printf("yes\n");
}
//adapt this for odd positions of x...
} else {
if(tflag){
}else{
printf("no\n");
}
}
}
//driver method to call the correct methods for each string. (char array)
void dowork(char buffer[]){
const int length = strlen(buffer);
if(aflag)
doaflag(buffer);
else if(bflag)
dobflag(buffer);
else if(cflag)
docflag(buffer);
else{
doaflag(buffer);
}
}
//main method. parses for flags, then passes each 'string' to a driver method that calls the right method based on active flags.
int main (int argc, char *argv[])
{
int count = 0;
char buffer[100]; //malloc
int i;
for(i = 0; i < 100; i ++){
buffer[i]=' ';
}
// //searching for a dash and finding the flags after that..
for(i = 1; i < argc; i++){
if(argv[i][0] == '-'){ //str[0] = argv...
char argument = argv[i][1];
switch (argument) {
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cflag = 1;
break;
case 't':
tflag = 1;
break;
}
}else{
dowork(argv[i]); //pass each element of buffer call alg here
}
}
return 0;
}