norminette ok and tester.sh

This commit is contained in:
whaffman 2024-11-19 16:16:34 +01:00
parent fe48b78005
commit e99b6f1ad4
8 changed files with 112 additions and 165 deletions

View File

@ -21,7 +21,7 @@ OBJ_PATH = obj
VPATH = src:src/util:src/moves:src/sort VPATH = src:src/util:src/moves:src/sort
SOURCES = push_swap.c ft_lstat.c new_element.c push.c rotate.c reverse_rotate.c \ SOURCES = push_swap.c ft_lstat.c new_element.c push.c rotate.c reverse_rotate.c \
swap.c sort3.c sort5.c stack_min.c stack_max.c utils.c sortmore.c swap.c sort3.c stack_min.c utils.c sortmore.c rotate_utils.c
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))

View File

@ -20,40 +20,35 @@ typedef struct s_state
t_list *a; t_list *a;
t_list *b; t_list *b;
} t_state; } t_state;
t_list *ft_lstat(t_list *list, int n);
int merge_to_a(t_state *state);
void sortmore(t_state *state);
void common_rotations(int *shortest_a, int *shortest_b, t_state *state);
void push_all_but_3_b(t_state *state);
int ft_abs(int a); int ft_abs(int a);
int ft_max(int a, int b); int ft_max(int a, int b);
int ft_min(int a, int b);
int count_gt(int n, t_list *stack); int stack_min(t_list *lst);
int put_at_index_asc(int number, t_list *stack);
int put_at_index_desc(int number, t_list *stack);
void rotate_a(int n, t_state *state);
void rotate_b(int n, t_state *state);
void rotate_a_to(int index, t_state *state);
void rotate_b_to(int index, t_state *state);
int index_of(int number, t_list *stack);
int put_at_index_asc(int number, t_list *stack);
int put_at_index_desc(int number, t_list *stack);
int rotate_a_to_top(t_state *state);
int shortest_rotate(int index, int size);
int new_element(t_list **stack, const char *str);
t_list *ft_lstat(t_list *list, int n);
void print_stack(const char *name, t_list *stack); void print_stack(const char *name, t_list *stack);
void print_state(t_state *state); void print_state(t_state *state);
int stack_max(t_list *lst);
int stack_min(t_list *lst);
void sortmore(t_state *state);
int sort3(t_state *state); int sort3(t_state *state);
int sort5(t_state *state);
int count_gt(int n, t_list *stack);
int index_of(int number, t_list *stack);
int new_element(t_list **stack, const char *str);
int put_at_index_asc(int number, t_list *stack);
void push_all_but_3_b(t_state *state);
void push(t_list **stack_1, t_list **stack_2); void push(t_list **stack_1, t_list **stack_2);
int shortest_rotate(int index, int size);
void common_rotations(int *shortest_a, int *shortest_b, t_state *state);
void rotate_b(int n, t_state *state);
void rotate_a(int n, t_state *state);
void rotate_a_to(int index, t_state *state);
int rotate_a_to_top(t_state *state);
void rotate(t_list **stack); void rotate(t_list **stack);
void reverse_rotate(t_list **stack); void reverse_rotate(t_list **stack);
void swap(t_list **stack); void swap(t_list **stack);
void pa(t_state *state); void pa(t_state *state);

View File

@ -13,6 +13,25 @@
#include "libft.h" #include "libft.h"
#include "push_swap.h" #include "push_swap.h"
int put_at_index_asc(int number, t_list *stack)
{
const int i = index_of(stack_min(stack), stack);
const int size = ft_lstsize(stack);
const int gt = count_gt(number, stack);
return ((i + gt) % size);
}
void push_all_but_3_b(t_state *state)
{
const int size = ft_lstsize(state->a);
int i;
i = 0;
while (i++ < size - 3)
pb(state);
}
void pb(t_state *state) void pb(t_state *state)
{ {
push(&(state->a), &(state->b)); push(&(state->a), &(state->b));

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: o_ :::::: ::: */ /* ::: o_ :::::: ::: */
/* sort5.c :+: / :+::+: :+: */ /* rotate_utils.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */ /* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */ /* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/07 15:17:36 by whaffman #+#+# #+#+# #+# #+# | */ /* Created: 2024/11/19 15:03:32 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/07 15:57:09 by whaffman ### ### ### ### / \ */ /* Updated: 2024/11/19 15:03:35 by whaffman ### ### ### ### / \ */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,29 +19,46 @@ int rotate_a_to_top(t_state *state)
return (1); return (1);
} }
//sort5 int shortest_rotate(int index, int size)
int merge_to_a(t_state *state)
{ {
const int b = *(int *) state->b->content; if (index <= size / 2)
return (index);
rotate_a_to(put_at_index_asc(b, state->a), state); return (-1 * (size - index));
pa(state);
return (1);
} }
int sort5(t_state *state) //TODO
void rotate_a(int n, t_state *state)
{ {
pb(state); while (n > 0)
pb(state); {
if (*(int *)state->b->content > *(int *)state->b->next->content) ra(state);
sb(state); n--;
sort3(state); }
print_state(state); while (n < 0)
merge_to_a(state); {
print_state(state); rra(state);
merge_to_a(state); n++;
print_state(state); }
rotate_a_to_top(state); }
print_state(state);
return (1); void rotate_b(int n, t_state *state)
{
while (n > 0)
{
rb(state);
n--;
}
while (n < 0)
{
rrb(state);
n++;
}
}
void rotate_a_to(int index, t_state *state)
{
int n;
n = shortest_rotate(index, ft_lstsize(state->a));
rotate_a(n, state);
} }

View File

@ -79,7 +79,7 @@ void common_rotations(int *shortest_a, int *shortest_b, t_state *state)
} }
else else
{ {
while (shortest_a < 0 && shortest_b < 0) while (*shortest_a < 0 && *shortest_b < 0)
{ {
rrr(state); rrr(state);
(*shortest_a)++; (*shortest_a)++;

View File

@ -1,29 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* stack_max.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */
/* Created: 2024/11/07 16:29:41 by whaffman #+#+# #+#+# #+# #+# | */
/* Updated: 2024/11/07 16:36:40 by whaffman ### ### ### ### / \ */
/* */
/* ************************************************************************** */
#include "libft.h"
int stack_max(t_list *lst)
{
int n;
n = *(int *) lst->content;
while (lst->next)
{
if (*(int *) lst->content > n)
n = *(int *) lst->content;
lst = lst->next;
}
if (*(int *) lst->content > n)
n = *(int *) lst->content;
return (n);
}

View File

@ -13,57 +13,6 @@
#include "libft.h" #include "libft.h"
#include "push_swap.h" #include "push_swap.h"
int shortest_rotate(int index, int size)
{
if (index <= size / 2)
return (index);
return (-1 * (size - index));
}
//TODO
void rotate_a(int n, t_state *state)
{
while (n > 0)
{
ra(state);
n--;
}
while (n < 0)
{
rra(state);
n++;
}
}
void rotate_b(int n, t_state *state)
{
while (n > 0)
{
rb(state);
n--;
}
while (n < 0)
{
rrb(state);
n++;
}
}
void rotate_a_to(int index, t_state *state)
{
int n;
n = shortest_rotate(index, ft_lstsize(state->a));
rotate_a(n, state);
}
void rotate_b_to(int index, t_state *state)
{
int n;
n = shortest_rotate(index, ft_lstsize(state->b));
rotate_b(n, state);
}
int index_of(int number, t_list *stack) int index_of(int number, t_list *stack)
{ {
int i; int i;
@ -78,6 +27,7 @@ int index_of(int number, t_list *stack)
} }
return (-1); return (-1);
} }
int ft_abs(int a) int ft_abs(int a)
{ {
if (a >= 0) if (a >= 0)
@ -92,13 +42,6 @@ int ft_max(int a, int b)
return (b); return (b);
} }
int ft_min(int a, int b)
{
if (a < b)
return (a);
return (b);
}
int count_gt(int n, t_list *stack) int count_gt(int n, t_list *stack)
{ {
int count; int count;
@ -112,31 +55,3 @@ int count_gt(int n, t_list *stack)
} }
return (count); return (count);
} }
int put_at_index_asc(int number, t_list *stack)
{
const int i = index_of(stack_min(stack), stack);
const int size = ft_lstsize(stack);
const int gt = count_gt(number, stack);
return ((i + gt) % size);
}
int put_at_index_desc(int number, t_list *stack)
{
const int i = index_of(stack_max(stack), stack);
const int size = ft_lstsize(stack);
const int lt = size - count_gt(number, stack);
return ((i + lt) % size);
}
void push_all_but_3_b(t_state *state)
{
const int size = ft_lstsize(state->a);
int i;
i = 0;
while (i++ < size - 3)
pb(state);
}

30
tester.sh Normal file
View File

@ -0,0 +1,30 @@
#!/bin/bash
# This script is used to test the program
# test case 1
echo "Test case 1"
MAX=0
MIN=10000
for i in {1..100}
do
ARG=$(shuf -i 0-1000 -n 500)
N=$(./push_swap $ARG | wc -l)
if [ $N -gt $MAX ]
then
if [ $N -gt 5500 ]; then
echo "Max: $N | $(echo $ARG | tr -d '\n')"
fi
MAX=$N
fi
if [ $N -lt $MIN ]
then
MIN=$N
fi
AVG=$((AVG + N))
done
AVG=$((AVG / 100))
echo "Max: $MAX"
echo "Min: $MIN"
echo "Average: $AVG"