Possible combinations calculator

南笙酒味 提交于 2021-01-29 06:52:57

问题


I would like to calculate all possible combinations in an array of digits (0 to 9), uppercase letters (A to Z) and lowercase letters (a to z).

Array contain only 10 digits or letters at time.

I'm trying this in vb.net but I found it a little confusing.

Anyone have an idea how to do this?


回答1:


Here's an iterative approach that would allow you to start the sequence from any position. This way you could stop the sequence, store the current value, then later restart it from the same position even if you closed the application (obviously you'd have to store that value somewhere like a text file).

This is simply a modified version of my code here: http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_23723548.html#a22452485

The Sleep() call is just so you can see the values going by in the Label:

Public Class Form1

    Private Rev As Revision

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' You can start from anywhere in the sequence by passing in a different starting value to the second parameter below:
        Rev = New Revision("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "aaaaaaaaaa")
        Label1.Text = Rev.CurrentRevision
    End Sub

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        System.Threading.Thread.Sleep(100)
        Dim curRevision As String = Rev.NextRevision
        While curRevision.Length = 10
            BackgroundWorker1.ReportProgress(-1, curRevision)
            System.Threading.Thread.Sleep(50)
            curRevision = Rev.NextRevision
        End While
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Label1.Text = e.UserState
    End Sub

End Class

Public Class Revision

    Private chars As String
    Private values() As Char
    Private curRevision As System.Text.StringBuilder

    Public Sub New()
        Me.DefaultRevision()
    End Sub

    Public Sub New(ByVal validChars As String)
        If validChars.Length > 0 Then
            chars = validChars
            values = chars.ToCharArray()
            curRevision = New System.Text.StringBuilder(values(0))
        Else
            Me.DefaultRevision()
        End If
    End Sub

    Public Sub New(ByVal validChars As String, ByVal startingRevision As String)
        Me.New(validChars)
        curRevision = New System.Text.StringBuilder(startingRevision)
        Dim i As Integer
        For i = 0 To curRevision.Length - 1
            If Array.IndexOf(values, curRevision.Chars(i)) = -1 Then
                curRevision = New System.Text.StringBuilder(values(0))
                MessageBox.Show("Revision has been reset." & vbCrLf & "Current Revision = " & Me.CurrentRevision, "Starting Revision contains an Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit For
            End If
        Next
    End Sub

    Private Sub DefaultRevision()
        chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        values = chars.ToCharArray
        curRevision = New System.Text.StringBuilder(values(0))
    End Sub

    Public ReadOnly Property ValidChars() As String
        Get
            Return chars
        End Get
    End Property

    Public ReadOnly Property CurrentRevision() As String
        Get
            Return curRevision.ToString()
        End Get
    End Property

    Public Function NextRevision(Optional ByVal numRevisions As Integer = 1) As String
        Dim forward As Boolean = (numRevisions > 0)
        numRevisions = Math.Abs(numRevisions)
        Dim i As Integer
        For i = 1 To numRevisions
            If forward Then
                Me.Increment()
            Else
                Me.Decrement()
            End If
        Next
        Return Me.CurrentRevision
    End Function

    Private Sub Increment()
        Dim curChar As Char = curRevision.Chars(curRevision.Length - 1)
        Dim index As Integer = Array.IndexOf(values, curChar)
        If index < (chars.Length - 1) Then
            index = index + 1
            curRevision.Chars(curRevision.Length - 1) = values(index)
        Else
            curRevision.Chars(curRevision.Length - 1) = values(0)
            Dim i As Integer
            Dim startPosition As Integer = curRevision.Length - 2
            For i = startPosition To 0 Step -1
                curChar = curRevision.Chars(i)
                index = Array.IndexOf(values, curChar)
                If index < (values.Length - 1) Then
                    index = index + 1
                    curRevision.Chars(i) = values(index)
                    Exit Sub
                Else
                    curRevision.Chars(i) = values(0)
                End If
            Next
            curRevision.Insert(0, values(0))
        End If
    End Sub

    Private Sub Decrement()
        Dim curChar As Char = curRevision.Chars(curRevision.Length - 1)
        Dim index As Integer = Array.IndexOf(values, curChar)
        If index > 0 Then
            index = index - 1
            curRevision.Chars(curRevision.Length - 1) = values(index)
        Else
            curRevision.Chars(curRevision.Length - 1) = values(values.Length - 1)
            Dim i As Integer
            Dim startPosition As Integer = curRevision.Length - 2
            For i = startPosition To 0 Step -1
                curChar = curRevision.Chars(i)
                index = Array.IndexOf(values, curChar)
                If index > 0 Then
                    index = index - 1
                    curRevision.Chars(i) = values(index)
                    Exit Sub
                Else
                    curRevision.Chars(i) = values(values.Length - 1)
                End If
            Next
            curRevision.Remove(0, 1)
            If curRevision.Length = 0 Then
                curRevision.Insert(0, values(0))
            End If
        End If
    End Sub

End Class



回答2:


The number of combination of 10 places with 26*2+10=62 characters per each will give 62^10 different strings. If you want to store them in a memory.. well good luck. If you want to process them one by one..good luck as well. But generally the method is recursive (pseudocode:

perms(n, chars)
   while (characters remaining in chars)
       firstChar = getNextCharacterFrom(chars)
       output <- firstChar + perms(n-1, chars)


来源:https://stackoverflow.com/questions/26981733/possible-combinations-calculator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!