Removing elements from list with condition (VB) - vb.net

I have a list
Dim list As New List(Of Double)
I want to remove the last entries, if the differences are > 20.
My idea to check the last 30 entries:
Do While index >= list.Count - 30
If Math.Sqrt((list(index) - list(index + 1)) ^ 2) > 20 Then
list.RemoveAt(index)
Exit Do
End If
Loop
It does not lead to my solution. Can somebody help? Thank you very much.

Instead of using a List of doubles I would use a LinkedList of doubles to make use of the class methods and properties like AddFirst, Last, Last.Previous
So let's assume that you have a LinkedList declared like
Dim list As New LinkedList(Of Double)
And you have added elements to this list using
list.AddFirst(134.5678)
Now, you could remove from the end of the list with something like this
' You want to have a list of at least 30 elements
Do While list.Count > 30
' Last node and the previous one
Dim dLast = list.Last
Dim dLastPrev = list.Last.Previous
' Evaluate the two elements
If Math.Sqrt(dLastPrev.Value - dLast.Value) ^ 2 > 20 Then
' Remove the last and continue to evaluate the next pair
list.RemoveLast()
Else
' Stop if the condition is not met.
Exit Do
End If
Loop
Of course this could also be done using your current list type.
Also notice how I have swapped the two elements to verify. This is done to avoid any possibility of an IndexOutOfRangeException
Dim index as Integer = list.Count - 1
Do While index >= list.Count - 30
Dim prev = index - 1
If Math.Sqrt((list(prev) - list(index)) ^ 2) > 20 Then
list.RemoveAt(index)
else
Exit Do
End If
index = index - 1
Loop

Related

Random numbers in array without any duplicates

I'm trying to randomize an array from numbers 0 to 51 using loops but I just cannot seem to pull it off. My idea was that
Generate a Random Number
Check if this random number has been used by storing the previous in an array
If this random number has been used, generate new random number until it is not a duplicate
If it's not a duplicate, store it
My attempt:
Dim list(51) As Integer
Dim templist(51) As Integer
For i As Integer = 0 To 51 Step 1
list(i) = i
Next i
Do While counter <= 51
p = rand.Next(0, 52)
templist(counter) = p
For n As Integer = 0 To 51 Step 1
p = rand.Next(0, 52)
If templist(n) = p Then
Do While templist(n) = p
p = rand.Next(0, 52)
Loop
templist(n) = p
Else
templist(n) = p
End If
Next
counter += 1
Loop
For n As Integer = 0 To 51 Step 1
ListBox1.Items.Add(templist(n))
Next
It will be a lot easier if you just have a list of all of the possible numbers (0 to 51 in your case), then remove the number from the list so it can't be picked again. Try something like this:
Dim allNumbers As New List (Of Integer)
Dim randomNumbers As New List (Of Integer)
Dim rand as New Random
' Fill the list of all numbers
For i As Integer = 0 To 51 Step 1
allNumbers.Add(i)
Next i
' Grab a random entry from the list of all numbers
For i As Integer = 0 To 51 Step 1
Dim selectedIndex as Integer = rand.Next(0, (allNumbers.Count - 1) )
Dim selectedNumber as Integer = allNumbers(selectedIndex)
randomNumbers.Add(selectedNumber)
allNumbers.Remove(selectedNumber)
' Might as well just add the number to ListBox1 here, too
ListBox1.Items.Add(selectedNumber)
Next i
If your goal is to get the numbers into ListBox1, then you don't even need the "randomNumbers" list.
EDIT:
If you must have an array, try something like this:
Function RandomArray(min As Integer, max As Integer) As Integer()
If min >= max Then
Throw New Exception("Min. must be less than Max.)")
End If
Dim count As Integer = (max - min)
Dim randomNumbers(count) As Integer
Dim rand As New Random()
' Since an array of integers sets every number to zero, and zero is possibly within our min/max range (0-51 here),
' we have to initialize every number in the array to something that is outside our min/max range.
If min <= 0 AndAlso max >= 0 Then
For i As Integer = 0 To count
randomNumbers(i) = (min - 1) ' Could also be max + 1
Next i
End If
Dim counter As Integer = 0
' Loop until the array has count # of elements (so counter will be equal to count + 1, since it is incremented AFTER we place a number in the array)
Do Until counter = count + 1
Dim someNumber As Integer = rand.Next(min, max + 1)
' Only add the number if it is not already in the array
If Not randomNumbers.Contains(someNumber) Then
randomNumbers(counter) = someNumber
counter += 1
End If
Loop
Return randomNumbers
End Function
This is good enough for your assignment, but the computer scientist in my hates this algorithm.
Here's why this algorithm is much less desirable. If zero is in your range of numbers, you will have to loop through the array at least 2N times (so 104+ times if you are going from 0 to 51). This is a best case scenario; the time complexity of this algorithm actually gets worse as the range of numbers scales higher. If you try running it from 0 to 100,000 for example, it will fill the first few thousand numbers very quickly, but as it goes on, it will take longer and longer to find a number that isn't already in the list. By the time you get to the last few numbers, you could potentially have randomly generated a few trillion different numbers before you find those last few numbers. If you assume an average complexity of 100000! (100,000 factorial), then the loop is going to execute almost ten to the half-a-millionth power times.
An array is more difficult to "shuffle" because it is a fixed size, so you can't really add and remove items like you can with a list or collection. What you CAN do, though, is fill the array with your numbers in order, then go through a random number of iterations where you randomly swap the positions of two numbers.
Do While counter <= 51
p = rand.Next(0, 52)
While Array.IndexOf(list, p) = -1
p = rand.Next(0, 52)
End While
counter += 1
Loop
Haven't written VB in about 5 years, but try this out:
Function GetRandomUniqueNumbersList(ByVal fromNumber As Integer, ByVal toNumber As Integer) As List(Of Integer)
If (toNumber <= fromNumber) Then
Throw New ArgumentException("toNumber must be greater than fromNumber", toNumber)
End If
Dim random As New Random
Dim randomNumbers As New HashSet(Of Integer)()
Do
randomNumbers.Add(random.Next(fromNumber, toNumber))
Loop While (randomNumbers.Count < toNumber - fromNumber)
Return randomNumbers.ToList()
End Function
Ok, that was painful. Please someone correct it if I made any mistakes. Should be very quick because it's using a HashSet.
First response to forum on stackoverflow - be gentle.
I was looking for a way to do this but couldn't find a suitable example online.
I've had a go myself and eventually got this to work:
Sub addUnique(ByRef tempList, ByVal n, ByRef s)
Dim rand = CInt(Rnd() * 15) + 1
For j = 0 To n
If tempList(j) = rand Then
s = True
End If
Next
If s = False Then
tempList(n) = rand
Else
s = False
addUnique(tempList, n, s)
End If
End Sub
Then call the sub using:
Dim values(15) As Byte
Dim valueSeen As Boolean = False
For i = 0 To 15
addUnique(values, i, valueSeen)
Next
This will randomly add the numbers 1 to 16 into an array. Each time a value is added, the previous values in the array are checked and if any of them are the same as the randomly generated value, s is set to true. If a value is not found (s=false), then the randomly generated value is added. The sub is recursively called again if s is still true at the end of the 'For' loop. Probably need 'Randomize()' in there somewhere.
Apologies if layout is a bit wobbly.

Ordering Outputted random numbers

I am making a program that outputs random numbers and then organizes them.
I am organizing the numbers so later I can add code to tell the user how many matching numbers he or she has received.
The program compiles fine, but then when I run the exe, after the first line of random numbers is outputted it crashes. The error I receive is:
the index is outside the boundaries of the array.
Any help at all would be gratefully appreciated.
Option Explicit On
Option Strict On
Imports System
Module Yahtzed
Sub Main()
Randomize()
Dim Index, Values, NumberOfPlayers,Temp as Integer
Dim order(index) as integer
Dim Last As Integer = 0 'to Order.Length-2
Console.Write("How many people will be playing Yahtzed?: ")
NumberOfPlayers = convert.toint32(Console.Readline)
Do while NumberOfPlayers > 0
Index = 0
Do until index = 5
Values = CInt(Int((6 * Rnd()) + 1))
Console.Write(" "&values)
Index += 1
Loop
Do Until Index = 0
If Order(Index + 1) < Order(index)
Temp = Order(Index + 1)
Order(Index + 1) = order(index)
Order(index) = Temp
Console.WriteLine(Order(Index))
End if
index -= 1
loop
Console.Writeline
NumberOfPlayers -= 1
Console.Writeline()
Loop
End Sub
End Module
The code doesn't really make sense as it is now. You create some random numbers, then you throw them away, and sort an array that never has been assigned anything. Also, the array only has one item, so it would not be able to hold the random values.
I think that you want to declare the array for five items, not one (as index is zero by the time you create the array):
Dim order(4) As Integer
Then put the random numbers in the array instead of putting them in a variable where each random number will replace the previous one:
Index = 0
Do until index = 5
order(index) = CInt(Int((6 * Rnd()) + 1))
Console.Write(" " & order(index))
Loop
When you sort the array, you start looking at index 6 (as the variable index is 5), which is outside the array. You would want to start at one item from the last in the array (i.e. at index 3). Then you loop until index is -1, otherwise you won't be comparing the two first items in the array.
Also, you have to continue sorting until there are no more swaps, just going through the array once doesn't make it sorted:
Dim swapped as Boolean = True
Do While swapped
index = 3
swapped = False
Do Until index = -1
If order(index + 1) < order(index)
temp = order(index + 1)
order(index + 1) = order(index)
order(index) = temp
swapped = True
End if
index -= 1
Loop
Loop
This sorting algorithm is called Bubble Sort.
There is also sorting built into the framework, if you want to use that instead:
Array.Sort(order)
If you would write out the values while sorting, you would get them several times over, so you do that after they are sorted:
index = 0
Do until index = 5
Console.Write(" " & order(index))
Loop

Return the previous index in vb.net?

I have a For Each loop that is looping through an array of strings to find the first string with a length of three characters. For example, when looping through the array, if the first 3 character string is the 4th index of the array, I would like for it to return the 3rd (previous) index of the array. Any help would be appreciated.
For Each piece As String In p
If piece.Length = 3 Then
'Return previous index
End If
Next
You have a couple of options.
If p is a collection accessible by index (ie: IList(Of T)), you can switch to a For loop, and return the element at the current index -1.
Otherwise, you can keep the previous item in a separate variable, and return it when your condition is met.
Dim lastItem as String
For Each piece As String In p
If piece.Length = 3 Then
Return lastItem ' Will return Nothing if this is the first item...
End If
lastItem = piece ' Store this for next time
Next
How about something like this:
For i As Integer = 1 To p.Count - 1
If p(i).Length = 3 Then
'Return previous index
Return p(i - 1)
End If
Next
Note the loop will have to start at 1 (not zero) otherwise you could return the (0-1)th element of the array which won't exist
I would use .FindIndex, if you have a List(Of T):
Dim i As Integer = p.FindIndex(Function(x) x.Length = 3)
If i > 0 Then
Dim previous As String = p(i - 1)
'Do something
End If
If you have a String(), you can also use the Array version:
Array.FindIndex(Of String)(p, Function(x) x.Length = 3)

Multi Select List Box

I have a list box on a form and it works fine for what I want to do.
I am wanting to edit items on the form, this means populating the listbox and then selecting the relevant items.
My listbox contains a list of item sizes, i want to select the sizes which belong to the item being edited.
PLease can someone give me some pointers.
I tried me.lstItemSizes.SetSelected(i,true) but this only works for a single item.
Any help wil be much appreciated.
My Code:
Private Sub SelectItemSizes(ByVal itemID As Integer)
Dim itemSizes As IList(Of ItemSize) = _sizeLogic.GetItemSizes(itemID)
Me.lstItemSizes.SelectionMode = SelectionMode.MultiExtended
If (itemSizes.Count > 0) Then
For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1
For x As Integer = 0 To itemSizes.Count - 1
If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
Me.lstItemSizes.SetSelected(i, True)
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Next
Next
End If
End Sub
Did you set the selectionmode to multi?
You need to specify that in order to allow multiple selections.
Then you can do:
Dim i as Integer=0
For i=0 To Me.listBox.SelectedItems.Count -1
'display the listbox value
next i
Here is a screen shot:
After you set the property on the listbox then call setselected based on the values you want selected.
me.lstItemSizes.SetSelected(3,true)
me.lstItemSizes.SetSelected(4,true)
me.lstItemSizes.SetSelected(9,true)
Here you can add 20 numbers and only select the even.
Dim i As Integer
'load the list with 20 numbers
For i = 0 To 20
Me.ListBox1.Items.Add(i)
Next
'now use setselected
'assume only even are selected
For i = 0 To 20
If i Mod 2 = 0 Then
Me.ListBox1.SetSelected(i, True)
End If
Next
3rd edit
Look at the way you are looping, lets assume I create a list of integers, my vb.net is rusty I mainly develop in C#. But assume you did this:
Dim l As New List(Of Integer)
l.Add(2)
l.Add(6)
l.Add(20)
You only have three items in your list, so first loop based on the items on your list, then within the items in your listbox, you have it vice versa. Look at this:
Dim i As Integer
Dim l As New List(Of Integer)
l.Add(2)
l.Add(6)
l.Add(20)
'load the list with 20 numbers
For i = 0 To 20
Me.ListBox1.Items.Add(i)
Next
Dim lCount As Integer = 0
For lCount = 0 To l.Count - 1
For i = 0 To 20
If i = l.Item(lCount) Then
Me.ListBox1.SetSelected(i, True)
Exit For
End If
Next
Next
In the code my l is a list of just 3 items: 2, 6, and 20.
I add these items to l which is just a list object.
So now I have to loop using these 3 numbers and compare with my listbox. You have it the opposite you are looping on your listbox and then taking into account the list object.
Notice in my for loop that once the item in my list is found I no longer need to loop so I exit for. This ensures I dont overdue the amount of looping required. Once the item is found get out and go back to the count of your list object count.
After running my code here is the result
You have to change the ListBox.SelectionMode property in order to enable multiple-selection.
The possible values are given by the SelectionMode enum, as follows:
None: No items can be selected
One: Only one item can be selected
MultiSimple: Multiple items can be selected
MultiExtended: Multiple items can be selected, and the user can use the Shift, Ctrl, and arrow keys to make selections
So, you simply need to add the following line to the code you already have:
' Change the selection mode (you could also use MultiExtended here)
lstItemSizes.SelectionMode = SelectionMode.MultiSimple;
' Select any items of your choice
lstItemSizes.SetSelected(1, True)
lstItemSizes.SetSelected(3, True)
lstItemSizes.SetSelected(8, True)
Alternatively, you can set the SelectionMode property at design time, instead of doing it with code.
According to MSDN, SetSelected() can be used to select multiple items. Simply repeat the call for each item that needs to be selected. This is the example they use:
' Select three items from the ListBox.
listBox1.SetSelected(1, True)
listBox1.SetSelected(3, True)
listBox1.SetSelected(5, True)
For reference, this is the MSDN article.
Because my code had the following loops:
For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1
For x As Integer = 0 To itemSizes.Count - 1
If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
Me.lstItemSizes.SetSelected(i, True)
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Next
Next
The first loop loops through the available sizes and the second loop is used to compare the item sizes.
Having the following code:
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Meant that even if item i became selected, it could also be deselected.
SOLUTION:
Remove Me.lstItemSizes.SetSelected(i, False) OR Include Exit For

Not able to add values in second Combobox

Here is my code.
for example TextBox1.Text= 12,34,45,67,67
Dim process_string As String() = TextBox1.Text.Split(New Char() {","})
Dim process As Integer
For Each process In process_string
Combo1.Items.Add(process)
count = count + 1
Next process
total_process.Text = count
End If
Dim array(count) As String
Dim a As Integer
For a = 0 To count - 1
array(a) = Combo1.Items(a).ToString
Next
a = 0
For a = count To 0
Combo2.Items.Add(array(a).ToString)
Next
i want to add values in reversed order in combobox2 that are available in combobox1
but when i run the application the second combobox remains empty and not showing any value.
You've specified this for loop
For a = count To 0
But you need to add STEP -1 to go backwards like that.
For a = count To 0 Step -1
2 things. First of all, K.I.S.S. Keep it simple stupid
For i As Integer = ComboBox1.Items.Count - 1 To 0 Step -1
ComboBox2.Items.Add(ComboBox1.Items(i))
Next
second: It didn't work because you forgot the Step -1 on your last loop
~~~~~~~~~~~~~~Edit~~~~~~~~~~~~~~
Sorting the data in a combo box should be done with the sorted property on a combo box
ComboBox3.Sorted = True
Sorting the data in reverse order should be done with arrays as you were trying to do before. The following code should suffice:
Dim List As ArrayList = ArrayList.Adapter(ComboBox3.Items)
List.Sort()
List.Reverse()
ComboBox4.Items.AddRange(List.ToArray)
If you wanted to get creative, you could potentially create your own combo box class and make your own version of the sorted property that allows for "sort alpha", "sort numeric", "sort alpha Desc", and "sort numeric desc" and perhaps some other options. But I'd only do that if you were going to use this in a lot of places.