Skip to content

Commit 5ae26e2

Browse files
Merge pull request #69 from Billsfriend/exercism-sync/fbfa2533f1309306
[Sync Iteration] c/etl/1
2 parents ec8af3a + 4985cf5 commit 5ae26e2

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

solutions/c/etl/1/etl.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "etl.h"
2+
#include <ctype.h>
3+
#include <stdlib.h>
4+
5+
int convert(const legacy_map *input, const size_t input_len, new_map **output) {
6+
size_t total_key = 0, key_count = 0;
7+
for (size_t i = 0; i < input_len; i++) {
8+
legacy_map temp = input[i];
9+
while (*temp.keys != '\0') {
10+
total_key++;
11+
temp.keys++;
12+
}
13+
}
14+
if (total_key == 0) {
15+
*output = NULL;
16+
return 0;
17+
}
18+
19+
*output = malloc(total_key * sizeof(new_map));
20+
if (*output == NULL) {
21+
return 0;
22+
}
23+
for (size_t i = 0; i < input_len; i++) {
24+
legacy_map temp = input[i];
25+
while (*temp.keys != '\0') {
26+
(*output)[key_count].key = tolower(*temp.keys);
27+
(*output)[key_count++].value = temp.value;
28+
temp.keys++;
29+
}
30+
}
31+
for (size_t i = 0; i < key_count-1; i++) {
32+
int swapped = 0;
33+
for (size_t j = 0; j < key_count-i-1; j++) {
34+
if ((*output)[j].key > (*output)[j+1].key) {
35+
new_map temp = (*output)[j];
36+
(*output)[j] = (*output)[j+1];
37+
(*output)[j+1] = temp;
38+
swapped = 1;
39+
}
40+
}
41+
if (swapped == 0) {
42+
break;
43+
}
44+
}
45+
return key_count;
46+
}

solutions/c/etl/1/etl.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef ETL_H
2+
#define ETL_H
3+
4+
#include <stddef.h>
5+
6+
typedef struct {
7+
int value;
8+
const char *keys;
9+
} legacy_map;
10+
11+
typedef struct {
12+
char key;
13+
int value;
14+
} new_map;
15+
16+
int convert(const legacy_map *input, const size_t input_len, new_map **output);
17+
18+
#endif

0 commit comments

Comments
 (0)