Coder Social home page Coder Social logo

codev0 / js-combinatorics Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jsantirso/js-combinatorics

0.0 2.0 0.0 128 KB

A javascript combinatorics library, currently a function that generates combinations of an array's elements

License: MIT License

JavaScript 100.00%

js-combinatorics's Introduction

js-combinatorics

A javascript combinatorics library. Right now, it provides just the following utility:

combinatorics.processCombinations(array, process, minCombLen, maxCombLen)

A generator of combinations of an array's elements

  • It's fast (0.2 secs for an array of length 20)
  • It's able to compute only some subsets of combinations (using minCombLen and maxCombLen)
  • It's memory-constant, because it doesn't build an array with the combinations (it receives a processing function instead, which can build an array if it needs to). Furthermore, the function can return false to halt mid-process.

Usage examples:

var deck = []; // A standard deck of cards
for(var i = 1; i <= 52; i++) deck.push(i); // Deck initialization, [1..52]

// Let's calculate how many unique first hands there are (it should be 2,598,960)
var count = 0,
	sdate = new Date();
combinatorics.processCombinations(deck, function(combination){count += 1;}, 5, 5);
// Notice that I'm simply counting, but I could be processing the combinations in any other way
console.log(count + ' combinations processed in ' + (new Date() - sdate)/1000 + ' seconds');
>> 2598960 combinations processed in 0.371 seconds

// Given a hand and the opportunity to discard from 1 to 5 of the cards, let's calculate how many
// different outcomes there can be (it should be 1,729,647, +1 if you count the option of not discarding)
count = 0;
sdate = new Date();
combinatorics.processCombinations(deck.slice(5), function(combination){count += 1;}, 1, 5);
// Notice that I'm simply counting, but I could be processing the combinations in any other way
console.log(count + ' combinations processed in ' + (new Date() - sdate)/1000 + ' seconds');
>> 1729647 combinations processed in 0.232 seconds

// A simple example
combinatorics.processCombinations([1,2,3,4], function(combination){console.log(combination)});
>> [1]
>> [2]
>> [3]
>> [4]
>> [1, 2]
>> [1, 3]
>> [1, 4]
>> [2, 3]
>> [2, 4]
>> [3, 4]
>> [1, 2, 3]
>> [1, 2, 4]
>> [1, 3, 4]
>> [2, 3, 4]
>> [1, 2, 3, 4]

js-combinatorics's People

Contributors

jsantirso avatar

Watchers

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