Push_swap

Description

Push_swap is a program that sorts a stack of integers using a specific set of operations. It takes a list of integers as an argument and displays the smallest list of instructions required to sort the stack in ascending order.

Usage

./push_swap <list of integers>
  • The stack is formatted as a list of integers, where the first argument is at the top of the stack.
  • Instructions are separated by a newline character ('\n').
  • If no parameters are specified, the program will not display anything and return to the prompt.

Instructions

The program uses the following set of operations to manipulate the stacks:

  • sa: Swap the first two elements at the top of stack A. (Do nothing if there is only one or no elements)
  • sb: Swap the first two 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 the first element at the top of stack B to stack A. (Do nothing if stack B is empty)
  • pb: Push the first element at the top of stack A to stack B. (Do nothing if stack A is empty)
  • ra: Rotate all elements of stack A up by one. The first element becomes the last one.
  • rb: Rotate all elements of stack B up by one. The first element becomes the last one.
  • rr: ra and rb at the same time.
  • rra: Reverse rotate all elements of stack A down by one. The last element becomes the first one.
  • rrb: Reverse rotate all elements of stack B down by one. The last element becomes the first one.
  • rrr: rra and rrb at the same time.

Error Handling

  • If there are any errors, such as non-integer arguments, arguments exceeding the integer limit, or duplicate numbers, the program will display an error message: "Error\n".

Example

$> ./push_swap 2 1 3 6 5 8
sa
pb
pb
pb
sa
pa
pa
pa

Testing

During the evaluation process, a binary named checker_OS will be provided to check the correctness of the program's output. It works as follows:

$> ARG="4 67 3 87 23"; ./push_swap $ARG | wc -l
6
$> ARG="4 67 3 87 23"; ./push_swap $ARG | ./checker_OS $ARG
OK

If the checker_OS program displays "KO," it means that the list of instructions generated by push_swap does not sort the numbers correctly.