PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] How do you do permutation with 2 numbers in Visual Basic Express 2008?-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] How do you do permutation with 2 numbers in Visual Basic Express 2008?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Posts
    24

    Resolved [RESOLVED] How do you do permutation with 2 numbers in Visual Basic Express 2008?

    If you have two text boxes and when you input two numbers it outputs the permutations in a third text box of the two numbers.
    Example: You put 5 in textbox1 and 6 in textbox2
    textbox3 outputs:
    5,6
    6,5
    I know this can be done with nested For loops, but I don't know exactly how.

  2. #2
    Junior Member
    Join Date
    Oct 2010
    Posts
    24

    Re: How do you do permutation with 2 numbers in Visual Basic Express 2008?

    I don't know what level you're at, but below is a (very) dirty solution. I'm still learning VB.NET myself, so it might be worth waiting around for something and someone better. Obviously using poor naming conventions like I have is not a good idea, I wouldn't usually stick the contents of a textbox straight into an array, for example.

    Code:
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim arrNumbers() As Integer = {CInt(TextBox1.Text), CInt(TextBox2.Text)}
            Dim upperBound As Integer = arrNumbers.GetUpperBound(0)
            Dim lowerBound As Integer = arrNumbers.GetLowerBound(0)
    
            Array.Sort(arrNumbers)
            For i As Integer = lowerBound To upperBound
                TextBox3.AppendText(arrNumbers(i).ToString())
                If upperBound <> i Then
                    TextBox3.AppendText(",")
                End If
            Next i
            Array.Sort(arrNumbers)
            TextBox3.AppendText(vbNewLine)
            For j As Integer = upperBound To lowerBound Step -1
                TextBox3.AppendText(arrNumbers(j).ToString())
                If j <> lowerBound Then
                    TextBox3.AppendText(",")
                End If
            Next j
    
        End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Posts
    24

    Re: How do you do permutation with 2 numbers in Visual Basic Express 2008?

    I wish there was a built-in vb function that would allow permutation like:
    Permutate(first value, second value, third value, ..... n value) and would just output a a list of permutations.
    1, 2
    2, 1

    1,2,3
    1,3,2
    2,1,3
    2,3,1
    3,2,1
    3,1,2

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    24

    Re: How do you do permutation with 2 numbers in Visual Basic Express 2008?

    Quote Originally Posted by codekid2000 View Post
    I wish there was a built-in vb function that would allow permutation like:
    Permutate(first value, second value, third value, ..... n value) and would just
    Yeah, this is quite a classic example set in computer science. I've just google'd around and there's a few solutions out there (some more elegant than others).

    If it's for something personal, then it might be worth just taking an example posted online. If it's for school/college then it's worth sitting down with a pen and paper and working it out.

    As I said, there are some very smart guys on here who I'm sure have some permutation method kicking about in their libraries.

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,636

    Re: How do you do permutation with 2 numbers in Visual Basic Express 2008?

    Decided to take a jab at this and came up with an implementation based on this algorithm:-
    vbnet Code:
    1. Private Function GetPermutation(ByVal ParamArray elements() As String) As List(Of String)
    2.         Dim init As String = elements(0)
    3.         Dim curEleIndex As Integer = 0
    4.         Dim lstRet As New List(Of String)
    5.         Dim curNumElements As Integer = 1
    6.  
    7.         lstRet.Add(init)
    8.  
    9.         Do
    10.             curEleIndex += 1
    11.             curNumElements += 1
    12.             DuplicateList(lstRet, curNumElements)
    13.  
    14.             'Weave Index is zero-based
    15.             Dim curWeaveIndex = CountElements(lstRet.Item(0))
    16.  
    17.  
    18.             'The number being weaved is moving left
    19.             Dim bMoveLeft As Boolean = True
    20.  
    21.             For i = 0 To lstRet.Count - 1
    22.                 Dim ss As String = lstRet.Item(i)
    23.  
    24.                 lstRet(i) = WeaveInto(ss, elements(curEleIndex), curWeaveIndex)
    25.  
    26.                 If bMoveLeft Then
    27.                     curWeaveIndex -= 1
    28.                 Else
    29.                     curWeaveIndex += 1
    30.                 End If
    31.  
    32.                 If bMoveLeft And curWeaveIndex = 0 Then
    33.                     bMoveLeft = False
    34.                     i += 1
    35.                     lstRet(i) = WeaveInto(ss, elements(curEleIndex), curWeaveIndex)
    36.  
    37.                 End If
    38.  
    39.  
    40.                 If bMoveLeft = False And curWeaveIndex = curNumElements - 1 Then
    41.                     i += 1
    42.                     lstRet(i) = WeaveInto(ss, elements(curEleIndex), curWeaveIndex)
    43.  
    44.                     bMoveLeft = True
    45.                 End If
    46.  
    47.  
    48.             Next
    49.  
    50.         Loop Until curEleIndex >= UBound(elements)
    51.         Return lstRet
    52.     End Function
    53.  
    54.     Private Function GetPermutation(ByVal Lo As Integer, ByVal Hi As Integer) As List(Of String)
    55.         Dim init As String = CStr(Lo)
    56.         Dim curNum As Integer = Lo
    57.         Dim lstRet As New List(Of String)
    58.         Dim curNumElements As Integer = 1
    59.  
    60.         lstRet.Add(init)
    61.  
    62.         Do
    63.             curNum += 1
    64.             curNumElements += 1
    65.             DuplicateList(lstRet, curNumElements)
    66.  
    67.             'Weave Index is zero-based
    68.             Dim curWeaveIndex = CountElements(lstRet.Item(0))
    69.  
    70.  
    71.             'The number being weaved is moving left
    72.             Dim bMoveLeft As Boolean = True
    73.  
    74.             For i = 0 To lstRet.Count - 1
    75.                 Dim ss As String = lstRet.Item(i)
    76.  
    77.                 lstRet(i) = WeaveInto(ss, CStr(curNum), curWeaveIndex)
    78.  
    79.                 If bMoveLeft Then
    80.                     curWeaveIndex -= 1
    81.                 Else
    82.                     curWeaveIndex += 1
    83.                 End If
    84.  
    85.                 If bMoveLeft And curWeaveIndex = 0 Then
    86.                     bMoveLeft = False
    87.                     i += 1
    88.                     lstRet(i) = WeaveInto(ss, CStr(curNum), curWeaveIndex)
    89.  
    90.                 End If
    91.  
    92.  
    93.                 If bMoveLeft = False And curWeaveIndex = curNumElements - 1 Then
    94.                     i += 1
    95.                     lstRet(i) = WeaveInto(ss, CStr(curNum), curWeaveIndex)
    96.  
    97.                     bMoveLeft = True
    98.                 End If
    99.  
    100.  
    101.             Next
    102.  
    103.         Loop Until curNum >= Hi
    104.         Return lstRet
    105.     End Function
    106.  
    107.     Public Sub DuplicateList(ByRef list As List(Of String), ByVal numDups As Integer)
    108.         Dim ll As New List(Of String)
    109.         For Each S As String In list
    110.             For i = 1 To numDups
    111.                 ll.Add(S)
    112.             Next
    113.         Next
    114.  
    115.         list = ll
    116.     End Sub
    117.  
    118.     Public Function CountElements(ByVal numbers As String) As Integer
    119.         Return numbers.Split(",").Count
    120.     End Function
    121.  
    122.     Public Function WeaveInto(ByVal elements As String, ByVal newElement As String, ByVal index As Integer) As String
    123.  
    124.         Dim nums As List(Of String) = elements.Split(",").ToList
    125.  
    126.         nums.Insert(index, newElement)
    127.  
    128.         Return String.Join(",", nums.ToArray)
    129.  
    130.     End Function
    There are two functions, one that takes a paramarray of string values to calculate the permutations of and one that takes a high and a low value to calculate the permutations of all the numbers in the range.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2012
    Posts
    24

    Re: How do you do permutation with 2 numbers in Visual Basic Express 2008?

    Thanks, yea it's just for a personal thing, not really a big deal; just wanted to see if there was a way it could be done.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width