Solved

stopping a number from permutating twice

Posted on 2006-10-23
5
151 Views
Last Modified: 2010-04-30
When I enter a number to be permutated, the program permutates the entered number twice I.E
If I enter the number 898 the program returns 898, 988, 889, 898, 988, 889
 The program works fine when i enter a number like 123 or 1234.
will not work for a number like 1122, 1223 these type of numbers get permutated twice

Here is a copy of the program

Dim CurrentRow
Sub GetString()
    Dim InString As String
    InString = InputBox("Enter text to permute:")
    Range("a:a").ClearContents
    If Len(InString) < 2 Then Exit Sub
    If Len(S) >= 8 Then
        MsgBox "Too many permutations!"
        Exit Sub
    Else
        ActiveSheet.Columns(1).Clear
        CurrentRow = 1
        Call GetPermutation("", InString)
    End If
End Sub

Sub GetPermutation(x As String, y As String)
    Dim i As Integer, j As Integer
    j = Len(y)
    If j < 2 Then
        Cells(CurrentRow, 1) = x & y
        CurrentRow = CurrentRow + 1
    Else
        For i = 1 To j
            Call GetPermutation(x + Mid(y, i, 1), _
            Left(y, i - 1) + Right(y, j - i))
        Next
    End If
0
Comment
Question by:tbell000
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:JackOfPH
ID: 17793268
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 17793273
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17793298
Hi,

I would just dump everything into a dictionary (include the microsoft scripting runtime library) and if it exists then not repeat it then just go through and output everything in the array

ie

Dim CurrentRow
Sub GetString()
    Dim InString As String
    InString = InputBox("Enter text to permute:")
    Range("a:a").ClearContents
    If Len(InString) < 2 Then Exit Sub
    If Len(S) >= 8 Then
        MsgBox "Too many permutations!"
        Exit Sub
    Else
        ActiveSheet.Columns(1).Clear
        CurrentRow = 1
        Dim n As New Dictionary
        Call GetPermutation("", InString, n)
    End If
End Sub

Sub GetPermutation(x As String, y As String, n As Dictionary)
    Dim i As Integer, j As Integer
    j = Len(y)
    If j < 2 Then
        If n.Exists(x & y) = False Then
            Cells(CurrentRow, 1) = x & y
            CurrentRow = CurrentRow + 1
            n.Add x & y, 1
        End If
    Else
        For i = 1 To j
            Call GetPermutation(x + Mid(y, i, 1), _
            Left(y, i - 1) + Right(y, j - i), n)
        Next
    End If
End Sub
0
 
LVL 35

Accepted Solution

by:
Raynard7 earned 500 total points
ID: 18024695
My Option is tested and works, those provided by JackOfPh were not exactly on point.
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

Suggested Solutions

Title # Comments Views Activity
VBA saving file message display 5 56
VBS file using code from 2nd file (txt or vbs) 4 32
Copy a row 12 59
Visual Studio 2005 text editor 10 35
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

867 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

14 Experts available now in Live!

Get 1:1 Help Now