Giter Site home page Giter Site logo

valentinarho / go-dutch-flag Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 17 KB

Simple implementation of the Dutch national flag problem in Golang

License: GNU General Public License v3.0

Go 100.00%
golang dutch-flag partitioning partitioning-algorithms

go-dutch-flag's Introduction

go-dutch-flag

Simple implementation of the Dutch national flag problem in Golang.

The problem

source: Wikipedia

The Dutch national flag problem is a computer science programming problem proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: red, white and blue. Given balls of these three colors arranged randomly in a line (the actual number of balls does not matter), the task is to arrange them such that all balls of the same color are together and their collective color groups are in the correct order.

This problem can also be viewed in terms of rearranging elements of an array. Suppose each of the possible elements could be classified into exactly one of three categories (bottom, middle, and top). The problem is then to produce an array such that all "bottom" elements come before (have an index less than the index of) all "middle" elements, which come before all "top" elements.

Usage

Run main.go for a usage example.

DutchFlag function takes in input the array to sort and the partitioning function. This function should be a function from int to int and return, given an integer item the corresponding partition (-1, 0, 1).

The first example in main.go uses the builtin partitioning function that works on arrays containing only elements in {1,2,3}.

The second example implements a custom partitioning function that split an array in even, zeros and odd items.

It is possible to tailor the code in order to handle different data types in the array. It is only necessary to modify in dutch.go:

  • line 5: the type of the elem parameter in partitioningFunction
  • line 8: the type of the a parameter
  • line 19: the type of the a parameter

And then, create your main.go and your partitioning function!

go-dutch-flag's People

Contributors

valentinarho avatar

Stargazers

 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.