116 lines
2.1 KiB
C
116 lines
2.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* tools.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/12/07 18:37:38 by erey-bet #+# #+# */
|
|
/* Updated: 2022/12/07 18:37:39 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "push_swap.h"
|
|
|
|
int get_min(long *tab, int len)
|
|
{
|
|
int i;
|
|
int index;
|
|
|
|
i = 0;
|
|
index = 0;
|
|
while (++i < len)
|
|
if (tab[i] <= tab[index])
|
|
index = i;
|
|
return (index);
|
|
}
|
|
|
|
long *get_index(long *tab, int len)
|
|
{
|
|
int i;
|
|
int min;
|
|
long *new;
|
|
|
|
i = -1;
|
|
min = tab[0];
|
|
new = malloc(sizeof(long) * (len + 1));
|
|
if (new == NULL)
|
|
return (NULL);
|
|
while (++i < len)
|
|
{
|
|
min = get_min(tab, len);
|
|
tab[min] = 2147483648;
|
|
new[min] = i;
|
|
}
|
|
return (new);
|
|
}
|
|
|
|
int max_len_binary(t_stack s)
|
|
{
|
|
long i;
|
|
int x;
|
|
int y;
|
|
int max;
|
|
|
|
y = -1;
|
|
max = 1;
|
|
while (++y < s.len)
|
|
{
|
|
i = 1;
|
|
x = 1;
|
|
while (i < s.list[y])
|
|
{
|
|
i *= 2;
|
|
x++;
|
|
}
|
|
if (x > max)
|
|
max = x;
|
|
}
|
|
return (max);
|
|
}
|
|
|
|
int check_sa(t_stack s)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (i < s.len - 1)
|
|
{
|
|
if (s.list[i] > s.list[i + 1])
|
|
{
|
|
return (0);
|
|
}
|
|
i++;
|
|
}
|
|
return (1);
|
|
}
|
|
|
|
int tab_malloc(int argc, char *argv[])
|
|
{
|
|
int x;
|
|
int y;
|
|
int check;
|
|
int count;
|
|
|
|
y = 1;
|
|
count = 0;
|
|
while (y < argc)
|
|
{
|
|
x = 0;
|
|
check = 0;
|
|
while (argv[y][x])
|
|
{
|
|
if (argv[y][x] >= '0' && argv[y][x] <= '9' && check == 0)
|
|
{
|
|
count++;
|
|
check = 1;
|
|
}
|
|
else if ((argv[y][x] < '0' || argv[y][x] > '9') && check == 1)
|
|
check = 0;
|
|
x++;
|
|
}
|
|
y++;
|
|
}
|
|
return (count);
|
|
}
|