combinatorics

Determining the number of possible combinations

I'm trying to figure out how many possible ways there are to combine various elements form this string. "{Hello|Hi|Hey} {world|earth}{!|.|?}" Where one item (separated by a pipe/|) is selected at random from each group ({}) and combined into a single string. So the above "template" could produce: Hello world. Hi earth? Hey world. Hi...

Number of possible outcomes for 2 numbers given that one number is greater than the other

I am trying to write an algorithm to calculate outcomes. But I need help with combinatorics. Suppose I have to choose 2 numbers from 1 to 10. From the fundamental rule of counting, in the absence of any restriction, the number of possible outcomes is 10 * 10 = 100. (10 possible outcomes in choosing the first number x 10 possible outcom...

Combinatoric 'N choose R' in java math?

Is there a built in method in a java library that can compute 'N choose R' for any N, R? ...

A Knockout tournament - no of possible combinations

What are the no of combinations in which 8 persons taking part in a knockout tornament play? Total no of matches played would be 7 but I also need the number of combinations that can for this set ...

Concatenate values of n arrays in php

I have an unknown number of arrays, each containing an unknown number of words. I want to concatenate the values from each list so that all possible variations of the words are stored to a final array. For example, if array 1 contains: dog cat and array 2 contains: food tooth and array 3 contains: car bike I'd like the output t...

How can I print out all possible letter combinations a given phone number can represent?

I just tried for my first programming interview and one of the questions was to write a program that given a 7 digit telephone number, could print all possible combinations of letters that each number could represent. A second part of the question was, what about if this was a 12 digit international number? How would that effect you de...

Code for Variations with repetition (combinatorics)?

Does anyone have Java code for generating all VARIATIONS WITH REPETITION? There are plenty of permutation and combination examples available, and variations must be the easiest one... It feels stupid to waste time to reinvent the wheel (it must be plenty of code written for this). An example of VARIATIONS WITH REPETITION could be like...

enumerating through every five card combination

Hi, I have just had a brain block, I have a Deck object and want to get every 5 card combination from it in a iterative manner. Could someone show me how to do this, I would imagine it would be: for(int i =0; i <52; i++){ for(int j = i + 1 ; j < 52; j++){ for(int k = j + 1; k < 52; k++{ for(int l = k + 1; l < ...

Computing unique index for every poker starting hand

As there are 52 cards in a deck we know there are 52 choose 2 = 1326 distinct matchups, however in preflop poker this can be bucketed into 169 different hands such as AK offsuit and AK suited as whether it is A hearts K hearts or A spade K spades it makes no difference preflop. My question is, is there a nice mathematical property in whi...

How to calculate the cycles that change one permutation into another?

Hi, I'm looking for an algorithm that given two permutations of a sequence (e.g. [2, 3, 1, 4] and [4, 1, 3, 2]) calculates the cycles that are needed to convert the first into the second (for the example, [[0, 3], [1, 2]]). The link from mathworld says that Mathematica's ToCycle function does that, but sadly I don't have any Mathematica...

How to make all possible sum combinations from array elements in VB

If there is an array with elements: 1,2,3,4, the program should return another array with sum of all combinations: 1 2 3 4 3 (1+2) 4 (1+3) 5 (1+4) 5 (2+3) 6 (2+4) 7 (3+4) 6 (1+2+3) 7 (1+2+4) 8 (1+3+4) 9 (2+3+4) 10 (1+2+3+4) ...

Combinatorics, probability, dice

A friend of mine asked: if I have two dice and I throw both of them, what is the most frequent sum (of the two dice' numbers)? I wrote a small script: from random import randrange d = dict((i, 0) for i in range(2, 13)) for i in xrange(100000): d[randrange(1, 7) + randrange(1, 7)] += 1 print d Which prints: 2: 2770, 3: 5547, 4:...

What are some practical uses of generating all permutations of a list, such as ['a', 'b', 'c'] ?

I was asked by somebody in an interview for web front end job, to write a function that generates all permutations of a string, such as "abc" (or consider it ['a', 'b', 'c']). so the expected result from the function, when given ['a', 'b', 'c'], is abc acb bac bca cab cba Actually in my past 20 years of career, I have never needed to...

Generating all unique combinations for "drive ya nuts" puzzle

A while back I wrote a simple python program to brute-force the single solution for the drive ya nuts puzzle. The puzzle consists of 7 hexagons with the numbers 1-6 on them, and all pieces must be aligned so that each number is adjacent to the same number on the next piece. The puzzle has ~1.4G non-unique possibilities: you have 7! o...

Mysterious combination

I decided to learn concurrency and wanted to find out in how many ways instructions from two different processes could overlap. The code for both processes is just a 10 iteration loop with 3 instructions performed in each iteration. I figured out the problem consisted of leaving X instructions fixed at a point and then fit the other X in...

Algorithm for dynamic combinations

My code has a list called INPUTS, that contains a dynamic number of lists, let's call them A, B, C, .. N. These lists contain a dynamic number of Events I would like to call a function with each combination of Events. To illustrate with an example: INPUTS: A(0,1,2), B(0,1), C(0,1,2,3) I need to call my function this many times for ea...

Help me finish this Python 3.x self-challenge.

This is not homework. I saw this article praising Linq library and how great it is for doing combinatorics stuff, and I thought to myself: Python can do it in a more readable fashion. After half hour of dabbing with Python I failed. Please finish where I left off. Also, do it in the most Pythonic and efficient way possible please. fro...

Minimal-change algorithm which maximises 'swapping'

This is a question on combinatorics from a non-mathematician, so please try to bear with me! Given an array of n distinct characters, I want to generate subsets of k characters in a minimal-change order, i.e. an order in which generation i+1 contains exactly one character that was not in generation i. That's not too hard in itself. Howe...

What category of combinatorial problems appear on the logic games section of the LSAT?

EDIT: See http://stackoverflow.com/questions/318888/solving-who-owns-the-zebra-programmatically for a similar class of problem There's a category of logic problem on the LSAT that goes like this: Seven consecutive time slots for a broadcast, numbered in chronological order I through 7, will be filled by six song tapes-G, H, L, O, ...

Python script to calculate aded combinations from a dictionary

I am trying to write a script that will take a dictionary of items, each containing properties of values from 0 - 10, and add the various elements to select which combination of items achieve the desired totals. I also need the script to do this, using only items that have the same "slot" in common. For example: item_list = { 'item_1...