/42push_swap

sorting data on a stack with a limited set of instructions

Primary LanguageC

42 result

code-size last-commit

Table of Contents
  1. Summary
  2. Project Structure
  3. Environment
  4. Compile
  5. Execute
  6. Example
  7. Links

push_swap

This project involves sorting data on a stack, with a limited set of instructions, and the smallest number of moves. To make this happen, you will have to manipulate various sorting algorithms and choose the most appropriate solution(s) for optimized data sorting.

Instructions

  • sa: swap a - swap the first 2 elements at the top of stack a. Do nothing if there is only one or no elements.
  • sb: swap b - swap the first 2 elements at the top of stack b. Do nothing if there is only one or no elements.
  • ss: sa and sb at the same time.
  • pa: push a - take the first element at the top of b and put it at the top of a. Do nothing if b is empty.
  • pb: push b - take the first element at the top of a and put it at the top of b. Do nothing if a is empty.
  • ra: rotate a - shift up all elements of stack a by 1. The first element becomes the last one.
  • rb: rotate b - shift up all elements of stack b by 1. The first element becomes the last one.
  • rr: ra and rb at the same time.
  • rra: reverse rotate a - shift down all elements of stack a by 1. The last element becomes the first one.
  • rrb: reverse rotate b - shift down all elements of stack b by 1. The last element becomes the first one.
  • rrr: rra and rrb at the same time.

Summary

Program name push_swap
Makefile all, clean, fclean, re
Arguments a random list of integers
External functs. write, read, malloc, free, exit
Libft authorized Yes

Project Structure

./
├── includes/	# header files
├── libft/		# library files
├── src/		# source files
└── Makefile

Environment

  • MacOS 12.1(Monterey, Intel)

Developed and tested in this environment.

Compile

Mandatory

$ git clone https://github.com/srngch/42push_swap
$ make

Execute

Run compiled executable file in the root folder.

$ ./push_swap [numbers]

Example

# print instructions
$ ./push_swap 1 3 2 5 4
pb
ra
pb
sa
rra
pa
pa

# print nothing if the list is already sorted
$ ./push_swap 1 2 3 4 5

# print nothing if argument is not provided
$ ./push_swap

# print error if argument is not a integer
$ ./push_swap 0 one 2 3
Error

# print error if argument is not a range of integers
$ ./push_swap 2147483648
Error

# print error if argument is duplicated
$ ./push_swap 1 2 3 1
Error

# check if the list is sorted
$ ARG="1 5 2 4 3"; ./push_swap $ARG | ./checker_Mac $ARG
OK

Links