Solved

List of combinations

Posted on 2014-11-24
14
100 Views
Last Modified: 2016-02-18
After help with code

Need to list all combinations in groups of 3 where number cannot list more than once in the 3 number combo

Eg GOOD

1 - 2 - 3
1 - 2 - 4
1 - 2 - 5

Bad

1-1-1
1-1-2
1-1-3
1-2-1
etc
0
Comment
Question by:Jason
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40462001
That would normally be done using three loops:
For loop1 as integer = 1 to 10
   For loop2 as integer = loop1 + 1 to 10
      For loop3 as integer = loop 2 + 1 to 10
<your action here>
      Next
  Next
Next

Open in new window

0
 

Author Comment

by:Jason
ID: 40462019
Don't think that will work
Eg
2-1-3
Doesn't fit the formula
0
 

Author Comment

by:Jason
ID: 40462027
Eg combos for 1 to 3
123
132
213
231
321
312
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40462046
Ok...Something like this?
For loop1 as integer = 1 to 10
   For loop2 as integer = 1 to 10
      If loop1 = loop2 Then Continue For
      For loop3 as integer = 1 to 10
          If loop1 = loop3 OrElse loop2 = loop3 Then Continue For
<your action here>
      Next
  Next
Next

Open in new window

0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 40462080
Dim comb As String = ""

For a1 As Integer = 1 To 9
      For a2 As Integer = 1 To 9
            For a3 As Integer = 1 To 9
                  If a1 <> a2 AndAlso a1 <> a3 AndAlso a2 <> a3 Then
                        comb = a1.ToString() & "-" & a2.ToString() & "-" & a3.ToString()
' save each value as you wish here
                  End If

            Next
      Next
Next
0
 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 500 total points
ID: 40462204
Or you could use linq:
Module Module1
	Sub Main()
		Dim combinations = (From a As Integer In Enumerable.Range(1, 9) Let a1 As Integer = a _
		    From b As Integer In Enumerable.Range(1, 9) Let b1 As Integer = b _
		    From c As Integer In Enumerable.Range(1, 9) Let c1 As Integer = c _
		    Where Not a1.Equals(b1) AndAlso Not a1.Equals(c1) _
		    AndAlso Not b1.Equals(a1) AndAlso Not b1.Equals(c1) _
		    AndAlso Not c1.Equals(a1) AndAlso Not c1.Equals(b1) _
		    Select New Integer() {a1, b1, c1})
		For Each combination In combinations
			Console.WriteLine(String.Format("{0} - {1} - {2}", combination(0), combination(1), combination(2)))
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window


Produces the following sample output:Capture.JPG
-saige-
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40462207
Modifying for the test case 1 to 3:
Module Module1
	Sub Main()
		Dim combinations = (From a As Integer In Enumerable.Range(1, 3) Let a1 As Integer = a _
		    From b As Integer In Enumerable.Range(1, 3) Let b1 As Integer = b _
		    From c As Integer In Enumerable.Range(1, 3) Let c1 As Integer = c _
		    Where Not a1.Equals(b1) AndAlso Not a1.Equals(c1) _
		    AndAlso Not b1.Equals(a1) AndAlso Not b1.Equals(c1) _
		    AndAlso Not c1.Equals(a1) AndAlso Not c1.Equals(b1) _
		    Select New Integer() {a1, b1, c1})
		For Each combination In combinations
			Console.WriteLine(String.Format("{0} - {1} - {2}", combination(0), combination(1), combination(2)))
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window


Produces the following output:Capture.JPG
-saige-
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Expert Comment

by:it_saige
ID: 40462232
And for the coupe de gras, if you feel so inclined, you can create a method and add parameters so that you can provide your bookend integers, e.g. -
Module Module1
	Private Function GetCombinations(ByVal start As Integer, ByVal [end] As Integer) As IEnumerable(Of Integer())
		Dim combinations = (From a As Integer In Enumerable.Range(start, [end]) Let a1 As Integer = a _
		  From b As Integer In Enumerable.Range(start, [end]) Let b1 As Integer = b _
		  From c As Integer In Enumerable.Range(start, [end]) Let c1 As Integer = c _
		  Where Not a1.Equals(b1) AndAlso Not a1.Equals(c1) _
		  AndAlso Not b1.Equals(a1) AndAlso Not b1.Equals(c1) _
		  AndAlso Not c1.Equals(a1) AndAlso Not c1.Equals(b1) _
		  Select New Integer() {a1, b1, c1})
		Return combinations
	End Function

	Sub Main()
		For Each combination In GetCombinations(1, 3)
			Console.WriteLine(String.Format("{0} - {1} - {2}", combination(0), combination(1), combination(2)))
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window


-saige-
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40462349
Hi Renegade_AD;

Try this function out to see if it gives you what you are looking for.

public List<string> GeneratePermutations(string value)
{
    if (value.Length > 1)
    {
        return (from character in value
                from permutation in GeneratePermutations(value.Remove(value.IndexOf(character), 1))
                select string.Format("{0} - {1}", character, permutation)).ToList();
    }
    else
    {
        return new List<string>() { value };
    }
}

Open in new window


To call it you can do this,

List<string> results = GeneratePermutations("123");

or

List<string> results = GeneratePermutations(123.ToString());
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40462419
Here is Fernando's method in VB.NET:
Module Module1
	Private Function GetCombinations(ByVal start As Integer, ByVal [end] As Integer) As IEnumerable(Of Integer())
		Dim combinations = (From a As Integer In Enumerable.Range(start, [end]) Let a1 As Integer = a _
		  From b As Integer In Enumerable.Range(start, [end]) Let b1 As Integer = b _
		  From c As Integer In Enumerable.Range(start, [end]) Let c1 As Integer = c _
		  Where Not a1.Equals(b1) AndAlso Not a1.Equals(c1) _
		  AndAlso Not b1.Equals(a1) AndAlso Not b1.Equals(c1) _
		  AndAlso Not c1.Equals(a1) AndAlso Not c1.Equals(b1) _
		  Select New Integer() {a1, b1, c1})
		Return combinations
	End Function

	Private Function GeneratePermutations(ByVal value As String) As List(Of String)
		If value.Length > 1 Then
			Return (From character In value _
			    From permutation In GeneratePermutations(value.Remove(value.IndexOf(character), 1)) _
			    Select String.Format("{0} - {1}", character, permutation)).ToList()
		Else
			Return New List(Of String) From {value}
		End If
	End Function

	Sub Main()
		Console.WriteLine("Using get combinations...")
		For Each combination In GetCombinations(1, 3)
			Console.WriteLine(String.Format("{0} - {1} - {2}", combination(0), combination(1), combination(2)))
		Next

		Console.WriteLine()
		Console.WriteLine("Using generate permutations...")
		For Each permutation In GeneratePermutations("123")
			Console.WriteLine(permutation)
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window


Produces the following output:Capture.JPG
BTW, nice use of recursion Fernando... ;)

-saige-
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40462475
Hi Renegade_AD;

As -saige- pointed out that my solution was in C# I resubmit it in my version of VB .Net.

Public Function GeneratePermutations(ByVal value As String) As List(Of String)
    if value.Length > 1
        Return (From character in value
                from permutation in GeneratePermutations(value.Remove(value.IndexOf(character), 1))
                Select String.Format("{0} - {1}", character, permutation)).ToList()
    else
        Return New List(Of String)() From { value }
    End If
End Function

Open in new window


To call it you can do this,

Dim results As List(Of String) = GeneratePermutations("123");

or

Dim results As List(Of String) = GeneratePermutations(123.ToString());

@ -saige-, Thank you.
0
 

Author Comment

by:Jason
ID: 40463156
@it saige
your code works great.  one adjustment what about if i had a rage of numbers and i want them in combinations of 3
ie user enters
5
9  
7
1
3
8

output is all combination of these numbers in groups of 3
how you the code look for this
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40463202
Thats simple enough.  Now you are dealing with a distinct *list* of integers and a *list* is enumerable, so in this case we would not use the Enumerable.Range but rather the list of distinct numbers itself, e.g. -
Module Module1
	Private Function GetCombinations(ByVal numbers As IEnumerable(Of Integer)) As IEnumerable(Of Integer())
		Dim combinations = (From a As Integer In numbers _
		    From b As Integer In numbers _
		    From c As Integer In numbers _
		    Where Not a.Equals(b) AndAlso Not a.Equals(c) _
		    AndAlso Not b.Equals(a) AndAlso Not b.Equals(c) _
		    AndAlso Not c.Equals(a) AndAlso Not c.Equals(b) _
		    Select New Integer() {a, b, c})
		Return combinations
	End Function

	Sub Main()
		Dim numbers As Integer() = {5, 9, 7, 1, 3, 8}
		Console.WriteLine("Using distinct combinations...")
		For Each combination In GetCombinations(numbers)
			Console.WriteLine(String.Format("{0} - {1} - {2}", combination(0), combination(1), combination(2)))
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window


Produces the following output:Capture.JPG
Now we can simplify the original method by just calling the new GetCombinations method with the ranged enumerable:
Module Module1
	Private Function GetCombinations(ByVal start As Integer, ByVal [end] As Integer) As IEnumerable(Of Integer())
		Return GetCombinations(Enumerable.Range(start, [end]))
	End Function

	Private Function GetCombinations(ByVal numbers As IEnumerable(Of Integer)) As IEnumerable(Of Integer())
		Dim combinations = (From a As Integer In numbers _
		    From b As Integer In numbers _
		    From c As Integer In numbers _
		    Where Not a.Equals(b) AndAlso Not a.Equals(c) _
		    AndAlso Not b.Equals(a) AndAlso Not b.Equals(c) _
		    AndAlso Not c.Equals(a) AndAlso Not c.Equals(b) _
		    Select New Integer() {a, b, c})
		Return combinations
	End Function

	Sub Main()
		Dim numbers As Integer() = {5, 9, 7, 1, 3, 8}
		Console.WriteLine("Using get combinations...")
		For Each combination In GetCombinations(1, 3)
			Console.WriteLine(String.Format("{0} - {1} - {2}", combination(0), combination(1), combination(2)))
		Next
		Console.WriteLine("Press any key to continue...")
		Console.ReadLine()

		Console.WriteLine()
		Console.WriteLine("Using distinct combinations...")
		For Each combination In GetCombinations(numbers)
			Console.WriteLine(String.Format("{0} - {1} - {2}", combination(0), combination(1), combination(2)))
		Next
		Console.ReadLine()
	End Sub
End Module

Open in new window


-saige-
0
 

Author Closing Comment

by:Jason
ID: 40463961
Great help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now