Questions tagged [combinations]

A combination is a selection of objects from a larger collection in no particular order.

A combination is a selection of objects from a larger collection in no particular order. More formally, calculating a combination is based on the number of ways of picking k unordered outcomes from n possibilities. Calculating the number of k combinations is known as the binomial coefficient.

An important difference between combinations and permutations is in combinations, the order of the selection does not matter.

For example, given three fruits, say, {apple, orange, pear}, there are three combinations of two that can be drawn from this set: {apple, pear}, {apple, orange}, {pear, orange}.

References:

6331 questions
693
votes
33 answers

Get all possible (2^N) combinations of a list’s elements, of any length

I have a list with 15 numbers. How can I produce all 32,768 combinations of those numbers (i.e., any number of elements, in the original order)? I thought of looping through the decimal integers 1–32768 and using the binary representation of each…
Ben
  • 66,838
  • 37
  • 84
  • 108
641
votes
77 answers

Algorithm to return all combinations of k elements from n

I want to write a function that takes an array of letters as an argument and a number of those letters to select. Say you provide an array of 8 letters and want to select 3 letters from that. Then you should get: 8! / ((8 - 3)! * 3!) = 56 Arrays…
Fredrik
  • 4,161
  • 9
  • 28
  • 31
308
votes
32 answers

Finding all possible combinations of numbers to reach a given sum

How would you go about testing all possible combinations of additions from a given set N of numbers so they add up to a given final number? A brief example: Set of numbers to add: N = {1,5,22,15,0,...} Desired result: 12345
James P.
  • 19,313
  • 27
  • 97
  • 155
197
votes
19 answers

How can I combine two strings together in PHP?

I don't actually know how to describe what I wanted, but I'll show you: For example: $data1 = "the color is"; $data2 = "red"; What should I do (or process) so $result is the combination of $data1 and $data2? Desired result: $result = "the color is…
glennanyway
  • 2,013
  • 2
  • 13
  • 3
141
votes
20 answers

Statistics: combinations in Python

I need to compute combinatorials (nCr) in Python but cannot find the function to do that in math, numpy or stat libraries. Something like a function of the type: comb = calculate_combinations(n, r) I need the number of possible combinations, not…
Morlock
  • 6,880
  • 16
  • 43
  • 50
125
votes
10 answers

Capturing ctrl+z key combination in javascript

I am trying to capture ctrl+z key combination in javascript with this code: Untitled Document