Code Monkey home page Code Monkey logo

push_swap's Introduction

push_swap

The project consists of two programs:

  1. The first ./push_swap sorts the stack and outputs a sequential set of commands to the standard output stream of the required ones.
  2. The second ./checker checks that the stack is sorted ascending

The game is composed of 2 stacks named A and B.

To start with:
A contains a random number of either positive or negative numbers without any duplicates.
B is empty.

Image alt

The goal is to sort in ascending order numbers into stack A.

To do this you have the following operations at your disposal:
sa :swap A-swap the first 2 elements at the top of stack A. Do nothing if thereis only one or no elements.
sb :swap B-swap the first 2 elements at the top of stack B. Do nothing if thereis 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. Donothing if B is empty.
pb :push B-take the first element at the top of A and put it at the top of B. Donothing if A is empty.
ra :rotate A-shift up all elements of stack A by 1. The first element becomesthe last one.
rb :rotate B-shift up all elements of stack B by 1. The first element becomesthe 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 elementbecomes the first one.
rrb :reverse rotate B-shift down all elements of stack B by 1. The last elementbecomes the first one.
rrr :rra and rrb at the same time.

Run push_swap

This will display the necessary instructions to sort the stack.

./push_swap 1 5 4 3 2

To check whether the stack is really sorted, you can run the ./checker program, as follows

./push_swap 1 5 4 3 2  | ./checker 1 5 4 3 2

If the stack is sorted then the output will be OK; if not, then KO

For debug mode you can use -v flag in ./checker, as follows

./checker -v 1 5 4 3 2

In this mode, you can sequentially execute the instructions given above and keep track of the current state of the stack
Alt Text

push_swap's People

Contributors

arptra avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.