I'm currently trying to create a Sudoku Solver, and on the step of assigning some possible values to a box that is not already preoccupied. (Bit of background info for why I'm doing this shebang: Sudoku is a number game based on a 9x9 grid, its contextual rules allow certain boxes in the grid that are not preoccupied to hold possible values during the process of solving )
To do this I created a structure, defined it as two dimensional, and populated it with a predefined list of integers using a for-loop.
Now when I tried to remove one integer from the list of a particular item in the two dimensional structure, I found out that all the lists of the items in the structure have had that integer removed. There's probably a simple solution to this, but I've been really struggling to find it. Hope the code below clarifies the somewhat confusing verbal explanation.
Structure Element
Dim PossibleValues As List(Of Integer)
Dim ElementValue As Integer
End Structure
Sub Main()
Dim List as New List(Of Integer)({1,2,3})
Dim TDP(8,8) as Element
For x as integer = 0 to 8
For y as integer = 0 to 8
TDP(x,y).PossibleValues = List
Next
Next
TDP(0,0).PossibleValues.Remove(1)
End Sub
Now I expect only TDP(0,0) would have a list of "2,3" when print out its list of integers, but when I check other items , i.e. TDP(1,0), its list is of integer is also "2,3"
Look at the assignment here:
TDP(x,y).PossibleValues = List
List(Of T) is a reference type, so this assigns a reference to the same List object to each of the array elements.
If you want each item to have it's own list of possible items, you need to either deep copy the list or create a new list:
Sub Main()
Dim TDP(8,8) as Element
For x as integer = 0 to 8
For y as integer = 0 to 8
TDP(x,y).PossibleValues = New List(Of Integer)({1,2,3})
Next
Next
TDP(0,0).PossibleValues.Remove(1)
End Sub
I have list with duplicates values
Dim lstPlayerValidate As New List(Of Integer)
Dim duplicates = lstPlayerValidate.Where(Function(x) lstPlayerValidate.Where(Function(y) x = y).Count() > 1).Distinct().ToList
Now I want check if comboxes contains any value from this list.
I do it so:
If cmbPlayer.Items.Contains(duplicates) Then 'do something
My combobox has Item as List(Of Player), where PlayerID is value which I want compare. Perhaps I'm doing an unnecessary job creating another list with only id values. But I don't now how I can do it
I have some hard-coded values for Switch case (and one case have 4 to 5 elements to be added in a list object) and the case statement is 450 + times.
I don't want to implement this by using switch case(actually increasing the length of code ) .
Here is sample code :
Case Set = "A"
obj.Add("1")
obj.Add("2")
obj.Add("3")
obj.Add("4")
obj.Add("5")
Case Set = "B"
obj.Add("66")
obj.Add("77")
Case Set = "C"
obj.Add("888")
obj.Add("999")
obj.Add("111")
Every case statement is different and the elements inside the case is different
My product in Windows product and using vb.net .
Any other alternate way to achieve this .
Suggestions are most welcome!!!!!
You can create a Dictionary(of String, List(of String)). The Key of the dictionary is "A", "B", "C" and the List contains the element you want to add.
Dim dic As New Dictionary(of String, List(of String))
dic.add("A", new List(of String)({"1", "2"}))
dic.add("B", new List(of String)({"66", "77"}))
later you can just do this:
Dim key As String = "A"
obj.addRange(dic(key))
If you want to remove the static dictionary creation, you can easily build up the dictionary based on a file or database.
I need help figuring out how to go about programming this problem. I have an unknown number of pairs. Each pair is a Length x Width. I want to create sets of every possible combination of either a Length or Width from each pair. Here is an example of what I'm trying to do:
If I input 3 pairs, (1x2) (3x4) (5x6) I would get the following sets:
(1,3,5) (1,3,6) (1,4,5) (1,4,6) (2,3,5) (2,3,6) (2,4,5) (2,4,6)
So if I had 4 pairs, it would create a total of 16 sets, etc. I need to be able to input each pair and after all pairs have been entered, I need it to print out the sets. It can never include both numbers from a given pair in the same set. How do I create a loop or is there a built in math function that could produce all possible sets from a given number of pair inputs? I hope I described the problem well enough but if not, please ask questions. Thanks
This is called Cartesian product.
For example, if we have two sets A and B, such that
A = {1,2}
B = {3,4}
Then the result of the Cartesian product A x B is equal to
A x B = {(1,3),(1,4),(2,3),(2,4)}
If now we want to make the Cartesian product between the result obtained above and a new set, for example:
N = {5,6}
The result of the Cartesian product A x B x N, is equal to
A x B = {(1,3),(1,4),(2,3),(2,4)}
N = {5,6}
──────────────────────────────────────────────────
A x B x N = {(1,3,5),(1,3,6),(1,4,5),(1,4,6),(2,3,5),(2,3,6),(2,4,5),(2,4,6)}
Each element of the first set must be paired with each element of the second set.
I have developed 4 solutions to the Cartesian product:
Using a mathematical model, but without recursion. This solution
vectors using calculating for each combination number.
Using recursion, with the Collections class.
using the List (Of ...) class, also with recursion.
These three solutions seemed to me difficult to explain to you.
Furthermore, it is very difficult for me to explain my thoughts in English, because my native language is Castilian.
So I made the effort to create another solution that does not use recursion, which was more simple and friendly for any programmer.
Finally, I could create a satisfactory solution. Easy to understand and without recursion.
It is also very versatile. Any number of sets is accepted, as required, from 2 onwards.
You can also use any number of items. This depends on the requirements of each developer.
I hope this 4th. solution I devised, will please you, esteemed colleagues.
Only need a ListBox1 within Form4. Here it is:
Public Class Form4
Private Sub Form4_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'┌─────────── Temporary code for example ───────────┐
Dim Set_1 As List(Of String) = New List(Of String)
Dim Set_2 As List(Of String) = New List(Of String)
Dim Set_3 As List(Of String) = New List(Of String)
Set_1.Add("1")
Set_1.Add("2")
Set_2.Add("3")
Set_2.Add("4")
Set_3.Add("5")
Set_3.Add("6")
'└──────────────────────────────────────────────────┘
Dim Sets As List(Of Object) = New List(Of Object)
Sets.Add(Set_1)
Sets.Add(Set_2)
Sets.Add(Set_3)
Dim product As List(Of String) = Sets(0)
For i = 1 To Sets.Count - 1
product = CartesianProduct(product, Sets(i))
Next
For Each element As String In product
Me.ListBox1.Items.Add(element)
Next
End Sub
Private Function CartesianProduct(ByVal Set_A As List(Of String), ByVal Set_B As List(Of String)) As List(Of String)
Dim product As List(Of String) = New List(Of String)
For Each a As String In Set_A
For Each b As String In Set_B
product.Add(a & b)
Next
Next
Return product
End Function
End Class
Have a nice day! :)
I want to store a two value combination but cannot find the right class. I know I could do it with a two dimentional array but is there another way since I always have to do the redimentionalisation. I know about the dictionary but there is the first value always the key.
Example:
I have a dgv and want to collect the rownumbers with duplicates. I iterate throu the dgv and collect 1 and 4, 20 and 33 and so on and store this combinations to use them somewhere else.
You can use collection of Tuple. For example :
Dim tuples As New List(Of Tuple(Of Integer, Integer))
tuples.Add(Tuple.Create(1, 4))
tuples.Add(Tuple.Create(20, 33))
One way would be with a Dictionary(Of DataGridViewRow, List(Of Integer)). The list would contain the rows holding the values in the key. This will work if you get more than 2 rows trhat are duplicates.