Skip to content

Commit ea91650

Browse files
committed
update push_swap
1 parent f3fabfd commit ea91650

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1645
-0
lines changed

Makefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
NAME = push_swap
2+
DEQUE_SRCS = deque_basic.c deque_access.c deque_push_pop.c deque_rotate.c deque_swap.c
3+
SRCS = main.c push_swap.c is_valid_string.c compress.c is_duplicated.c is_empty_array.c error.c $(DEQUE_SRCS)
4+
OBJS = $(SRCS:.c=.o)
5+
LIBDIR = libft
6+
LIB = libft.a
7+
CC = cc
8+
CFLAGS = -Wall -Wextra -Werror
9+
10+
.DEFAULT_GOAL = all
11+
12+
$(NAME): $(OBJS) $(LIBDIR)/$(LIB)
13+
$(CC) $(CFLAGS) $(OBJS) $(LIBDIR)/$(LIB) -o $(NAME)
14+
15+
%.o: %.c
16+
$(CC) $(CFLAGS) -c $< -o $@
17+
18+
$(LIBDIR)/$(LIB):
19+
make -C $(LIBDIR)
20+
21+
clean:
22+
make -C $(LIBDIR) fclean
23+
rm -f $(OBJS)
24+
25+
fclean: clean
26+
rm -f $(NAME)
27+
28+
re: fclean all
29+
30+
all: $(NAME)
31+
32+
.PHONY: all clean fclean re

compress.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* compress.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: nkawaguc <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/10/14 14:49:53 by nkawaguc #+# #+# */
9+
/* Updated: 2024/10/14 15:57:05 by nkawaguc ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "push_swap.h"
14+
15+
static void quick_sort(int *arr, int left, int right);
16+
static void swap(int *a, int *b);
17+
static int lower_bound(int *arr, int size, int target);
18+
19+
void compress(int *arr, int size)
20+
{
21+
int *arr_sorted;
22+
int i;
23+
24+
arr_sorted = (int *)malloc(sizeof(int) * size);
25+
if (arr_sorted == NULL)
26+
error();
27+
i = -1;
28+
while (++i < size)
29+
arr_sorted[i] = arr[i];
30+
quick_sort(arr_sorted, 0, size);
31+
if (is_duplicated(arr_sorted, size))
32+
error();
33+
i = -1;
34+
while (++i < size)
35+
arr[i] = lower_bound(arr_sorted, size, arr[i]);
36+
free(arr_sorted);
37+
}
38+
39+
static void quick_sort(int *arr, int left, int right)
40+
{
41+
int pivot;
42+
int i;
43+
int j;
44+
45+
if (left >= right)
46+
return ;
47+
swap(&arr[left], &arr[(left + right) / 2]);
48+
pivot = arr[left];
49+
i = left + 1;
50+
j = right;
51+
while (1)
52+
{
53+
while (arr[i] < pivot)
54+
i++;
55+
while (pivot < arr[j])
56+
j--;
57+
if (i >= j)
58+
break ;
59+
swap(&arr[i], &arr[j]);
60+
i++;
61+
j--;
62+
}
63+
swap(&arr[left], &arr[j]);
64+
quick_sort(arr, left, i - 1);
65+
quick_sort(arr, j + 1, right);
66+
}
67+
68+
static void swap(int *a, int *b)
69+
{
70+
int tmp;
71+
72+
tmp = *a;
73+
*a = *b;
74+
*b = tmp;
75+
}
76+
77+
static int lower_bound(int *arr, int size, int target)
78+
{
79+
int left;
80+
int right;
81+
int mid;
82+
83+
left = -1;
84+
right = size;
85+
while (right - left > 1)
86+
{
87+
mid = (left + right) / 2;
88+
if (arr[mid] >= target)
89+
right = mid;
90+
else
91+
left = mid;
92+
}
93+
return (right);
94+
}

error.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* error.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: nkawaguc <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/10/14 14:03:34 by nkawaguc #+# #+# */
9+
/* Updated: 2024/10/14 15:57:24 by nkawaguc ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "push_swap.h"
14+
15+
void error(void)
16+
{
17+
ft_putendl_fd(MSG_ERROR, STDERR_FILENO);
18+
exit(EXIT_FAILURE);
19+
}

is_duplicated.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* is_duplicated.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: nkawaguc <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/10/14 15:51:58 by nkawaguc #+# #+# */
9+
/* Updated: 2024/10/14 15:57:34 by nkawaguc ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "push_swap.h"
14+
15+
int is_duplicated(int *arr, int size)
16+
{
17+
int i;
18+
19+
i = 0;
20+
while (++i < size)
21+
if (arr[i - 1] == arr[i])
22+
return (TRUE);
23+
return (FALSE);
24+
}

is_empty_array.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* is_empty_array.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: nkawaguc <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/10/14 14:25:59 by nkawaguc #+# #+# */
9+
/* Updated: 2024/10/14 14:47:15 by nkawaguc ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "push_swap.h"
14+
15+
int is_empty_array(int argc)
16+
{
17+
if (argc < VALID_ARGC_MIN)
18+
return (TRUE);
19+
return (FALSE);
20+
}

is_valid_string.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* is_valid_string.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: nkawaguc <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/10/14 14:33:05 by nkawaguc #+# #+# */
9+
/* Updated: 2024/10/14 15:57:45 by nkawaguc ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "push_swap.h"
14+
15+
static int ft_isspace(char c);
16+
static int ft_isnull(char c);
17+
18+
int is_valid_string(const char *str)
19+
{
20+
int i;
21+
22+
i = 0;
23+
while (ft_isspace(str[i]))
24+
i++;
25+
if (str[i] == '+' || str[i] == '-')
26+
i++;
27+
if (!ft_isdigit(str[i]))
28+
return (FALSE);
29+
while (ft_isdigit(str[i]))
30+
i++;
31+
if (ft_isnull(str[i]))
32+
return (TRUE);
33+
return (FALSE);
34+
}
35+
36+
static int ft_isspace(char c)
37+
{
38+
return ((9 <= c && c <= 13) || c == ' ');
39+
}
40+
41+
static int ft_isnull(char c)
42+
{
43+
return (c == '\0');
44+
}

libft/Makefile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
SRCS = ft_atoi.c ft_memchr.c ft_split.c ft_strncmp.c \
2+
ft_bzero.c ft_memcmp.c ft_strchr.c ft_strnstr.c \
3+
ft_calloc.c ft_memcpy.c ft_strdup.c ft_strrchr.c \
4+
ft_isalnum.c ft_memmove.c ft_striteri.c ft_strtrim.c \
5+
ft_isalpha.c ft_memset.c ft_strjoin.c ft_substr.c \
6+
ft_isascii.c ft_putchar_fd.c ft_strlcat.c ft_tolower.c \
7+
ft_isdigit.c ft_putendl_fd.c ft_strlcpy.c ft_toupper.c \
8+
ft_isprint.c ft_putnbr_fd.c ft_strlen.c \
9+
ft_itoa.c ft_putstr_fd.c ft_strmapi.c
10+
BONUS_SRCS = ft_lstadd_back_bonus.c ft_lstlast_bonus.c \
11+
ft_lstadd_front_bonus.c ft_lstmap_bonus.c \
12+
ft_lstclear_bonus.c ft_lstnew_bonus.c \
13+
ft_lstdelone_bonus.c ft_lstsize_bonus.c \
14+
ft_lstiter_bonus.c
15+
NAME = libft.a
16+
OBJS = $(SRCS:.c=.o)
17+
BONUS_OBJS = $(BONUS_SRCS:.c=.o)
18+
BONUS_NAME = libft_bonus.a
19+
CC = cc
20+
CFLAG = -Wall -Wextra -Werror
21+
22+
.DEFAULT_GOAL := all
23+
24+
$(NAME): $(OBJS) $(BONUS_OBJS)
25+
ar rcs $(NAME) $(OBJS)
26+
27+
$(BONUS_NAME): $(BONUS_OBJS) $(NAME)
28+
ar rcs $(BONUS_NAME) $(BONUS_OBJS) $(OBJS)
29+
ar rcs $(NAME) $(BONUS_OBJS) $(OBJS)
30+
31+
%.o: %.c
32+
$(CC) $(CFLAG) -c $< -o $@
33+
34+
clean:
35+
rm -f $(OBJS) $(BONUS_OBJS) $(BONUS_NAME)
36+
37+
fclean: clean
38+
rm -f $(NAME)
39+
40+
re: fclean all
41+
42+
all: $(NAME)
43+
44+
bonus: $(BONUS_NAME)
45+
46+
.PHONY: all clean fclean re bonus

libft/ft_atoi.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_atoi.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: nkawaguc <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/03/15 16:52:33 by nkawaguc #+# #+# */
9+
/* Updated: 2024/03/15 16:52:33 by nkawaguc ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libft.h"
14+
15+
int ft_atoi(const char *str)
16+
{
17+
long ret;
18+
long sign;
19+
20+
ret = 0l;
21+
sign = 1l;
22+
while ((9 <= *str && *str <= 13) || *str == ' ')
23+
str++;
24+
if (*str == '-' || *str == '+')
25+
if (*str++ == '-')
26+
sign = -1l;
27+
while (ft_isdigit(*str))
28+
{
29+
if (ret > LONG_MAX / 10 || (ret == LONG_MAX / 10 && *str > '7'))
30+
return ((int)LONG_MAX);
31+
if (ret < LONG_MIN / 10 || (ret == LONG_MIN / 10 && *str > '8'))
32+
return ((int)LONG_MIN);
33+
ret = ret * 10l + (*str++ - '0') * sign;
34+
}
35+
return ((int)ret);
36+
}

libft/ft_bzero.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_bzero.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: nkawaguc <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/02/14 19:33:49 by nkawaguc #+# #+# */
9+
/* Updated: 2024/02/14 19:33:49 by nkawaguc ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libft.h"
14+
15+
void ft_bzero(void *s, size_t n)
16+
{
17+
(void)ft_memset(s, 0, n);
18+
}

libft/ft_calloc.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_calloc.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: nkawaguc <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/04/15 23:11:59 by nkawaguc #+# #+# */
9+
/* Updated: 2024/05/03 17:33:47 by nkawaguc ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libft.h"
14+
15+
void *ft_calloc(size_t count, size_t size)
16+
{
17+
void *ret;
18+
size_t i;
19+
20+
if (size > 0 && count > SIZE_MAX / size)
21+
return (NULL);
22+
ret = malloc(count * size);
23+
if (!ret)
24+
return (NULL);
25+
i = 0;
26+
while (i < count * size)
27+
((char *)ret)[i++] = 0;
28+
return (ret);
29+
}

0 commit comments

Comments
 (0)