GithubHelp home page GithubHelp logo

push_swap's Introduction

push_swap

push_swap is a 42 school algorithm project where we must sort a given list of random numbers with a limited set of instructions, using the lowest possible number of actions.

To know

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

Usage

Git clone the repository and cd into it. Then use make to compile.

Then run it with :

./push_swap <numbers>

The numbers provided can be positive or negative integers. There must not be any duplicates. For example :

./push_swap 9 0 -217 2147483647 -2147483648

If the arguments are valid, the program will output the most efficient list of actions to sort the list.

The program can be checked with the provided checker, like this :

ARG="3 0 9 2 -1"; ./push_swap $ARG | ./checker $ARG

You can also simply make test3, make test5, make test100 or make test500 to quickly test the program with that number of values to sort.

You can visualize this push_swap algorithm in action with O-reo's push_swap visualizer:

Push_swap visualization by mcombeau

Push_Swap Rules and Grading

The program is only allowed two stacks to work with, stack A and stack B. All the numbers are initially added to stack A, and B is empty.

The possible actions are:

  • 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.
  • 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.
  • ra (rotate A): Shift all elements of stack A up by 1. The first element becomes the last one.
  • rb (rotate B): Shift all elements of stack B up by 1. The first element becomes the last one.
  • rr: ra and rb at the same time.
  • rra (reverse rotate A): Shift all elements of stack A down by 1. The last element becomes the first one.
  • rrb (reverse rotate B): Shift all elements of stack b down by 1. The last element becomes the first one.
  • rrr : rra and rrb at the same time.

The grade depends on how efficient the program's sorting process is.

  • Sorting 3 values: no more than 3 actions.
  • Sorting 5 values: no more than 12 actions.
  • Sorting 100 values: rating from 1 to 5 points depending on the number of actions:
    • 5 points for less than 700 actions
    • 4 points for less than 900
    • 3 points for less than 1100
    • 2 points for less than 1300
    • 1 point for less than 1500
  • Sorting 500 values: rating from 1 to 5 points depending on the number of actions:
    • 5 points for less than 5500 actions
    • 4 points for less than 7000
    • 3 points for less than 8500
    • 2 points for less than 10000
    • 1 point for less than 11500

Validating the project requires at least 80/100.


push_swap's People

Contributors

ababdelo avatar

Stargazers

 avatar Hiba ERRAOUI avatar eduwnant 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.