npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

combination_gen

v1.0.3

Published

Combinations generator (combinatorics) Generates all possible combinations of (n/k) k elements from n elements with no repetitions. Iterate or display combinations.

Downloads

14

Readme

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

combination_gen.js

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Javascript module to work with combinations. Generate (n/k) combinations, iterate and work with them.

Available as a js file, and as a node.js npm package:

  • node.js npm package -> master branch

  • plain .js component -> no_npm branch

MIT License (MIT)

tags

combinations, javascript, basic combinatorics, set, k-combinations,

Description

this module can generate all the possible combinations of a subset of number k distinct elements, from a set of n elements, where the order does not matter.

The combinations can be listed for examination, or can be iterated over.

The total number of possible combination for n and k, is given by:

 / n \    =       n!            for k <= n
 \ k /         k! (n-k)! 

http://en.wikipedia.org/wiki/Combination

Usage

Installation

node.js package

To install with npm, as a node.js package:
With command line, navigate to the folder where will be installed and enter:

npm install combination_gen

Then In javascript, import with require:

var combination_gen = require('combination_gen')

plain javascript

To install as a plain javascript file:
In github select the no_npm branch, and download with the link there. Include the combination_gen.js file in your project.

Two ways to use

There are two ways to use this component:

Basic

To explore combinations (n/k) Specify values for n and k, and explore the combinations.

With items

To operate over combinations of items. Specify the items in the set, and the size k of the subsets. Operate over the combiantions.

Usage description

Basic

Specify values for n and k

   combination_gen.n = 6
   combination_gen.k = 4

Draw to console a matrix of the combinations ( to console.log() )

   combination_gen.draw_matrix()

This method will iterate for all the combinations, and output them, well formated, to console.log Will output this:

  ::  combinations matrix  :: 
  In this matrix each row is a possible combination.
  Each column is an element of the set of n elements.
  An o means the element is included.
  An - means the element is not included.

  [ 0 1 2 3 4 5 ]
  [ o o o o - - ]  1
  [ o o o - o - ]  2
  [ o o o - - o ]  3
  [ o o - o o - ]  4
  [ o o - o - o ]  5
  [ o o - - o o ]  6
  [ o - o o o - ]  7
  [ o - o o - o ]  8
  [ o - o - o o ]  9
  [ o - - o o o ]  10
  [ - o o o o - ]  11
  [ - o o o - o ]  12
  [ - o o - o o ]  13
  [ - o - o o o ]  14
  [ - - o o o o ]  15   

With items

Add items to the set

   combination_gen.add( 'blue' )
   combination_gen.add( 'orange' )
   combination_gen.add( 'purple' )

Each item added will adjust the value of n appropiately.

Specify a value for k

   combination_gen.k = 2

List all the combinations ( to console.log() )

   combination_gen.list()

Will output this

   ::  combinations list  :: 
   List all possible combinations
   For the values of k and n.

   [ blue      orange    ]  1
   [ blue      purple    ]  2
   [ orange    purple    ]  3   

This method will iterate for all the combinations, and output them, well formated, to console.log. toString() will be called on each item.

Draw to console a matrix of the combinations ( to console.log() )

   combination_gen.draw_matrix()

Work with the combinations

Other than displaying the combinations for exploring, This module also allow to iterate and work over the combinations.

Add items to the set

   combination_gen.add( 'blue' )
   combination_gen.add( 'orange' )
   combination_gen.add( 'purple' )

Specify a value for k

   combination_gen.k = 2

Build the set

   combination_gen.build()

This will create the set, and set it to an initial value. This method should be called after changing n,k or adding items. Methods draw_matrix and list call it before strating to iterate.

Get the current combination as an array of items:

   combination_gen.get_combination()

Will return an array:

   [ 'blue', 'orange' ]

Get the current combination as an array of booleans:

    combination_gen.set

will return:

   [ true, true, false ]

where each value correspond to an item, and is true if the item is included in the current combination, or false if it is not.

Iterate to the next combination

   combination_gen.next()

Will return true, if the iteration was successful, or false if the current iteration is the last. So it can be easily used in loops.

Return to the first combination with:

   combination_gen.build()

Will recreate the set, and set it to the initial position, Ready for iteration.

Reference

This section content is pending

combination_gen

combination_gen properties

combination_gen.n

combination_gen.k

combination_gen.set
    

combination_gen methods

combination_gen.add()

combination_gen.build()

combination_gen.next()   

combination_gen.get_combination()   

combination_gen.list()  

combination_gen.draw_matrix()