This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Generating all Possible Combinations
Is there a good LINQ way to do a cartesian product?
How to generate combination of N elements with limited supply of 2 each without explicit nested loops
I have a list of lists, and I want to iterate all the possible combinations where I choose one element from each inner list. This is pretty straightforward if I know at compile-time how many lists there are, but how can I do it when I don't know in advance how many lists there will be?
If I have three lists (and if I know, at compile-time, that there will be exactly three lists), and I want all the combinations of choosing a single element from each of the three lists, I can do that easily with a LINQ query:
var list1 = new[] { 1, 2 };
var list2 = new[] { 3, 4 };
var list3 = new[] { 5, 6 };
var combinations = from item1 in list1
from item2 in list2
from item3 in list3
select new[] { item1, item2, item3 };
// Results:
// {1, 3, 5}
// {1, 3, 6}
// {1, 4, 5}
// {1, 4, 6}
// {2, 3, 5}
// {2, 3, 6}
// {2, 4, 5}
// {2, 4, 6}
But how can I do the same thing when I don't know at compile-time how many lists there will be?
var lists = new[] {
new[] { 1, 2 },
new[] { 3, 4 },
new[] { 5, 6 } };
var combinations = ???;
// This particular example happens to be the same inputs as above, so it
// has the same expected outputs. But there could be two lists instead,
// or four, so the three hard-coded "from" clauses won't work.
It seems like this should actually be doable in LINQ -- SelectMany already does the equivalent of two nested foreach loops, so all I need to do is do a bunch of SelectMany calls and then combine all the results with another SelectMany. Or something. But when it starts getting meta like that, my brain gets all tied in knots. I can't get a handle on how to put the pieces together. I can't even figure out what the generic type arguments to the outer SelectMany call would be.
How can I iterate those lists of lists, and return all the combinations, without knowing at compile-time how many lists there will be?
(Note: everywhere I used arrays above, I'd be fine with using IEnumerable<T> instead. Arrays are easier to write in sample code, but I'm expecting that the output is more likely to be in the form IEnumerable<IEnumerable<int>> rather than the int[][] I show in my sample output above.)
You don't use SelectMany to combine the SelectMany calls; you use Aggregate. Code courtesy of Eric Lippert (answering on a question that's much more specific than this one, but giving a general answer that fits this question as well):
static IEnumerable<IEnumerable<T>> CartesianProduct<T>(
this IEnumerable<IEnumerable<T>> sequences)
{
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
return sequences.Aggregate(
emptyProduct,
(accumulator, sequence) =>
from accseq in accumulator
from item in sequence
select accseq.Concat(new[] {item}) :
);
}
As with all of Eric's answers, he includes a detailed discussion that lays out exactly how and why this works, in terms of the equivalent non-LINQ code.
Related
I have a vector of structs, each struct has a numerical ID that I am using to sort the vector items by. I want the IDs to be sorted, but to also appear in the order they did in the original vector after sorting. Let me explain...
Suppose you have a vector like this (ignoring the structs):
vector<int> items = {
1,
2,
5, // First 5
8,
9,
6,
5, // Second 5
4,
7,
3,
5, // Third 5
10
};
After sorting I want the vector to look like this:
vector<int> items = {
1,
2,
3,
4,
5, // First 5
5, // Second 5
5, // Third 5
6,
7,
8,
9,
10
};
Remember, these items would actually be structs. Multiple can have the same ID, but different values for the other properties. Right now, I don't think the structs have a predictable order after sorting. Is there a way to ensure this kind output? Could I add another property to the structs indicating their original order and somehow use that in the sorting algorithm perhaps?
What you're looking for is called a "stable sort", and the C++ standard library provides it as std::stable_sort; when items compare equal, they appear in the same order they appeared in the original data set. Plain std::sort makes no such guarantees (and can therefore use slightly more efficient algorithms for the sorting that won't preserve order for equal elements), but std::stable_sort requires the use of algorithms that do make that guarantee.
I am trying to figure out a problem. I have a vector with elements:
1, 2, 3, 4
and I am wondering if it is possible to increase (all or some) values at once in O(1) complexity.
for example I might want to add 3 so I have:
4, 5, 6, 7.
Is it even possible? Maybe you can give me other insight how to approach this problem.
Thank you in advance
If you mean O(1), then yes, it's possible.
1, 2, 3, 4 are just human conditional notations/symbols of volumes one, two, three, four. You can define the symbol 1 is four, 2 is five... Do you understand what I am getting at?
You can implement your own vector class, and override the constant vector's subscript operator for interpreting real values differently:
int operator[](size_t idx) const {
return v_[idx] + 3; // v_ is an encapsulated std::vector
}
// Or like below, but inheriting std containers is not considered
// a good practice.
int operator[](size_t idx) const {
return std::vector<int>::operator[](idx) + 3;
}
So, you fill your vector with the values 1, 2, 3, 4 and read the values 4, 5, 6, 7 from a const vector reference.
I am very certain that I saw the syntax for what I want to do on this website a while ago, but I cannot find it anymore and I forgot what it was. Say I wanted to store the values from 1 to a 100 in an array. What would be the syntax without using a loop. I remember the syntax being something like this:
int line [] = {1 .. 100};
What is the correct syntax?
UPDATE: I figured out what I meant by this question. What I read a while ago was the syntax to have all the elements in an array equal to a number. For example, in a GCC compiler, you can set all the elements in an array equal to zero like this:
int line [10] = {[0 ... 9] = 0};
By doing this, all the elements in the array will be equal to 0. It is a very useful thing to know in my opinion and much easier than using a for-loop.
Try like this:
const int line[5] = { 1, 2, 3, 4, 5 };
How is a list such as:
{{1,2}, 3, {5, 6, 7}, 8, 9}
represented in Go? I need to be able to pass a nested variable size list to a function in Go from the main function. This list will then be processed inside the function by accessing the individual elements/sublists of the list.
Note that the size of the list and the sizes of sublists are variable.
You have two basic choices:
First, you can make a slice of slices:
a := [][]int{
[]int{1,2},
[]int{3},
[]int{5, 6, 7},
[]int{8},
[]int{9},
}
This isn't quite what you wanted, but it is close. In this case single items are represented as a slice with one item. Simple.
Second, you can make a slice of interface{} and use type switches to identify and unpack each index:
b := []interface{}{
[]int{1,2},
3,
[]int{5, 6, 7},
8,
9,
}
This is much more complicate to work with, but is also much closer to what you want.
Example of reading items from slice b:
// Note that things can get a lot more complicated if you have
// more types and/or you nest deeper than one level.
for _, iv := range b {
switch v := iv.(type) {
case []int:
// v is an int slice
case int:
// v is an int
default:
// This will probably be an error case.
}
}
I recomend you stick with the first method unless you have a really good reason not to.
Is it possible to characterize an integer array in C++? Once characterized, arrays containing same set of elements will have same characteristics.
I was thinking on lines of hashcode, each hashcode will uniquely identify an array!
For example ary[]={4,5,3,2,4} and ary_two[]={4,4,2,3,5} should both have same characteristics/ hashcode!
I am trying to solve this question( asked in an interview ): A number of variable sized arrays are being generated. For each array determine if we have encountered an array before containing the same elements as this array!
Investigate std::hash. You can probably overload it to do what you want. For instance, if you want the arrays with values {4, 5, 3, 2, 4} and {4, 4, 2, 3, 5} to hash to the same value, you could specialize it like this:
template<> struct hash<std::array<int, 5>>
{
size_t operator()(const std::array<int, 5> &ary) const
{
return std::accumulate(std::begin(ary), std::end(ary), 0U) * 16777619;
}
};
One possible solution would be to use the elements hashes themselves (assuming the content of the array is hashable). Then just fold them together with some suitable function (e.g. an xor or better yet +). Make sure the folding function is commutative and associative, or the order of the array will make a difference.