0

Below is link to one solution for similar problem. What I want to add to this solution is that combinations should be limited only to those items which satisfy total sum condition.

https://stackoverflow.com/a/10629938/4347577

Thanks in advance

Community
  • 1
  • 1
dannyboy
  • 13
  • 1
  • To make sure I understand this correctly - do you mean that if you have a list of numbers, say `1,2,3`, and your sum is 4, you want all combinations of those numbers that add up to 4, i.e. `{1,3} {2,2} {3,1}`? – AJ Richardson Dec 10 '14 at 23:08
  • We're not a code writing service. We can help you fix your code. What have you tried? – Enigmativity Dec 10 '14 at 23:09
  • @aj_r yes, you are right! Enigmativity I have spending few days to solve this, and finaly I give up. Because of it I asked here. This is what I tryed: Private Shared Function GetPermutationsWithRept1(Of T)(list As IEnumerable(Of T), length As Integer) As IEnumerable(Of IEnumerable(Of T)) If length = 1 Then Return list.[Select](Function(p) New T() {p}) Return GetPermutationsWithRept1(list, length-1).SelectMany(Function(p) list.Where(Function(o) p.Sum() = 100), Function(t1, t2) t1.Concat(New T() {t2})) End Function Problem is with p.Sum() because I don't know how to put corect parameters – dannyboy Dec 10 '14 at 23:22
  • What I have to mention is that I am not professional programer, I am structural engineer but in meanwhile I like to code. – dannyboy Dec 10 '14 at 23:25

1 Answers1

0

The simplest solution is to add a where clause that filters for the results that you want. For example:

public static IEnumerable<IEnumerable<int>> GetPermutationsWithSum(
    IEnumerable<int> list, int length, int sum)
{
    return GetPermutationsWithRept(list, length).Where(lst => lst.Sum() == sum);
}

Here is the definition for GetPermutationsWithRept (from the answer you linked to in your question)

public static IEnumerable<IEnumerable<T>> GetPermutationsWithRept<T>(
    IEnumerable<T> list, int length)
{
    if (length == 1) return list.Select(t => new T[] { t });
    return GetPermutationsWithRept(list, length - 1)
        .SelectMany(t => list, (t1, t2) => t1.Concat(new T[] { t2 }));
}
AJ Richardson
  • 6,610
  • 1
  • 49
  • 59
  • Ok, let me know if it works for you. Also note that I use `int` instead of `T` because there is no concept of a sum for generic types - you have to use a numeric type. – AJ Richardson Dec 11 '14 at 00:13
  • I am testing right now, still I am not sure but maybe will be problem with this. I am getting always same number of combinations regarding does I specify "length" of 3 or 4 or more. In list exist 0-101 integers, lenght varies from 2 to 5..., sum is 100. After I finish will inform you. Thank you... – dannyboy Dec 11 '14 at 00:18
  • It looks that the combinations are calculated only for first two members of length (1-2...), others members in length (3-5) keep zero values. – dannyboy Dec 11 '14 at 00:36
  • Oops! I forgot to account for the fact that the method is recursive. I have updated my answer and tested to make sure it works in those cases. – AJ Richardson Dec 11 '14 at 01:36
  • Now working great! Unfortunately my reputation is low so I couldn't vote on this. But, I am very thankful for your help. – dannyboy Dec 11 '14 at 08:07
  • Great! You can still mark this as the answer if it is what you were looking for. – AJ Richardson Dec 11 '14 at 14:32