push_swap

This project was made in accordance with the project of School 21 (Ecole 42).

GitHub code size in bytes Number of lines of code Code language count GitHub top language


Preamble

The purpose of this project is to code algorithm for sorting list of integers using two stacks.

push_swap should sort in ascending order numbers into stack and display a sequence
of instructions that can sort given stack.

You can see the subject here: push_swap.

Main requirements, rules and code style: Norm.


Description

Mandatory part

  • At start of the programm there are two stacks named a and b.

  • The stack a contains a random amount of integers which cannot be duplicated.

  • The stack b is empty.

Allowed operations with stacks are:

  1. sa : swap a - swap the first 2 elements at the top of stack a.
    Do nothing if there is only one or no elements.

  2. sb : swap b - swap the first 2 elements at the top of stack b.
    Do nothing if there is only one or no elements.

  3. ss : sa and sb at the same time.

  4. 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.

  5. 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.

  6. ra : rotate a - shift up all elements of stack a by 1.
    The first element becomes the last one.

  7. rb : rotate b - shift up all elements of stack b by 1.
    The first element becomes the last one.

  8. rr : ra and rb at the same time.

  9. rra : reverse rotate a - shift down all elements of stack a by 1.
    The last element becomes the first one.

  10. rrb : reverse rotate b - shift down all elements of stack b by 1.
    The last element becomes the first one.

  11. rrr : rra and rrb at the same time.

  • At evaluation final grade depends on amount of instructions to sort the stack.

Bonus part

  • In this section we should code a checker for push_swap.

  • checker receives as arguments given stack a.
    It will then wait and read instructions on the standard input. If after executing those instructions, stack a is actually sorted
    and b is empty, then checker must display "OK\n" on the standard output.
    In every other case, checker must display "KO\n" on the standard output.


Installation and usage

Makefile compiles given functions into push_swap or checker executable file.

Compiler: gcc

Flags: -Wall -Werror -Wextra


  • Go to the project folder:
$ cd 'path_to_push_swap'
  • Then typo one of these command:
Command Description
make compiling mandatory part
make bonus compiling bonus part
make clean clearing all .o files
make fclean clearing all .o files and executables
  • Example of executing push_swap :
./push_swap 3 5 2 7 9
  • Example of executing checker with instructions received from push_swap :
ARG="3 5 2 7 9"; ./push_swap $ARG | ./checker $ARG

Testing