Couting unique elements, with a peculiar definition of equality.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "elements.h" | |
#include <stddef.h> | |
#include <stdbool.h> | |
static int compare(const element *a, const element *b) | |
{ | |
if (a->e2 == b->e2) | |
return 0; | |
else if (a->e1 != b->e1) | |
return b->e1 - a->e1; | |
return b->e2 - a->e2; | |
} | |
size_t count_unique(const element *s) | |
{ | |
size_t c = 0; | |
bool found; | |
for (size_t i = 0; s[i].s != NULL; ++i) { | |
found = false; | |
for (size_t j = 0; j < i; ++j) { | |
if (compare(&s[i], &s[j]) == 0) { | |
found = true; | |
break; | |
} | |
} | |
if (!found) | |
c++; | |
} | |
return c; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ELEMENTS_H | |
#define ELEMENTS_H | |
#include <stddef.h> | |
typedef struct | |
{ | |
const char *s; | |
int e1; | |
int e2; | |
} element; | |
size_t count_unique(const element *s); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment