0

the vb.net code below permutates a given word...the problem i have is that it does not accept larger words like "photosynthesis", "Calendar", etc but accepts smaller words like "book", "land", etc ...what is missing...Pls help

Module Module1

Sub Main()
    Dim strInputString As String = String.Empty
    Dim lstPermutations As List(Of String)
    'Loop until exit character is read
    While strInputString <> "x"
        Console.Write("Please enter a string or x to exit: ")
        strInputString = Console.ReadLine()
        If strInputString = "x" Then
            Continue While
        End If
        'Create a new list and append all possible permutations to it.
        lstPermutations = New List(Of String)
        Append(strInputString, lstPermutations)

        'Sort and display list+stats
        lstPermutations.Sort()
        For Each strPermutation As String In lstPermutations
            Console.WriteLine("Permutation: " + strPermutation)
        Next
        Console.WriteLine("Total: " + lstPermutations.Count.ToString)
        Console.WriteLine("")
    End While
End Sub

Public Sub Append(ByVal pString As String, ByRef pList As List(Of String))
    Dim strInsertValue As String
    Dim strBase As String
    Dim strComposed As String
    'Add the base string to the list if it doesn't exist

    If pList.Contains(pString) = False Then
        pList.Add(pString)
    End If
    'Iterate through every possible set of characters
    For intLoop As Integer = 1 To pString.Length - 1
        'we need to slide and call an interative function.
        For intInnerLoop As Integer = 0 To pString.Length - intLoop
            'Get a base insert value, example (a,ab,abc)
            strInsertValue = pString.Substring(intInnerLoop, intLoop)
            'Remove the base insert value from the string eg (bcd,cd,d)
            strBase = pString.Remove(intInnerLoop, intLoop)
            'insert the value from the string into spot and check
            For intCharLoop As Integer = 0 To strBase.Length - 1
                strComposed = strBase.Insert(intCharLoop, strInsertValue)
                If pList.Contains(strComposed) = False Then
                    pList.Add(strComposed)
                    'Call the same function to review any sub-permutations.
                    Append(strComposed, pList)
                End If
            Next
        Next
    Next
End Sub

End Module

luzoma
  • 21
  • 2
    In what way does it not accept longer words? – Andrew Morton May 26 '14 at 17:21
  • if you enter words like "book", it generates all possible permutation, but if you enter a word like "photosynthesis" or even "calendar" it generates an error statement....you can run it as a console application to see – luzoma May 26 '14 at 18:27

1 Answers1

0

Without actually creating a project to run this code, nor knowing how it 'doesn't accept' long words, my answer would be that there are a lot of permutations for long words and your program is just taking much longer than you're expecting to run. So you probably think it has crashed.

UPDATE: The problem is the recursion, it's blowing up the stack. You'll have to rewrite your code to use an iteration instead of recursion. Generally explained here

http://www.refactoring.com/catalog/replaceRecursionWithIteration.html

Psuedo code here uses iteration instead of recursion

Generate list of all possible permutations of a string

Community
  • 1
  • 1
Jim W
  • 4,866
  • 1
  • 27
  • 43
  • it actually crashes..telling me that there's an unhandled stackOverflowException when larger words are entered but works fine when smaller words are entered – luzoma May 26 '14 at 18:04
  • 1
    @luzoma sorry I can't do that, I don't think it's right. – Jim W May 26 '14 at 18:37