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.
Instruction | Description |
---|---|
sa | swaps the first and second elements of stack A |
sb | swaps the first and second elements of stack B |
ss | executes the sa and sb commands simultaneously |
pa | removes the first element from stack B and places it on top of stack A |
pb | removes the first element from stack A and places it on top of stack B |
ra | rotates stack A from top to bottom |
rb | rotates stack B from top to bottom |
rr | rotates both stacks A and B from top to bottom |
rra | rotates stack A from bottom to top |
rrb | rotates stack B from bottom to top |
rrr | rotates both stacks A and B from bottom to top |
In this part we need to sort a list of numbers given in command line.
$> ./push_swap 5 3 2 1 4 # sorted list 1 2 3 4 5
Another example
$> ./push_swap -100 -145 150 10 5 300 # sorted list -145 -100 5 10 300
We need to handle errors too, because our program only works with unique numbers, no letter or special characters.
We need to create a program called 'checker'. This program check whether the list of istructions generated by the push_swap program actually sort the stack properly.
$>./checker 3 2 1 0
rra
pb
sa
rra
pa
OK # show 'OK' if list was sorted properly
$>./checker 3 2 1 0
sa
rra
pb
KO # show 'KO' if it's unsorted.
$>./checker 3 2 one 0
Error # when are not only numbers in args.
$>./checker "" 1
Error # when have empty string or invalid args
git clone git@github.com:vinicius-f-pereira/push_swap.git
git clone https://github.com/vinicius-f-pereira/push_swap.git
gh repo clone vinicius-f-pereira/push_swap
Use make
or make bonus
and follow instructions Here