/push_swap

This project made me sort data on a stack, with a limited set of instructions, using the lowest possible number of actions. To succeed I had to manipulate various types of algorithms and chose the most appropriate solution (out of many) for an optimized data sorting.

Primary LanguageC

📖 Push_Swap

Sort data on a stack

Made with C GitHub code size in bytes Number of lines of code Code language count GitHub top language GitHub last commit

This project will make you sort data on a stack, with a limited set of instructions, using the lowest possible number of actions. To succeed you’ll have to manipulate various types of algorithms and choose the most appropriate solution (out of many) for an optimized data sorting. Version: 6.
You can read the subject: push_swap.pdf

Discription of mandatory part

Challenge

Sort a random list of integers using the smallest number of moves, 2 stacks and a limited set of operations.

You start with two empty stacks: a and b. You are given a random list of integers via command line arguments.

Only these moves are allowed:

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

At the end, stack b must empty empty and all integers must be in stack a, sorted in ascending order.

The Project

Create two programs: checker and push_swap.

The checker program reads a random list of integers from the stdin, stores them, and checks to see if they are sorted.

The push_swap program calculates the moves to sort the integers – pushing, popping, swapping and rotating them between stack a and stack b – and displays those directions on the stdout.

You can pipe push_swap into checker, and checker will verify that push_swap's instructions were successful.

Both programs must mandatorily parse input for errors, including empty strings, no parameters, non-numeric parameters, duplicates, and invalid/non-existent instructions.

Push_Swap must conform to the 42 Norm.
Using normal libc functions is strictly forbidden. Students are however, allowed to use: write, read, malloc, free, exit. It must not have any memory leaks. Errors must be handled carefully.
In no way can it quit in an unexpected manner (segmentation fault, bus error, double free, etc).

All errors like: wrong commands, permission to files and etc, need be handle.

Testers

Tester

hsaadi's 42 push_swap Score