Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
52
Task/Symmetric-difference/C/symmetric-difference-1.c
Normal file
52
Task/Symmetric-difference/C/symmetric-difference-1.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
const char *A[] = { "John", "Serena", "Bob", "Mary", "Serena" };
|
||||
const char *B[] = { "Jim", "Mary", "John", "Jim", "Bob" };
|
||||
|
||||
#define LEN(x) sizeof(x)/sizeof(x[0])
|
||||
|
||||
/* null duplicate items */
|
||||
void uniq(const char *x[], int len)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < len; i++)
|
||||
for (j = i + 1; j < len; j++)
|
||||
if (x[j] && x[i] && !strcmp(x[i], x[j])) x[j] = 0;
|
||||
}
|
||||
|
||||
int in_set(const char *const x[], int len, const char *match)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < len; i++)
|
||||
if (x[i] && !strcmp(x[i], match))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* x - y */
|
||||
void show_diff(const char *const x[], int lenx, const char *const y[], int leny)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < lenx; i++)
|
||||
if (x[i] && !in_set(y, leny, x[i]))
|
||||
printf(" %s\n", x[i]);
|
||||
}
|
||||
|
||||
/* X ^ Y */
|
||||
void show_sym_diff(const char *const x[], int lenx, const char *const y[], int leny)
|
||||
{
|
||||
show_diff(x, lenx, y, leny);
|
||||
show_diff(y, leny, x, lenx);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
uniq(A, LEN(A));
|
||||
uniq(B, LEN(B));
|
||||
printf("A \\ B:\n"); show_diff(A, LEN(A), B, LEN(B));
|
||||
printf("\nB \\ A:\n"); show_diff(B, LEN(B), A, LEN(A));
|
||||
printf("\nA ^ B:\n"); show_sym_diff(A, LEN(A), B, LEN(B));
|
||||
|
||||
return 0;
|
||||
}
|
||||
135
Task/Symmetric-difference/C/symmetric-difference-2.c
Normal file
135
Task/Symmetric-difference/C/symmetric-difference-2.c
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
const char *mary="Mary";
|
||||
const char *bob="Bob";
|
||||
const char *jim="Jim";
|
||||
const char *john="John";
|
||||
const char *serena="Serena";
|
||||
|
||||
const char *setA[] = {john,bob,mary,serena};
|
||||
const char *setB[] = {jim,mary,john,bob};
|
||||
|
||||
#define XSET(j) j, (sizeof(j)/sizeof(*j))
|
||||
#define TALLOC(n,typ) malloc(n*sizeof(typ))
|
||||
|
||||
typedef enum {
|
||||
esdDIFFERENCE,
|
||||
esdSYMMETRIC } EsdFunction;
|
||||
/** * * * * * * * * * * * * * * * * * * * *
|
||||
* return value is difference or symmetric difference set
|
||||
* its size is returned in sym_size
|
||||
* f determinse whether it is a symmetric difference, or normal difference
|
||||
* * * * * * * * * * * * * * * * * * * * **/
|
||||
const char ** symmdiff( int *sym_size, EsdFunction f, const char *setA[], int setAsize, const char *setB[], int setBsize)
|
||||
{
|
||||
int union_size;
|
||||
int max_union_size;
|
||||
int diff_size;
|
||||
const char **union_set;
|
||||
const char **diff_set;
|
||||
int *union_xor;
|
||||
int ix, ixu;
|
||||
|
||||
max_union_size = setAsize + setBsize;
|
||||
union_set = TALLOC(max_union_size, const char *);
|
||||
union_xor = TALLOC(max_union_size, int);
|
||||
|
||||
/* I'm assuming here that setA has no duplicates,
|
||||
* i.e. is a set in mathematical sense */
|
||||
for (ix=0; ix<setAsize; ix++) {
|
||||
union_set[ix] = setA[ix];
|
||||
union_xor[ix] = 1;
|
||||
}
|
||||
diff_size = union_size = setAsize;
|
||||
for (ix=0; ix<setBsize; ix++) {
|
||||
for (ixu=0; ixu<union_size; ixu++) {
|
||||
if (union_set[ixu] == setB[ix]) break;
|
||||
}
|
||||
if (ixu < union_size) { /* already in union */
|
||||
union_xor[ixu] = 1-union_xor[ixu];
|
||||
diff_size--;
|
||||
}
|
||||
else { /* not already in union -add */
|
||||
if (f == esdSYMMETRIC) {
|
||||
union_set[ixu] = setB[ix];
|
||||
union_xor[ixu] = 1;
|
||||
union_size++;
|
||||
diff_size++;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Put results in symdiff set */
|
||||
diff_set = TALLOC(diff_size, const char *);
|
||||
ix = 0;
|
||||
for (ixu=0; ixu<union_size; ixu++) {
|
||||
if (union_xor[ixu]) {
|
||||
if (ix == diff_size) {
|
||||
printf("Short of space in diff_set\n");
|
||||
exit(1);
|
||||
}
|
||||
diff_set[ix] = union_set[ixu];
|
||||
ix++;
|
||||
}
|
||||
}
|
||||
*sym_size = diff_size;
|
||||
free(union_xor);
|
||||
free(union_set);
|
||||
return diff_set;
|
||||
}
|
||||
|
||||
/* isSet tests that elements of list are unique, that is, that the list is a
|
||||
* mathematical set. The uniqueness test implemented here is strcmp. */
|
||||
int isSet(const char *list[], int lsize)
|
||||
{
|
||||
int i, j;
|
||||
const char *e;
|
||||
if (lsize == 0) {
|
||||
return 1;
|
||||
}
|
||||
for (i = lsize-1; i>0; i--) {
|
||||
e = list[i];
|
||||
for (j = i-1; j>=0; j--) {
|
||||
if (strcmp(list[j], e) == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void printSet (const char *set[], int ssize)
|
||||
{
|
||||
int ix;
|
||||
printf(" = {");
|
||||
for (ix=0;ix<ssize; ix++) {
|
||||
printf( "%s ", set[ix]);
|
||||
}
|
||||
printf("}\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const char **symset;
|
||||
int sysize;
|
||||
|
||||
/* Validate precondition stated by task, that inputs are sets. */
|
||||
assert(isSet(XSET(setA)));
|
||||
assert(isSet(XSET(setB)));
|
||||
|
||||
printf ("A symmdiff B");
|
||||
symset = symmdiff( &sysize, esdSYMMETRIC, XSET(setA), XSET(setB));
|
||||
printSet(symset, sysize);
|
||||
free(symset);
|
||||
printf ("A - B");
|
||||
symset = symmdiff( &sysize, esdDIFFERENCE, XSET(setA), XSET(setB));
|
||||
printSet(symset, sysize);
|
||||
printf ("B - A");
|
||||
symset = symmdiff( &sysize, esdDIFFERENCE, XSET(setB), XSET(setA));
|
||||
printSet(symset, sysize);
|
||||
free(symset);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue