improve dm3
This commit is contained in:
parent
80f9d37909
commit
d3c9e61f90
194
dm3/dm3.c
194
dm3/dm3.c
@ -1,18 +1,28 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <strings.h>
|
|
||||||
|
|
||||||
int spitze(const bool t[], int size) {
|
int spitze(const bool t[], int size) {
|
||||||
for (int i = 0; i < size; i += 1) {
|
for (int i = 0; i < size; i += 1) {
|
||||||
if (t[0] ^ t[i]) {
|
if (t[0] ^ t[i]) {
|
||||||
return i + 1;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return size;
|
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) {
|
bool nul(const bool t[], int size) {
|
||||||
while (size > 0 && !t[size - 1]) {
|
while (size > 0 && !t[size - 1]) {
|
||||||
@ -27,11 +37,27 @@ bool nul(const bool t[], int size) {
|
|||||||
}
|
}
|
||||||
return true;
|
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 modfloat(double a, double b) {
|
||||||
int q = (int)(a / b);
|
double q = floor(a / b);
|
||||||
return a - (double)(b * q);
|
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) {
|
void minmax(const int t[], int size, int *min, int *max) {
|
||||||
if (size) {
|
if (size) {
|
||||||
@ -46,12 +72,24 @@ void minmax(const int t[], int size, int *min, int *max) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 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) {
|
int medianemax(const int t[], int size) {
|
||||||
if (!size) {
|
if (!size) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
bool forward = true;
|
bool forward = false;
|
||||||
int min = 0;
|
int min = 0;
|
||||||
int max = size - 1;
|
int max = size - 1;
|
||||||
while (min < max) {
|
while (min < max) {
|
||||||
@ -69,13 +107,29 @@ int medianemax(const int t[], int size) {
|
|||||||
}
|
}
|
||||||
return min;
|
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) {
|
void derive(double t[], int size) {
|
||||||
|
if (!size) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
for (int i = 1; i < size; i += 1) {
|
for (int i = 1; i < size; i += 1) {
|
||||||
t[i - 1] = i * t[i];
|
t[i - 1] = i * t[i];
|
||||||
}
|
}
|
||||||
t[size - 1] = 0;
|
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) {
|
double *multpol(const double t1[], const double t2[], int size1, int size2) {
|
||||||
if (size1 == 0 || size2 == 0) {
|
if (size1 == 0 || size2 == 0) {
|
||||||
@ -99,14 +153,13 @@ double *multpol(const double t1[], const double t2[], int size1, int size2) {
|
|||||||
|
|
||||||
return t3;
|
return t3;
|
||||||
}
|
}
|
||||||
|
|
||||||
// int main() {
|
// int main() {
|
||||||
// double t1[] = {2.1, 3.1, 5.1};
|
// double t1[] = {2.1, 3.1, 5.1};
|
||||||
// double t2[] = {2, 3};
|
// double t2[] = {2, 3};
|
||||||
// double t3[] = {4.2, 12.5, 19.5, 15.299999999999999};
|
// double t3[] = {4.2, 12.5, 19.5, 15.3};
|
||||||
// double *res = multpol(t1, t2, 3, 2);
|
// double *res = multpol(t1, t2, 3, 2);
|
||||||
// for (int i = 0; i < 4; i += 1) {
|
// for (int i = 0; i < 4; i += 1) {
|
||||||
// assert(t3[i] == res[i]);
|
// assert(fabs(t3[i] - res[i]) <= 1e-14);
|
||||||
// }
|
// }
|
||||||
// free(res);
|
// free(res);
|
||||||
// }
|
// }
|
||||||
@ -122,7 +175,7 @@ char *cesar(const char str[], int a, int b) {
|
|||||||
}
|
}
|
||||||
for (char i = 0; i < c; i += 1) {
|
for (char i = 0; i < c; i += 1) {
|
||||||
if (map[i] == r) {
|
if (map[i] == r) {
|
||||||
assert(false && "la fonction n'est pas bijective");
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
map[c] = r;
|
map[c] = r;
|
||||||
@ -144,24 +197,117 @@ char *cesar(const char str[], int a, int b) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// char *CESAR_RES = "Ajoof hfwom!";
|
||||||
// int main() {
|
// int main() {
|
||||||
// char chaine1[] = "Hello world!";
|
// 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);
|
||||||
|
|
||||||
// char *res1 = cesar(chaine1, -3, -5);
|
// assert(cesar("Hello world!", 13, 5) == NULL);
|
||||||
// printf("resultat 1 : %s\n", res1);
|
|
||||||
|
|
||||||
// free(res1);
|
|
||||||
|
|
||||||
// int c = 13;
|
|
||||||
// int d = 5;
|
|
||||||
// char *res2 = cesar(chaine1, c, d);
|
|
||||||
// printf("resultat 2 : %s \n", res2);
|
|
||||||
// free(res2);
|
|
||||||
|
|
||||||
// exit(0);
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
char FIGURES[] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
|
||||||
|
int POWS[] = {1, 10, 100, 1000};
|
||||||
char *auguste(int n) {
|
char *auguste(int n) {
|
||||||
assert(n >= 1 && n <= 3999);
|
assert(n >= 1 && n <= 3999);
|
||||||
char *str =
|
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);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user