This question is about this topic:
Vb.net all combinations
Question:
I use this code for my application but i've got a problem.
The chance exists that i have much items which has to be combinated.
But i just want to show the first 10 combinations/results.
What i want is the text to be completely unique.
so the example at my topic you see in the beginning of this question there's an ape cow deer ... example.
It doesn't matter here.
but if i got something like this:
1|2|3|4|5
6|7|8|9
3|2|1
0|9|8|7|6|5
( sometimes it's even bigger )
the first 10 results are:
1-6-3-0
1-6-3-9
1-6-3-8
1-6-3-7
1-6-3-6
1-6-3-5
1-6-2-0
1-6-2-9
1-6-2-8
1-6-2-7
but they are almost the same.
i want the first 10 results to be something like this then:
1-8-1-6
3-6-1-5
4-8-3-0
etc...
Is this possible??
Here is my solution that I converted from c# using http://www.developerfusion.com/tools/convert/csharp-to-vb/:
Dim numbers = New Integer()() { _
New Integer() {1, 2, 3, 4, 5}, _
New Integer() {6, 7, 8, 9}, _
New Integer() {3, 2, 1}, _
New Integer() {0, 9, 8, 7, 6, 5} _
}
Dim random = New Random()
Dim codes = New HashSet(Of String)()
Dim newCode As String
For resultNr As Integer = 0 To 9
' Try to generate random codes until a non exisiting one is found.
Do
Dim sb = New StringBuilder()
For i As Integer = 0 To 3
Dim r As Integer = random.[Next](numbers(i).Length)
sb.Append(numbers(i)(r)).Append("-")
Next
sb.Length -= 1
newCode = sb.ToString()
Loop While codes.Contains(newCode)
codes.Add(newCode)
Console.WriteLine(newCode)
Next
Console.ReadKey()
Related
I am creating a program where the code needs to get a value from each character in a string (that can vary in length). Such as a name, an example, Tony, where T = 1, O = 4, N = 7, Y = 3. This has to add to a total. I have tried loops but only getting 1 as an output, even if there isn't a character that has a value in the string.
Dim FNTL As Integer = Nothing
Dim FN = FN_TBX.Text
For Each A, J, S As Char In FN_TBX.Text
FNTL = +8
Next
Dim FNTLstr
FNTLstr = Convert.ToString(FNTL)
LN_TBX.Text = FNTL
FN_TBX is the textbox input
FN is the textbox input
FNTL is the total
FNstr is the total in string format
Some characters have the same value such as above, A, J, S are = to 8. The integer conversion is to display the total to a text box.
The logic is not entirely clear. Maybe you should have a Dictionary(Of Char, Int32) to get a value for each char and a fallback-value if it's not defined.
Here is an example with a dictionary which is declared as class field:
Dim _charValue As New Dictionary(Of Char, Int32) From
{
{"t"c, 1}, {"T"c, 1},
{"o"c, 4}, {"O"c, 4},
{"n"c, 7}, {"N"c, 7},
{"y"c, 3}, {"Y"c, 3},
{"a"c, 8}, {"A"c, 8},
{"j"c, 8}, {"J"c, 8},
{"s"c, 8}, {"S"c, 8}
}
Now you can calculate the sum in your method, for example with a simple loop:
Dim sum as Int32 = 0
For Each c As Char In FN_TBX.Text
Dim charValue = 0
If Not _charValue.TryGetValue(c, charValue)
charValue = 1 ' fallback value
End If
sum += charValue
Next
LN_TBX.Text = sum.ToString()
i want to campare 2 arraylist and show the values ..
arraylist1 = {1,2,3,4,5,6,7,8,9}
arraylist2 = {2,4,5}
i have compare and the value to listview .. like this
1 Not available
2 available
3 Not available
4 available
5 available
6 Not available
i write the program like this but looping many times ..
For position As Integer = 0 To arraylist1.Count - 1
Dim words As String() = arraylist2(position).Split(New Char() {" "c})
arr(1) = words(3)
For i = 0 To arr.Length - 1
If arraylist1(i).Contains(arr(1)) Then
arr(0) = i
arr(2) = "working"
itm = New ListViewItem(arr)
lv1.Items.Add(itm)
Else
arr1(0) = i
arr1(1) = arrproc(i)
arr1(2) = "NOT working"
itm = New ListViewItem(arr1)
lv1.Items.Add(itm)
End If
Next
Next
as Tim was suggesting use List(Of Integer)
kind of like this
Dim list1 As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim list2 As New List(Of Integer) From {2, 4, 5}
For Each i As Integer In list1
If list2.Contains(i) Then
Console.WriteLine(i & " available")
Else
Console.WriteLine(i & " Not available")
End If
Next
also in your code within the first loop, you use the index of arraylist1 in arraylist2 to get the words this will fail if both lists don't have the same amount of items and will run into an out of bounds exception
EDIT
So there are a few more problems to solve i guess
you loop over the arraylist1 to retrieve items from arraylist2
you use the forth word of that items and assign it to a random array (arr())
then you loop over that random array and compare the second item in
that array with all item from the arraylist1 at the indices of that
random array
so my suggestion is to review your code again
this might work but still some questions to answer
For Each item_list1 As String In arraylist2
Dim words As String() = item_list1.Split(New Char() {" "c})
If arraylist1.contains(words(3)) Then
itm = New ListViewItem(arraylist1.IndexOf(words(3)))
itm.SubItems.Add(words(3))
itm.SubItems.Add("working")
Else
itm = New ListViewItem(arraylist1.IndexOf(words(3)))
itm.SubItems.Add(arrproc(arraylist1.IndexOf(words(3))))
itm.SubItems.Add("NOT working")
End If
lv1.items.add(itm)
Next
You can use such linq query to compare those lists and return the appearance status of items of first list in seccond list:
Dim list1 = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim list2 = {2, 4, 5}
Dim data = list1.Select(Function(item)
Return New With
{
.Value = item,
.Status = String.Format("{0} {1}", item, IIf(list2.Contains(item), "Available", "Not Available"))
}
End Function).ToList()
Then you can simply add them to ListView this way:
For Each item In data
Me.ListView1.Items.Add(item.Value.ToString()).SubItems.Add(item.Status)
Next
Full example
Sub Main()
Dim list1 As New List(Of Integer)() From {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim list2 As New List(Of Integer)() From {2, 4, 5}
Dim rows = From i In list1
Group Join j In list2
On j Equals i Into g = Group
From j In g.DefaultIfEmpty()
Select i, j
For Each r In rows
Console.WriteLine("{0} {1}", r.i, If(r.i = r.j, "exist", "not exist"))
Next
Console.ReadLine()
End Sub
For instance my array is {4,6,9,3,1}
And I want to cycle it X amount of times, for example 3 times,
I want my array to become {3,1,4,6,9}
Is there an easy way to do this in VB.NET?
Thank you
Function CycleArray(ByVal Arr() As Integer)
Dim Tmp As Integer = Arr(0)
Dim Arr2 = Arr.Skip(1).ToArray()
Arr2(UBound(Arr2) + 1) = Tmp
Return Arr2
End Function
You will find that using List is far easier than using Arrays in most cases. To try this call the function like this:
Dim MyArray() As Integer = {4, 6, 9, 3, 1}
MyArray = LeftRotateArray(MyArray, 3)
the function looks like this:
Private Function LeftRotateArray(theArray() As Integer, Shift As Integer) As Integer()
Dim shiftVal As Integer = Shift Mod theArray.Length 'in case Shift is larger than array
Dim rv As List(Of Integer) = theArray.ToList
For ct As Integer = 1 To shiftVal
rv.Add(rv(0))
rv.RemoveAt(0)
Next
Return rv.ToArray
End Function
The method is straight forward. Copy the array to a list and then move elements from the front to the rear until you have completed the desired iterations. The return creates an array from the list.
or using this method from Jon Bentley's 'Programming Pearls':
Private Function LeftRotateArray(theArray() As Integer, Shift As Integer) As Integer()
Dim shiftVal As Integer = Shift Mod theArray.Length 'in case Shift is larger than array
Dim rv(theArray.GetUpperBound(0)) As Integer
Array.Copy(theArray, rv, theArray.Length)
Array.Reverse(rv, 0, shiftVal)
Array.Reverse(rv, shiftVal, rv.Length - shiftVal)
Array.Reverse(rv, 0, rv.Length)
Return rv
End Function
Since it's essentially cutting and pasting a part of the original array at a specific place it can be done in one action. As dbasnett said it's easier to work with a List or IEnumerable, if you're focusing on clarity of code. Performance wise other solutions might be better.
Dim numbers = New Integer() {4, 6, 9, 3, 1}
Dim result = RotateRight(numbers, 3)
Public Function RotateRight(numbers As Integer(), offset As Integer) As Integer()
Dim list = numbers.ToList()
Return list.Skip(offset).Concat(list.Take(offset)).ToArray()
End Function
I think you can simply do Array.Copy. You would need to do it in two parts...
Function CycleArray(Arr() As Integer, cycleTimes As Integer) As Integer()
Dim Arr2() As Integer
ReDim Arr2(Arr.Length - 1)
cycleTimes = cycleTimes Mod Arr.Length
Array.Copy(Arr, cycleTimes, Arr2, 0, Arr.Length - cycleTimes)
Array.Copy(Arr, 0, Arr2, Arr.Length - cycleTimes, cycleTimes)
Return Arr2
End Function
How do i implement a index based sort in vb.net
Could someone guide me. By giving me a head start
eg
array = 9 8 7 6 5 4 3 2 1
Index Number = 5
Counting from 9 and the 5th character is 5.
So the first sorted character is 5.
Remove 5 and remains 9 8 7 6 4 3 2 1
start value should be from 4(current 5th character) now 5th character is 9 since it's circular
unsorted array : 8 7 6 4 3 2 1
sorted array : 5 9
any hints
First off, I definitely wouldn't call this "sorting"; it's a bit more like deterministically (non-randomly) "shuffling" the array. But I'll take a stab at this…
Basically, start by using List(Of T) rather than arrays, because they allow you to easily and remove at any point in the list, rather than arrays which are a fixed size. Next, use a running counter to track your current position in the input list, and use the Mod operator to make the index effectively 'wrap' around the end of the list. Use a While loop to continue until all the items from the input have been processed. It should look something like this:
Dim input As New List(Of Integer) From { 9, 8, 7, 6, 5, 4, 3, 2, 1 }
Dim stepBy As Integer = 5
Dim index As Integer = 0
Dim output As New List(Of Integer)
While input.Count > 0
index = (index + stepBy - 1) Mod input.Count
output.Add(input(index))
input.RemoveAt(index)
End While
In this case the output is:
5, 9, 3, 6, 7, 4, 1, 8, 2
I thought this was an interesting question and I loved the answer by p.s.w.g....
...but I was curious to see if any of the built-in .Net collections would facilitate a fairly trivial circular list.
This is what I came up with using the LinkedList class:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim originalIntegers() As Integer = {9, 8, 7, 6, 5, 4, 3, 2, 1}
Dim output As New List(Of Integer)
Dim circleInts As New CircularIntegerList(originalIntegers)
circleInts.MoveForward(4) ' move forward 4 from the 1st to get the 5th slot
While circleInts.Count > 0
If circleInts.Current.HasValue Then
output.Add(circleInts.Current)
End If
circleInts.RemoveCurrent() ' next number in sequence becomes the current one
circleInts.MoveForward(4) ' move forward another 4, net jump is 5 from the deleted one
End While
Dim result As String = String.Join(",", output.Select(Function(x) x.ToString).ToArray)
Debug.Print(result)
End Sub
End Class
Public Class CircularIntegerList
Private values As New LinkedList(Of Integer)
Private _current As LinkedListNode(Of Integer)
Public Sub New(ByVal data() As Integer)
If data.Length > 0 Then
values = New LinkedList(Of Integer)(data)
_current = values.First
End If
End Sub
Public ReadOnly Property Current As Integer?
Get
If Not IsNothing(_current) Then
Return _current.Value
Else
Return Nothing
End If
End Get
End Property
Public ReadOnly Property Count As Integer
Get
Return values.Count
End Get
End Property
Public Sub RemoveCurrent()
If Not IsNothing(_current) Then
Dim tmp As LinkedListNode(Of Integer) = If(IsNothing(_current.Next), values.First, _current.Next)
values.Remove(_current)
If values.Count > 0 Then
_current = tmp
Else
_current = Nothing
End If
End If
End Sub
Public Sub MoveForward(Optional ByVal NumberOfJumps As Integer = 1)
If Not IsNothing(_current) Then
For i As Integer = 1 To NumberOfJumps
_current = If(IsNothing(_current.Next), values.First, _current.Next)
Next
End If
End Sub
End Class
I have the following code, the objective of it is to take an initial list and to take each element within the list and store it in an array of lists, with each list in the array, holding each element in its own list. For instance
The list 2, 2, 3, 3, 3, 3, 5, 5, 7, 9, 9. Would create five lists:
List 1: 2, 2
List 2: 3, 3, 3, 3,
List 3: 5, 5
list 4: 7
List 5: 9, 9
This is my current code:-
Dim cnt As Integer = 0
Dim lists(uniqueFactors.Count) As List(Of Integer)
Dim saver As Integer = factors.Item(0)
Console.WriteLine(saver)
For Each i In factors
lists(cnt).Add(i)
If saver <> i Then
cnt = cnt + 1
End If
saver = i
Next
Thanks all in advance!
You'd probably be better off using a Dictonary<TKey, TValue>.
Dim storage As New Dictionary(Of Integer, Integer)
' Number Occurrences
' | |
storage.Add(2, 2)
storage.Add(3, 4)
storage.Add(5, 2)
storage.Add(7, 1)
storage.Add(9, 2)
You can iterate the list like this:
For Each item As KeyValuePair(Of Integer, Integer) In storage
Dim number As Integer = item.Key
Dim occurrences As Integer = item.Value
Next
Get the number of occurrences for a given number like this:
Dim number As Integer = 9
Dim occurrences As Integer = storage(number) 'Return: 2
Change the number of occurrences:
storage.Item(number) = 4 'Set to 4
storage.Item(number) += 1 'Increase from 2 to 3
storage.Item(number) -= 1 'Decrease from 2 to 1
Create an enumerator, array and/or list of occurrences for a given number:
Dim iterator As IEnumerable(Of Integer) = Enumerable.Repeat(number, occurrences).ToList()
Dim array As Integer() = Enumerable.Repeat(number, occurrences).ToArray()
Dim list As List(Of Integer) = Enumerable.Repeat(number, occurrences).ToList()
You can also write an extension method:
Public Module Extensions
<System.Runtime.CompilerServices.Extension()>
Public Function ToList(Of TKey)(source As Dictionary(Of TKey, Integer), key As TKey) As List(Of TKey)
Return Enumerable.Repeat(key, source.Item(key)).ToList()
End Function
End Module
Now you create a list by simply writing:
Dim occurrences As List(Of Integer) = storage.ToList(number)
Why not use a List of Lists like this?
Dim last As Integer
Dim first As Boolean = True
Dim ints As List(Of Integer)
Dim lists As New List(Of List(Of Integer))
For Each i In factors
If first Then
first = False
ints = New List(Of Integer)
ints.Add(i)
lists.Add(ints)
last = i
ElseIf i = last Then
ints.Add(i)
Else
ints = New List(Of Integer)
ints.Add(i)
lists.Add(ints)
last = i
End If
Next