combinations

Find the best combination from a given set of multiple sets

Say you have a shipment. It needs to go from point A to point B, point B to point C and finally point C to point D. You need it to get there in five days for the least amount of money possible. There are three possible shippers for each leg, each with their own different time and cost for each leg: Array ( [leg0] => Array ( ...

Algorithm problem: letter combinations

I'm trying to write a piece of code that will do the following: Take the numbers 0 to 9 and assign one or more letters to this number. For example: 0 = N, 1 = L, 2 = T, 3 = D, 4 = R, 5 = V or F, 6 = B or P, 7 = Z, 8 = H or CH or J, 9 = G When I have a code like 0123, it's an easy job to encode it. It will obviously make up the code NL...

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 (or words) in return consisting of 3 letters each. ...

Most binary combinations from 4 bits - with each bit able to change state only once

I have 4 binary bits Bit 3 Bit 2 Bit 1 Bit 0 Normally the answer is simple: 2^4, or 16 different combinations; and it would looks something like the following: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 However, The LSB (Bit 0) changes state every iteration I need an algorithm where the state ...

Python code to pick out all possible combinations from a list?

Hi, I have a list with 15 numbers in, and I need to write some code that produces all 32,768 combinations of those numbers. I've found some code (by googling) that apparently does what I'm looking for, but I found the code fairly opaque and am wary of using it. Plus I have a feeling there must be a more elegant solution. The only thi...

Generate combinations ordered by an attribute

Hi, I'm looking for a way to generate combinations of objects ordered by a single attribute. I don't think lexicographical order is what I'm looking for... I'll try to give an example. Let's say I have a list of objects A,B,C,D with the attribute values I want to order by being 3,3,2,1. This gives A3, B3, C2, D1 objects. Now I want to g...

Combination of List<List<int>>

I've a List of this type List> that contains this List<int> A = new List<int> {1, 2, 3, 4, 5}; List<int> B = new List<int> {0, 1}; List<int> C = new List<int> {6}; List<int> X = new List<int> {....,....}; I want to have all combinations like this 1-0-6 1-1-6 2-0-6 2-1-6 3-0-6 and so on. According to you is This possibile to resolv...

Find a combination of two elements that have not been viewed together (LINQ, SQL or C#)

Hi, I have a page that displays two objects and then the user picks one of these. I record the preference and the combination in a MSSQL database and end up storing data like this: UserId=1, BetterObjectId=1, WorseObjectId=2 Now I would like to avoid showing that combination of objects (1,2 / 2,1) ever again. So how do I generate ra...

Calculate all combinations of a series

I have a list of items, and each item has a quantity. var items = { 1: 12, // we have 12 x item1 2: 1, // we have 1 x item2 3: 1, 4: 7, 5: 2, 6: 2 }; Alternatively this could be viewed as: var items = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 6]; How would you...

Combination Generator in Linq

Hi, Is it possible to create some Linq that generates a List containing all possible combinations of a series of numbers?? If you enter "21" it would generate a list with the elements: list[0] = "21" list[1] = "22" list[2] = "11" list[3] = "12" (Not nessesarily in that order) I understand you can use range to do things like: List<...

All combinations of a list of lists

I'm basically looking for a python version of Combination of List<List<int>> Given a list of lists, I need a new list that gives all the possible combinations of items between the lists. [[1,2,3],[4,5,6],[7,8,9,10]] -> [[1,4,7],[1,4,8],...,[3,6,10]] The number of lists is unknown, so I need something that works for all cases. Bonus p...

Possible number of combinations for return travel routes

My math is bad, real bad. So bad I'm struggling to even phrase this question, but here goes. The situation is train travel and you have four arrays to work with. Leaving_Stations Arriving_Stations Leaving_Dates Returning_Dates So let's say you're only interested in one way routes and you need to figure out how many combinations of r...

Generating random couples in C#

I have a table in my DB with a list of people. I need to create a list of random buddies every day. The idea is that every day every person is paired with a differenct random person for that day. Since the table may get very large I was wondering what would be the best way to do such a thing? I have thought of 2 ideas but I am not so ...

Fixed Place Permutation/Combination

I am looking for a way where I can generated different combination of 4 sets elements in such a manner that every set's element has a fixed place in the final combination: To explain better my requirement let me give sample of those 4 sets and finally what I am looking for: Set#1(Street Pre Direction) { N, S } Set#2(Street Name) {Frankf...

How would you write this algorithm for large combinations in the most compact way?

The number of combinations of k items which can be retrieved from N items is described by the following formula. N! c = ___________________ (k! * (N - k)!) An example would be how many combinations of 6 Balls can be drawn from a drum of 48 Balls in a lottery draw. Optimize this formula to run with the smallest ...

Retrieving MySQL records based on a variable set of points of comparison.

Let's say I have a MySQL table, people. Each record comprises a variety of properties, among these favourite_colour, country, and age_group. What I would like to do is retrieve records from this table by their similarity to a set of specific parameters. Given "Red", "United States", and "18-25", for example, the best results would be th...

Speed dating algorithm

I work in a consulting organization and am most of the time at customer locations. Because of that I rarely meet my colleagues. To get to know each other better we are going to arrange a dinner party. There will be many small tables so people can have a chat. In order to talk to as many different people as possible during the party, ever...

Generating Ordered (Weighted) Combinations of Arbitrary Length in PHP

Given a list of common words, sorted in order of prevalence of use, is it possible to form word combinations of an arbitrary length (any desired number of words) in order of the 'most common' sequences. For example,if the most common words are 'a, b, c' then for combinations of length two, the following would be generated: aa ab ba bb a...

using two for loops in python

Hello, I have started to learn python recently and have a question about for loops that I was hoping someone could answer. I want to be able to print all the possible products of two numbers from one to ten. so: 2 by 2, 2 by 3, 2 by 4...2 by 10, 3 by 2, 3 by 3...3 by 10, 4 by 2, 4 by 3 etc...I would have thought the easiest way to do so ...

KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm.

This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x <= maxval and x(1) + ... + x(ncells) = targetsum. Having tested several of the more promising answers, I'm going to award the answer-prize to Lennart Regebro, because: ...