Skip to content

A program which prints, for a set of N inputs and a selected K, all N choose K combinations.

Notifications You must be signed in to change notification settings

anthony-arnold/choose

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

#Choose

For a given set of N distinct items, there are N choose K ways of selecting a unique combination of K unordered items. The value, N choose K, is known as the binomial coefficient.

This program, choose, takes the set of N items and prints all the possible subsets of size K.

##Origins

The choose program originated from the need to print all valid combinations for a boxed trifecta on the Emirates Melbourne Cup.

The Ashgrove/The Gap Lions Club wished to run a sweep wherein each ticket was a boxed (unordered) trifecta. This allowed for 2024 (24 choose 3) tickets to be potentially sold; a great fundraising opportunity.

The tickets were generated by using an infantile version of choose to print all 2024 combinations in comma-separated form. The resulting file was used as a database to mail-merge into the template.

##Usage

choose [-t | -c] <item> [<item> ...] k

-t = Print selections separated by tabs ('\t')
-c = Print selections separated by commas (',') - default
<item> = Any string or range value. 
k = The K part of "N choose K" i.e. the size of the subsets to select from the item set.

A range is in the form of "m-n" and is expanded at runtime to create one item per number in the range of [m,n]. Either number may be negative, and the range may be descending.

If the only item given is equal to -, the program will read the input set from standard input.

##Examples

$ choose a b c 1
a
b
c


$ choose 1-5 4
1,2,3,4
1,2,3,5
1,2,4,5
1,3,4,5
2,3,4,5

$ choose -t 0-3 5-7 6
0    1    2    3    5    6
0    1    2    3    5    7
0    1    2    3    6    7
0    1    2    5    6    7
0    1    3    5    6    7
0    2    3    5    6    7
1    2    3    5    6    7

$ echo apple banana pear | choose -c - 2
apple,banana
apple,pear
banana,pear

There are also many more combinations of strings and ranges which could also be used.

##Compilation

There's only one file, so use whatever compiler you have available and compile the file choose.cpp.

For example:

$ g++ -Wall -Werror -pedantic-errors -std=c++11 -O2 choose.cpp -o choose

Or create a Visual Studio project and compile.

Note that C++11 features are used, so your compiler must support at least the following features:

##Installation

To install on a unix-like environment, just copy the compiled executable to /usr/bin, or any other directory in your path that makes sense:

$ sudo cp choose /usr/bin/

Under Windows, copy the choose.exe file to somewhere like C:\Program Files\anthony-arnold\bin\choose.exe and add the directory to the PATH environment variable. This will make choose available from the command line.

Contributions

Obviously, there are still heaps of improvements that could be made to choose, and any contribution would be most welcome.

About

A program which prints, for a set of N inputs and a selected K, all N choose K combinations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages