-
Mar 20th, 2012, 02:07 PM
#1
Thread Starter
Junior Member
[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.
-
Mar 20th, 2012, 03:09 PM
#2
Junior Member
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
-
Mar 20th, 2012, 03:15 PM
#3
Thread Starter
Junior Member
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
-
Mar 20th, 2012, 03:35 PM
#4
Junior Member
Re: How do you do permutation with 2 numbers in Visual Basic Express 2008?
Originally Posted by codekid2000
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.
-
Mar 20th, 2012, 06:04 PM
#5
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:
Private Function GetPermutation(ByVal ParamArray elements() As String) As List(Of String)
Dim init As String = elements(0)
Dim curEleIndex As Integer = 0
Dim lstRet As New List(Of String)
Dim curNumElements As Integer = 1
lstRet.Add(init)
Do
curEleIndex += 1
curNumElements += 1
DuplicateList(lstRet, curNumElements)
'Weave Index is zero-based
Dim curWeaveIndex = CountElements(lstRet.Item(0))
'The number being weaved is moving left
Dim bMoveLeft As Boolean = True
For i = 0 To lstRet.Count - 1
Dim ss As String = lstRet.Item(i)
lstRet(i) = WeaveInto(ss, elements(curEleIndex), curWeaveIndex)
If bMoveLeft Then
curWeaveIndex -= 1
Else
curWeaveIndex += 1
End If
If bMoveLeft And curWeaveIndex = 0 Then
bMoveLeft = False
i += 1
lstRet(i) = WeaveInto(ss, elements(curEleIndex), curWeaveIndex)
End If
If bMoveLeft = False And curWeaveIndex = curNumElements - 1 Then
i += 1
lstRet(i) = WeaveInto(ss, elements(curEleIndex), curWeaveIndex)
bMoveLeft = True
End If
Next
Loop Until curEleIndex >= UBound(elements)
Return lstRet
End Function
Private Function GetPermutation(ByVal Lo As Integer, ByVal Hi As Integer) As List(Of String)
Dim init As String = CStr(Lo)
Dim curNum As Integer = Lo
Dim lstRet As New List(Of String)
Dim curNumElements As Integer = 1
lstRet.Add(init)
Do
curNum += 1
curNumElements += 1
DuplicateList(lstRet, curNumElements)
'Weave Index is zero-based
Dim curWeaveIndex = CountElements(lstRet.Item(0))
'The number being weaved is moving left
Dim bMoveLeft As Boolean = True
For i = 0 To lstRet.Count - 1
Dim ss As String = lstRet.Item(i)
lstRet(i) = WeaveInto(ss, CStr(curNum), curWeaveIndex)
If bMoveLeft Then
curWeaveIndex -= 1
Else
curWeaveIndex += 1
End If
If bMoveLeft And curWeaveIndex = 0 Then
bMoveLeft = False
i += 1
lstRet(i) = WeaveInto(ss, CStr(curNum), curWeaveIndex)
End If
If bMoveLeft = False And curWeaveIndex = curNumElements - 1 Then
i += 1
lstRet(i) = WeaveInto(ss, CStr(curNum), curWeaveIndex)
bMoveLeft = True
End If
Next
Loop Until curNum >= Hi
Return lstRet
End Function
Public Sub DuplicateList(ByRef list As List(Of String), ByVal numDups As Integer)
Dim ll As New List(Of String)
For Each S As String In list
For i = 1 To numDups
ll.Add(S)
Next
Next
list = ll
End Sub
Public Function CountElements(ByVal numbers As String) As Integer
Return numbers.Split(",").Count
End Function
Public Function WeaveInto(ByVal elements As String, ByVal newElement As String, ByVal index As Integer) As String
Dim nums As List(Of String) = elements.Split(",").ToList
nums.Insert(index, newElement)
Return String.Join(",", nums.ToArray)
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.
-
Apr 30th, 2012, 02:02 PM
#6
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|