1

How can I write all possible combinations to the console? For example, if user enters abc, then it will write aaa, aab, aac, abb, abc, acc, bbb, bbc, ccc. Please help me.

Here's some code:

    Dim abc() As String = {"a", "b", "c"} '      

    Sub Main()
        Console.WriteLine("Enter the amount of characters")
        Dim count As Integer = Console.ReadLine
        outputStrings("", count)

        Console.ReadLine()
    End Sub

    Private Sub outputStrings(ByVal startString As String, ByVal letterCount As Integer)
        For i = 0 To abc.Length - 1
            Dim temp As String = startString           
            temp += abc(i)
            If temp.Length = letterCount Then
                Console.WriteLine(temp)

                If i = abc.Length - 1 Then
                    Console.WriteLine("----")    
                End If
            Else
                outputStrings(temp, letterCount)
            End If

        Next
    End Sub

Something has to be done after the dashed lines to remove unwanted permutation to leave out only valid combinations.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Cobold
  • 2,563
  • 6
  • 34
  • 51
  • Sorry, I marked this as a duplicate of http://stackoverflow.com/questions/6128790/how-to-create-undefined-count-of-loops but I don't think it is anymore – Chris Haas May 26 '11 at 17:23

4 Answers4

1
def go(chars,thusfar):
    if len(thusfar) = len(chars):
        print thusfar
    for char in chars:
        go(chars,thusfar+char);

This should be easy enough to translate to VB (read: I don't know VB)

dfb
  • 13,133
  • 2
  • 31
  • 52
1

You can restrict the letters used to ones at or to the right of abc(i) with an additional parameter abcIndex, and start the for loop from there. Only strings which have their letters in alphabetical order will be written, which prevents duplicates.

Private Sub outputStrings(ByVal startString As String, ByVal letterCount As Integer, ByVal abcIndex As Integer)
    For i = abcIndex To abc.Length - 1
        Dim temp As String = startString
        temp += abc(i)
        If temp.Length = letterCount Then
            Console.WriteLine(temp)
        Else
            outputStrings(temp, letterCount, i)
        End If
    Next
End Sub

Call with:

outputStrings("", 3, 0)
fgb
  • 18,439
  • 2
  • 38
  • 52
0

Amazing code from here:

Private Shared Function PermutationsWithRepetition(Of T)(list As IEnumerable(Of T), length As Integer) As IEnumerable(Of IEnumerable(Of T))
        If length = 1 Then
            Return list.[Select](Function(x) New T() {x})
        End If
        Return PermutationsWithRepetition(list, length - 1).SelectMany(Function(x) list, Function(t1, t2) t1.Concat(New T() {t2}))
    End Function

Can be used with Integer, Char, Double etc. Example of use:

Dim myarray(1) As Integer
myarray(0) = 1
myarray(1) = 2
Dim k As Integer = 2 'number of "slots" to do the permutations

mypermutations = PermutationsWithRepetition(myarray,k)

For Each row As IEnumerable(Of Integer) In mypermutations
            Console.WriteLine("")
            For Each col As IntegerIn row
                Console.Write(col.toString())
            Next            
Next

Output:

11 12 21 22

Carlos Borau
  • 1,433
  • 1
  • 24
  • 34
0

You just need to make a recursive call there.

Dim abc() As String = {"a", "b", "c"} '      

Sub Main()
    Console.WriteLine("Enter the amount of characters")
    Dim count As Integer = Console.ReadLine
    outputStrings("", count)

    Console.ReadLine()
End Sub

Private Sub outputStrings(ByVal startString As String, ByVal letterCount As Integer)
    For i = 0 To abc.Count - 1
        Dim temp As String = startString           
        temp += abc(i)
        If temp.Length = letterCount Then
            Console.WriteLine(temp)
        Else
            outputStrings(temp, letterCount)
        End If

    Next
End Sub

Do note that if someone enters a negative number that your code will run forever. I'll leave fixing that as an easy exercise.

btilly
  • 43,296
  • 3
  • 59
  • 88
  • That doesn't make any sense calling outputString. It just generates a runtime exception. – Cobold May 26 '11 at 17:08
  • @Cobold: `outputStrings` not `outputString`. If you fixed that and you're still getting a runtime exception then I suspect that it is not reading the number as you think it should be reading the number. Try hardcoding 3 instead of passing `count` in and see if that helps. As for "doesn't make any sense", watch http://msdn.microsoft.com/en-us/beginner/bb308749.aspx and hopefully you'll become enlightened about recursion. – btilly May 26 '11 at 17:18
  • I mean you don't have to call the method, instead you have to remove unwanted characters. For example: aa, ab, ac, then remove a, because anything else with a would be the same, like ba is the same as ab. Then bb, bc, remove b, and finally cc. – Cobold May 26 '11 at 17:36
  • @Cobold: I would strongly suggest verifying that the code works and then trying to understand why it works, instead of trying to figure out how to make it work in a way it doesn't. Alternately you can realize that the replacement of the last unwanted character happens implicitly when you go to the next iteration of the loop and initialize `temp` to something new. But that is a confusing way to understand this code. – btilly May 26 '11 at 17:52
  • @Cobold: Reading more closely you had an extra recursive call that had no point, which I had blindly copied. I removed that. Does that work better now? – btilly May 26 '11 at 19:04