Because Swap_push isn’t as natural
Summary: 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 one (of many) most appropriate solution for an optimized data sorting.
instructions | Description |
---|---|
sa | swap first two elements of stack A |
sb | swap first two elements of stack B |
ss | sa and sb at the same time |
pa | pops the first elememt on B and puts it on top of A |
pb | pops the first elememt on A and puts it on top of B |
ra | rotates stuck A up by one |
rb | rotates stuck B up by one |
rr | rotates both A and B up by one |
rra | rotates stuck A down by one |
rrb | rotates stuck B down by one |
rrr | rotates both A and B down by one |
The next 3 images show you the basic process that the algorythm follows to sort the arguments using two stacks. Using the visualiser I wrote as a bonus. can be called using "-v" flag.
The screenshot below shows the visualiser once the stack has been sorted, see the OK which is the checkers output meaning that the push-swap program sorted the arguments
Finally the "-s" flag shows each stack after every opperation as shown with 3 arguments.
In the screenshot below you can see the use of the ARG=`ruby -e "puts (0..3).to_a.shuffle.join(' ')"`;
which populates ARG with random numbers which is then passed as the arguments to both the checker and the push-swap programs.
-WE THINK CODE(CPT)