314 lines
8.1 KiB
C
314 lines
8.1 KiB
C
#include <assert.h>
|
|
#include <inttypes.h>
|
|
#include <math.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int spitze(const bool t[], int size) {
|
|
for (int i = 0; i < size; i += 1) {
|
|
if (t[0] ^ t[i]) {
|
|
return i;
|
|
}
|
|
}
|
|
return size;
|
|
}
|
|
// int main() {
|
|
// assert(spitze((bool[]){}, 0) == 0);
|
|
// assert(spitze((bool[]){true}, 1) == 1);
|
|
// assert(spitze((bool[]){false}, 1) == 1);
|
|
// assert(spitze((bool[]){true, false}, 2) == 1);
|
|
// assert(spitze((bool[]){true, true}, 2) == 2);
|
|
// assert(spitze((bool[]){false, true}, 2) == 1);
|
|
// assert(spitze((bool[]){false, false, true, false}, 4) == 2);
|
|
// }
|
|
|
|
bool nul(const bool t[], int size) {
|
|
while (size > 0 && !t[size - 1]) {
|
|
size -= 1;
|
|
}
|
|
int count = 0;
|
|
for (int i = 0; i < size; i += 1) {
|
|
count += t[i];
|
|
if (count <= i / 2) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
// int main() {
|
|
// assert(nul((bool []){}, 0));
|
|
// assert(nul((bool []){true}, 1));
|
|
// assert(nul((bool []){false}, 1));
|
|
// assert(nul((bool []){true, false, true, false, false}, 5));
|
|
// assert(!nul((bool []){true, false, false, true}, 4));
|
|
// assert(!nul((bool []){false, true}, 2));
|
|
// }
|
|
|
|
// On suppose que b != 0
|
|
double modfloat(double a, double b) {
|
|
double q = floor(a / b);
|
|
return a - (b * q) - ((b < 0) * b);
|
|
}
|
|
// int main() {
|
|
// assert(fabs(modfloat(3.14, 1.5) - 0.14) <= 1e-15);
|
|
// assert(fabs(modfloat(-3.14, 1.5) - 1.36) <= 1e-15);
|
|
// assert(fabs(modfloat(3.14, -1.5) - 0.14) <= 1e-15);
|
|
// assert(fabs(modfloat(-3.14, -1.5) - 1.36) <= 1e-15);
|
|
// assert(fabs(modfloat(3, 1.5) - 0) <= 1e-15);
|
|
// }
|
|
|
|
void minmax(const int t[], int size, int *min, int *max) {
|
|
if (size) {
|
|
*min = t[0];
|
|
*max = t[0];
|
|
}
|
|
for (int i = 1; i < size; i += 1) {
|
|
if (t[i] < *min) {
|
|
*min = t[i];
|
|
} else if (t[i] > *max) {
|
|
*max = t[i];
|
|
}
|
|
}
|
|
}
|
|
// int main() {
|
|
// int min = 0;
|
|
// int max = 0;
|
|
// minmax((int[]){1, -2, -2, 3, 4}, 5, &min, &max);
|
|
// assert(min == -2 && max == 4);
|
|
// minmax((int[]){}, 0, &min, &max);
|
|
// assert(min == -2 && max == 4);
|
|
// minmax((int[]){1, 1}, 2, &min, &max);
|
|
// assert(min == 1 && max == 1);
|
|
// minmax((int[]){-1}, 1, &min, &max);
|
|
// assert(min == -1 && max == -1);
|
|
// }
|
|
|
|
int medianemax(const int t[], int size) {
|
|
if (!size) {
|
|
return 0;
|
|
}
|
|
bool forward = false;
|
|
int min = 0;
|
|
int max = size - 1;
|
|
while (min < max) {
|
|
if (forward) {
|
|
min += 1;
|
|
if (t[min] >= t[max]) {
|
|
forward = false;
|
|
}
|
|
} else {
|
|
max -= 1;
|
|
if (t[max] >= t[min]) {
|
|
forward = true;
|
|
}
|
|
}
|
|
}
|
|
return min;
|
|
}
|
|
// int main() {
|
|
// assert(medianemax((int[]){}, 0) == 0);
|
|
// assert(medianemax((int[]){2}, 1) == 0);
|
|
// assert(medianemax((int[]){3, 5, 4, 5, 5, 4}, 6) == 3);
|
|
// assert(medianemax((int[]){3, 5, 5, 4, 5, 5, 4}, 7) == 2);
|
|
// }
|
|
|
|
void derive(double t[], int size) {
|
|
if (!size) {
|
|
return;
|
|
}
|
|
for (int i = 1; i < size; i += 1) {
|
|
t[i - 1] = i * t[i];
|
|
}
|
|
t[size - 1] = 0;
|
|
}
|
|
// int main() {
|
|
// double t[] = {1, 0, -3.5};
|
|
// derive(t, 0);
|
|
// assert(t[0] == 1 && t[1] == 0 && t[2] == -3.5);
|
|
// derive(t, 3);
|
|
// assert(t[0] == 0 && t[1] == -7 && t[2] == 0);
|
|
// }
|
|
|
|
double *multpol(const double t1[], const double t2[], int size1, int size2) {
|
|
if (size1 == 0 || size2 == 0) {
|
|
return NULL;
|
|
}
|
|
int size3 = size1 - 1 + size2;
|
|
double *t3 = malloc(size3 * sizeof(double));
|
|
// On s'économise l'initialisation
|
|
for (int i = 0; i < size1; i += 1) {
|
|
t3[i] = t1[i] * t2[0];
|
|
}
|
|
for (int j = 1; j < size2; j += 1) {
|
|
t3[j + size1 - 1] = t1[size1 - 1] * t2[j];
|
|
}
|
|
|
|
for (int i = 0; i < size1 - 1; i += 1) {
|
|
for (int j = 1; j < size2; j += 1) {
|
|
t3[i + j] += t1[i] * t2[j];
|
|
}
|
|
}
|
|
|
|
return t3;
|
|
}
|
|
// int main() {
|
|
// double t1[] = {2.1, 3.1, 5.1};
|
|
// double t2[] = {2, 3};
|
|
// double t3[] = {4.2, 12.5, 19.5, 15.3};
|
|
// double *res = multpol(t1, t2, 3, 2);
|
|
// for (int i = 0; i < 4; i += 1) {
|
|
// assert(fabs(t3[i] - res[i]) <= 1e-14);
|
|
// }
|
|
// free(res);
|
|
// }
|
|
|
|
char *cesar(const char str[], int a, int b) {
|
|
char map[26];
|
|
// C'est moche mais ça ne change pas la complexité. Sur de longues
|
|
// chaines ça doit même être plus efficace de calculer une map à l'avance.
|
|
for (char c = 0; c < 26; c += 1) {
|
|
int r = ((c * a) + b) % 26;
|
|
if (r < 0) {
|
|
r += 26;
|
|
}
|
|
for (char i = 0; i < c; i += 1) {
|
|
if (map[i] == r) {
|
|
return NULL;
|
|
}
|
|
}
|
|
map[c] = r;
|
|
}
|
|
|
|
// On peut faire plus efficace en consommant str et en stockant res dedans
|
|
char *res = malloc(strlen(str) * sizeof(char));
|
|
|
|
for (int i = 0; str[i] != 0; i += 1) {
|
|
if (str[i] >= 'a' && str[i] <= 'z') {
|
|
res[i] = 'a' + map[(str[i] - 'a')];
|
|
} else if (str[i] >= 'A' && str[i] <= 'Z') {
|
|
res[i] = 'A' + map[(str[i] - 'A')];
|
|
} else {
|
|
res[i] = str[i];
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
// char *CESAR_RES = "Ajoof hfwom!";
|
|
// int main() {
|
|
// char *res = cesar("Hello world!", -3, -5);
|
|
// for (int i = 0; CESAR_RES[i-1] != 0; i+=1) {
|
|
// assert(res[i] == CESAR_RES[i]);
|
|
// }
|
|
// free(res);
|
|
|
|
// assert(cesar("Hello world!", 13, 5) == NULL);
|
|
// }
|
|
|
|
char FIGURES[] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
|
|
int POWS[] = {1, 10, 100, 1000};
|
|
char *auguste(int n) {
|
|
assert(n >= 1 && n <= 3999);
|
|
char *str = malloc(16);
|
|
int i = 0;
|
|
|
|
int f = 3;
|
|
while (n > 0) {
|
|
int p = POWS[f];
|
|
if (n >= 9 * p) {
|
|
str[i] = FIGURES[f * 2];
|
|
str[i + 1] = FIGURES[(f * 2) + 2];
|
|
i += 2;
|
|
n -= 9 * p;
|
|
} else if (n >= 5 * p) {
|
|
str[i] = FIGURES[(f * 2) + 1];
|
|
i += 1;
|
|
n -= 5 * p;
|
|
}
|
|
while (n >= p) {
|
|
str[i] = FIGURES[f * 2];
|
|
i += 1;
|
|
n -= p;
|
|
}
|
|
f -= 1;
|
|
}
|
|
|
|
return str;
|
|
}
|
|
|
|
// int NS[] = {1, 42, 124, 532, 2323, 3888, 3999};
|
|
// char STRS[][16] = {"I", "XLII", "CXXIV", "DXXXII", "MMCCCXXIII",
|
|
// "MMMDCCCLXXXVIII", "MMMCMXCIX"};
|
|
// int main() {
|
|
// for (int i = 0; i < 6; i+=1) {
|
|
// char* str = auguste(NS[i]);
|
|
// for (int j = 0; STRS[i][j-1] != 0; j+=1) {
|
|
// assert(STRS[i][j] == str[j]);
|
|
// }
|
|
// free(str);
|
|
// }
|
|
// }
|
|
|
|
// Cette fonction consomme le tableau d'entrée. On pourrait cloner le tableau à
|
|
// la place si on ne souhaite pas le modifier
|
|
int *ecart(int t[], int size) {
|
|
// Sort t with an insertion sort
|
|
for (int i = 1; i < size; i += 1) {
|
|
int j = i;
|
|
while (j > 0 && t[j] < t[j - 1]) {
|
|
t[j] = t[j] ^ t[j - 1];
|
|
t[j - 1] = t[j] ^ t[j - 1];
|
|
t[j] = t[j] ^ t[j - 1];
|
|
j -= 1;
|
|
}
|
|
}
|
|
|
|
int *res = malloc(2);
|
|
int n = size + 1;
|
|
int m = size + 1;
|
|
int ci = 0;
|
|
for (int i = 0; i < size; i += 1) {
|
|
if (i + 1 == size || t[i + 1] != t[ci]) {
|
|
if (i - ci < n) {
|
|
res[0] = t[ci];
|
|
n = i - ci;
|
|
if (i - ci > 0) {
|
|
res[1] = t[ci];
|
|
m = i - ci;
|
|
}
|
|
} else if (i - ci < m) {
|
|
res[1] = t[ci];
|
|
m = i - ci;
|
|
}
|
|
ci = i + 1;
|
|
}
|
|
// Shortcut
|
|
if (n == 0 && m == 0) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
int main() {
|
|
int *res = ecart((int[]){4, 2, 4, 3}, 4);
|
|
assert((res[0] == 2 && res[1] == 3) || (res[1] == 2 && res[0] == 3));
|
|
free(res);
|
|
|
|
res = ecart((int[]){4, 2}, 2);
|
|
assert((res[0] == 2 && res[1] == 4) || (res[1] == 2 && res[0] == 4));
|
|
free(res);
|
|
|
|
res = ecart((int[]){3, 2, 1, 2, 1, 1}, 6);
|
|
assert((res[0] == 3 && res[1] == 2) || (res[1] == 2 && res[0] == 3));
|
|
free(res);
|
|
|
|
res = ecart((int[]){3, 2, 1, 2, 1, 1, 2, 3}, 8);
|
|
assert(res[0] == 3 && res[1] == 3);
|
|
free(res);
|
|
}
|