Jason
asked on
List of combinations
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
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
ASKER
Don't think that will work
Eg
2-1-3
Doesn't fit the formula
Eg
2-1-3
Doesn't fit the formula
ASKER
Eg combos for 1 to 3
123
132
213
231
321
312
123
132
213
231
321
312
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
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Modifying for the test case 1 to 3:
Produces the following output:
-saige-
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
Produces the following output:
-saige-
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. -
-saige-
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
-saige-
Hi Renegade_AD;
Try this function out to see if it gives you what you are looking for.
To call it you can do this,
List<string> results = GeneratePermutations("123" );
or
List<string> results = GeneratePermutations(123.T oString()) ;
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 };
}
}
To call it you can do this,
List<string> results = GeneratePermutations("123"
or
List<string> results = GeneratePermutations(123.T
Here is Fernando's method in VB.NET:
Produces the following output:
BTW, nice use of recursion Fernando... ;)
-saige-
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
Produces the following output:
BTW, nice use of recursion Fernando... ;)
-saige-
Hi Renegade_AD;
As -saige- pointed out that my solution was in C# I resubmit it in my version of VB .Net.
To call it you can do this,
Dim results As List(Of String) = GeneratePermutations("123" );
or
Dim results As List(Of String) = GeneratePermutations(123.T oString()) ;
@ -saige-, Thank you.
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
To call it you can do this,
Dim results As List(Of String) = GeneratePermutations("123"
or
Dim results As List(Of String) = GeneratePermutations(123.T
@ -saige-, Thank you.
ASKER
@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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great help
Open in new window