jotavare / push_swap

Algorithm project where i must sort a given list of random numbers with a limited set of instructions, using the lowest possible number of actions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Linkedin 42

AboutHow to useMandatoryBonusNorminetteLicense

ABOUT

In this project, I developed a sorting algorithm for a specific problem using two stacks. The goal of the project is to efficiently sort a stack of integers using a set of predefined operations.

HOW TO USE

1º - Clone the repository

git clone git@github.com:jotavare/push_swap.git

2º - Enter the project folder and run make

cd push_swap/push_swap
make

3º - Run the code

If you have problems running the ./checker, use chmod 777 ./checker and try again.

./push_swap [numbers] | ./checker [numbers]
./push_swap 9 0 -217 2147483647 -2147483648 | ./checker 9 0 -217 2147483647 -2147483648

4º - Assign numbers to a variable and run the code

ARG=["numbers"]; ./push_swap $ARG | ./checker $ARG
ARG="3 0 9 2 -1"; ./push_swap $ARG | ./checker $ARG

MAKEFILE RULES

make - Compile push_swap mandatory functions.

make bonus - Compile push_swap bonus functions.

make all - Compile mandatory + bonus functions.

make clean - Delete all .o (object files) files.

make fclean - Delete all .o (object files) and .a (executable) files.

make re - Use rules fclean + all.

MANDATORY

RULES

The program is only allowed to work with two stacks, stack A and stack B. All the numbers are initially added to stack A, and B is empty.

Possible actions:

  • 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.
  • sa (swap A): Swap the first 2 elements at the top of stack A. Do nothing if there are only one or no elements.
  • sb (swap B): Swap the first 2 elements at the top of stack B. Do nothing if there are only one or no elements.
  • ss: sa and sb at the same time.
  • ra (rotate A): Shift all elements of stack A up by 1. The first element becomes the last one.
  • rb (rotate B): Shift all elements of stack B up by 1. The first element becomes the last one.
  • rr : ra and rb at the same time.
  • rra (reverse rotate A): Shift all elements of stack A down by 1. The last element becomes the first one.
  • rrb (reverse rotate B): Shift all elements of stack b down by 1. The last element becomes the first one.
  • rrr : rra and rrb at the same time.

GRADE

The grade depends on how efficient the program's sorting process is.

  • Sorting 3 values: no more than 3 actions.

  • Sorting 5 values: no more than 12 actions.

  • Sorting 100 values: rating from 1 to 5 points depending on the number of actions:

    • 5 points for less than 700 actions.
    • 4 points for less than 900 actions.
    • 3 points for less than 1100 actions.
    • 2 points for less than 1300 actions.
    • 1 point for less than 1500 actions.
  • Sorting 500 values: rating from 1 to 5 points depending on the number of actions:

    • 5 points for less than 5500 actions.
    • 4 points for less than 7000 actions.
    • 3 points for less than 8500 actions.
    • 2 points for less than 10000 actions.
    • 1 point for less than 11500 actions.

Note: Validating the project requires at least 80/100. I believe 3 points with 100 and 500 numbers would be 80/100.

ERROR MANAGEMENT

The program should print Error + \n if the following tests are made:

  • Non-numeric parameters.
  • Duplicate numeric parameter.
  • Numeric parameter greater than INT_MAX.
  • Numeric parameter less than INT_MIN.
./push_swap 4 bb 2
./push_swap 4 4 5
./push_swap 4 2 2147483648
./push_swap 4 2 -2147483649

SORTED EXAMPLES

Should not print anything if the following tests are made:

  • No parameter.
  • Single numeric argument.
  • The numbers are already sorted.
./push_swap
./push_swap 42
./push_swap 0 1 2 3
./push_swap 0 1 2 3 4 5 6 7 8 9

BONUS

  • Create a checker for push_swap that will read the program instructions and display KO or OK.

NORMINETTE

At 42 School, it is expected that almost every project is written following the Norm, which is the coding standard of the school.

- No for, do...while, switch, case, goto, ternary operators, or variable-length arrays allowed;
- Each function must be a maximum of 25 lines, not counting the function's curly brackets;
- Each line must be at most 80 columns wide, with comments included;
- A function can take 4 named parameters maximum;
- No assigns and declarations in the same line (unless static);
- You can't declare more than 5 variables per function;
- ...
  • 42 Norms - Information about 42 code norms. PDF
  • Norminette - Tool to respect the code norm, made by 42. GitHub
  • 42 Header - 42 header for Vim. GitHub

LICENSE

This work is published under the terms of Unlicense.

About

Algorithm project where i must sort a given list of random numbers with a limited set of instructions, using the lowest possible number of actions.

License:The Unlicense


Languages

Language:C 94.1%Language:Makefile 5.9%