Add Exos
This commit is contained in:
66
ex0/ft_foreach.c
Normal file
66
ex0/ft_foreach.c
Normal file
@@ -0,0 +1,66 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_foreach.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/12 09:38:54 by lclerel- #+# #+# */
|
||||
/* Updated: 2026/03/12 11:26:41 by lclerel- ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
void ft_foreach(int *tab, int length, void (*f)(int))
|
||||
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < length)
|
||||
{
|
||||
f(tab[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
/*void ft_putnbr(int nb)
|
||||
{
|
||||
char value;
|
||||
|
||||
if (nb == -2147483648)
|
||||
{
|
||||
write(1, "-2147483648", 11);
|
||||
return ;
|
||||
}
|
||||
if (nb < 0)
|
||||
{
|
||||
write(1, "-", 1);
|
||||
nb = -nb;
|
||||
}
|
||||
if (nb > 9)
|
||||
{
|
||||
ft_putnbr(nb / 10);
|
||||
}
|
||||
value = nb % 10 + '0';
|
||||
write(1, &value, 1);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int tab[4] = {1, 8, 9, 6};
|
||||
int i;
|
||||
int length;
|
||||
i = 0;
|
||||
length = 4;
|
||||
|
||||
write(1, "Test avec tableau de 4, avec valeur 1 8 9 6 : ", 46);
|
||||
write(1, "\n", 1);
|
||||
while (i < length)
|
||||
{
|
||||
ft_putnbr(tab[i]);
|
||||
i++;
|
||||
write(1, "\n", 1);
|
||||
}
|
||||
}*/
|
||||
80
ex1/ft_map.c
Normal file
80
ex1/ft_map.c
Normal file
@@ -0,0 +1,80 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_map.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/12 09:41:12 by lclerel- #+# #+# */
|
||||
/* Updated: 2026/03/12 11:28:20 by lclerel- ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int *ft_map(int *tab, int length, int (*f)(int))
|
||||
{
|
||||
int i;
|
||||
int *tab2;
|
||||
|
||||
tab2 = malloc(sizeof(int) * length);
|
||||
i = 0;
|
||||
if (!tab2)
|
||||
{
|
||||
return (NULL);
|
||||
}
|
||||
while (i < length)
|
||||
{
|
||||
tab2[i] = f(tab[i]);
|
||||
i++;
|
||||
}
|
||||
return (tab2);
|
||||
}
|
||||
|
||||
/*void ft_putnbr(int nb)
|
||||
{
|
||||
char value;
|
||||
|
||||
if (nb == -2147483648)
|
||||
{
|
||||
write(1, "-2147483648", 11);
|
||||
return ;
|
||||
}
|
||||
if (nb < 0)
|
||||
{
|
||||
write(1, "-", 1);
|
||||
nb = -nb;
|
||||
}
|
||||
if (nb > 9)
|
||||
{
|
||||
ft_putnbr(nb / 10);
|
||||
}
|
||||
value = nb % 10 + '0';
|
||||
write(1, &value, 1);
|
||||
}
|
||||
|
||||
int ft_carre(int n)
|
||||
{
|
||||
return (n * n);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int tab[4] = {1, 8, 9, 6};
|
||||
int *result = ft_map(tab, 4, &ft_carre);
|
||||
int i;
|
||||
int length;
|
||||
|
||||
i = 0;
|
||||
length = 4;
|
||||
|
||||
write(1, "Test avec tableau de 4, avec valeur 1 8 9 6 : ", 46);
|
||||
write(1, "\n", 1);
|
||||
while (i < length)
|
||||
{
|
||||
ft_putnbr(result[i]);
|
||||
i++;
|
||||
write(1, "\n", 1);
|
||||
}
|
||||
}*/
|
||||
58
ex2/ft_any.c
Normal file
58
ex2/ft_any.c
Normal file
@@ -0,0 +1,58 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_any.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/12 11:29:41 by lclerel- #+# #+# */
|
||||
/* Updated: 2026/03/12 11:50:58 by lclerel- ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
int ft_any(char **tab, int (*f)(char*))
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (tab[i] != NULL)
|
||||
{
|
||||
if (f(tab[i]) != 0)
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
/*
|
||||
int s_check(char *s)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while (s[i] == 'S')
|
||||
{
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *tab[] = {"Test", "1234", "Test56", "singesavant", NULL};
|
||||
int result;
|
||||
|
||||
result = ft_any(tab, &s_check);
|
||||
if (result == 1)
|
||||
{
|
||||
write(1, "Un mot a ete trouver\n", 22);
|
||||
}
|
||||
else
|
||||
{
|
||||
write(1, "Rien trouver.\n", 14);
|
||||
}
|
||||
return (0);
|
||||
}*/
|
||||
96
ex3/ft_count_if.c
Normal file
96
ex3/ft_count_if.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_count_if.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/12 11:54:32 by lclerel- #+# #+# */
|
||||
/* Updated: 2026/03/16 14:05:24 by lclerel- ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
int ft_count_if(char **tab, int length, int (*f)(char*))
|
||||
{
|
||||
int i;
|
||||
int count;
|
||||
|
||||
i = 0;
|
||||
count = 0;
|
||||
if (!tab || !f)
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
while (i < length)
|
||||
{
|
||||
if (f(tab[i]) != 0)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
/*int len_check(char *s)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
while (s[i])
|
||||
{
|
||||
i++;
|
||||
}
|
||||
if (i > 3)
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void ft_putnbr(int nb)
|
||||
{
|
||||
char value;
|
||||
|
||||
if (nb == -2147483648)
|
||||
{
|
||||
write(1, "-2147483648", 11);
|
||||
return ;
|
||||
}
|
||||
if (nb < 0)
|
||||
{
|
||||
write(1, "-", 1);
|
||||
nb = -nb;
|
||||
}
|
||||
if (nb > 9)
|
||||
{
|
||||
ft_putnbr(nb / 10);
|
||||
}
|
||||
value = nb % 10 + '0';
|
||||
write(1, &value, 1);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *tab[] = {"Test", "1234", "Test56", "singesavant", NULL};
|
||||
int result;
|
||||
int length = 0;
|
||||
|
||||
while (tab[length] != NULL)
|
||||
{
|
||||
length++;
|
||||
}
|
||||
result = ft_count_if(tab, length, &len_check);
|
||||
if (result > 1)
|
||||
{
|
||||
write(1, "Plusieurs mots ont ete trouver\n", 31);
|
||||
write(1, "Nombre Total : ", 15);
|
||||
ft_putnbr(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
write(1, "Rien trouver.\n", 14);
|
||||
}
|
||||
return (0);
|
||||
}*/
|
||||
104
ex4/ft_is_sort.c
Normal file
104
ex4/ft_is_sort.c
Normal file
@@ -0,0 +1,104 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_is_sort.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lclerel- <lclerel-@learner.42.tech> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/03/13 14:01:47 by lclerel- #+# #+# */
|
||||
/* Updated: 2026/03/16 14:00:35 by lclerel- ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
int ft_is_sort(int *tab, int length, int (*f)(int, int))
|
||||
{
|
||||
int i;
|
||||
int cs;
|
||||
int ds;
|
||||
|
||||
i = 0;
|
||||
cs = 1;
|
||||
ds = 1;
|
||||
if (length <= 1)
|
||||
{
|
||||
return (1);
|
||||
}
|
||||
while (i < length - 1)
|
||||
{
|
||||
if (f(tab[i], tab[i + 1]) > 0)
|
||||
{
|
||||
cs = 0;
|
||||
}
|
||||
if (f(tab[i], tab[i + 1]) < 0)
|
||||
{
|
||||
ds = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (cs || ds);
|
||||
}
|
||||
|
||||
/*void ft_putnbr(int nb)
|
||||
{
|
||||
char value;
|
||||
|
||||
if (nb == -2147483648)
|
||||
{
|
||||
write(1, "-2147483648", 11);
|
||||
return ;
|
||||
}
|
||||
if (nb < 0)
|
||||
{
|
||||
write(1, "-", 1);
|
||||
nb = -nb;
|
||||
}
|
||||
if (nb > 9)
|
||||
{
|
||||
ft_putnbr(nb / 10);
|
||||
}
|
||||
value = nb % 10 + '0';
|
||||
write(1, &value, 1);
|
||||
}
|
||||
|
||||
int ft_compare(int a, int b)
|
||||
{
|
||||
return (a - b);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int croiss[] = {1, 2, 3, 4, 5, 6, 6, 7};
|
||||
int decroiss[] = {7, 6, 6, 5, 4, 3, 2, 1};
|
||||
int pas_trie[] = {1, 2, 3, 5, 6, 7, 6, 9};
|
||||
int neg[] = {-1, -12, 11, -19};
|
||||
|
||||
write(1, "Test Croissant > (1, 2, 3, 4, 5, 6, 6, 7) : ", 44);
|
||||
if (ft_is_sort(croiss, 8, &ft_compare) == 1)
|
||||
write(1, "Ok :3 (Trié)\n", 14);
|
||||
else
|
||||
write(1, "Not ok (Pas trié)\n", 19);
|
||||
|
||||
// Test 2 : Décroissant
|
||||
write(1, "Test Decroissant > (7, 6, 6, 5, 4, 3, 2, 1) : ", 46);
|
||||
if (ft_is_sort(decroiss, 8, &ft_compare) == 1)
|
||||
write(1, "Ok :3 (Trié)\n", 14);
|
||||
else
|
||||
write(1, "Not ok (Pas trié)\n", 19);
|
||||
|
||||
// Test 3 : Pas trié
|
||||
write(1, "Test Pas trier > (1, 2, 3, 5, 6, 7, 6, 9) : ", 44);
|
||||
if (ft_is_sort(pas_trie, 8, &ft_compare) == 1)
|
||||
write(1, "Ok :3 (Trié)\n", 14);
|
||||
else
|
||||
write(1, "Not ok (Pas trié)\n", 19);
|
||||
|
||||
// Test 4 : Negatif
|
||||
write(1, "Test avec negatif > (-1, -12, 11, -19) : ", 42);
|
||||
if (ft_is_sort(neg, 4, &ft_compare) == 1)
|
||||
write(1, "Ok :3 (Trié)\n", 14);
|
||||
else
|
||||
write(1, "Not ok (Pas trié)\n", 19);
|
||||
return (0);
|
||||
}*/
|
||||
Reference in New Issue
Block a user