none
Compare two method count 3 digits

    Question

  • Hi All,

    I have two click_button.

    Button_2 not count correctly at "017".I suspect because of "string or integer" but I don't how to do.

    button_3 show correctly but I need some improve if can.

    Thank.

    Option Strict On
    Option Infer On
    Option Explicit On
    
    Imports System.IO
    Imports System.Data
    Imports System.Text
    
    Imports System.Windows.Forms
    
    Public Class Form1
    
        Private Data As New List(Of String)
        Private Data_A As New List(Of String)
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TxtFile:
            '8257,5321,0181,8279,8661,2120,6641,4000,0752,6975,9849,9106,7661,4522,4287,8613,3279,6296,4498,6003,8273,6642,5201
            '6235,5507,0126,9753,0354,0423,0328,6185,8449,3424,1481,1861,3234,5928,3503,1966,0513,2306,2315,3750,0190,1510,0063
            '1954,8691,7223,5869,2822,3609,3259,5081,4204,3116,5806,7052,2347,9977,5773,7416,4605,0541,0548,2138,6627,0172,0617
    
            Data.AddRange(IO.File.ReadAllLines("C:\Users\Uset\Documents\3Lines.txt"))
    
            While (Data(Data.Count - 1) = "")
                Data.RemoveAt(Data.Count - 1)
            End While
    
            For i As Integer = 0 To Data.Count - 1
                TextBox1.AppendText(Data(i) & vbCrLf)
            Next
    
        End Sub
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim sb As New StringBuilder
    
            For I As Integer = 0 To Data.Count - 1
    
                Dim StringToCheck As String = Data(I).ToString
                Dim StringsToCheck As String() = StringToCheck.Split(","c)
                For Each S As String In StringsToCheck
                    '0 1 2 3
                    '012
                    '013
                    '123
                    '023
                    Dim StringsToFind As String() = {S(0) & S(1) & S(2),
                                                     S(0) & S(1) & S(3),
                                                     S(1) & S(2) & S(3),
                                                     S(0) & S(2) & S(3)}
    
                    For J As Integer = 0 To StringsToFind.Count - 1
                        sb.Append(StringsToFind(J) & vbCrLf)
                        Data_A.Add(StringsToFind(J))
                    Next
                Next
            Next
            Me.TextBox2.AppendText(sb.ToString)
    
        End Sub
    
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim sb As New System.Text.StringBuilder
            Dim counts As New Dictionary(Of String, Integer)
            For Each entry As String In Data_A
                counts(entry) = 0
            Next
            For Each StringToCheck As String In Data
                Dim StringsToCheck As String() = StringToCheck.Split(","c)
    
                For Each S As String In StringsToCheck
                    Dim t As List(Of String) = New List(Of String)
                    t.Add(S.Substring(0, 3))
                    t.Add(S.Substring(1, 3))
                    t.Add(S.Remove(1, 1))
                    t.Add(S.Remove(2, 1))
                    For Each ts In t
                        For Each Find2 As String In Data_A
    
                            Dim searchChars As New List(Of Char)(Find2.ToCharArray)
                            For Each c As Char In ts
                                If searchChars.Contains(c) Then
                                    searchChars.Remove(c)
                                    If searchChars.Count = 0 Then Exit For
                                End If
                            Next
                            If searchChars.Count = 0 Then counts(Find2) += 1
                        Next
                    Next
                Next
            Next
    
            For Each key As String In (From k In counts.Keys Order By k)
                sb.AppendLine("Occurences (" & key & ")= " & counts(key).ToString)
            Next
            TextBox3.Text = sb.ToString
    
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    
            Dim sb_Count_A As New StringBuilder
    
            For Each S As String In Data_A
    
                Dim StringsToFind As String() = {S(0), S(1), S(2)}
                For j As Integer = 0 To StringsToFind.Count - 3
                    Dim Count As Integer = 0
                    For Each Check As String In Data_A
                        If StringsToFind(0) <> StringsToFind(1) AndAlso StringsToFind(1) <> StringsToFind(2) AndAlso StringsToFind(0) <> StringsToFind(2) Then
                            If Check.Contains(StringsToFind(j)) AndAlso Check.Contains(StringsToFind(j + 1)) AndAlso Check.Contains(StringsToFind(j + 2)) Then Count += 1
                        ElseIf Check = S Then
                            Count += 1
                        End If
                    Next
                    sb_Count_A.Append("Occurances (" & S & ", " & StringsToFind(j) & StringsToFind(j + 1) & StringsToFind(j + 2) & ")= " & Count.ToString & vbCrLf)
                Next
            Next
            Me.TextBox4.AppendText(sb_Count_A.ToString)
        End Sub
    End Class
    

    Sunday, February 10, 2019 6:38 AM

Answers

  • Good...If digit not having same then count is correct but if digit having same it not correctly count.At least I understand.

    Next, How to do count correctly if having same digit Example;

    "662 code count 6, expected is 3.if i wrong , what is true.

    Hi

    It is very difficult to determine what you are actually wanting.

    Here is another variation which shows the desired 3 count for the value 662 - HOWEVER, check 266 and 626 as well - they are each 3 count - and they are the same values differently ordered.

    I have also altered the code significantly, but only to be an easier stand alone example. (Doesn't use the file now)

    The main change that effect the results are highlighted in the code below.

    Image

    Code

    Option Strict On
    Option Explicit On
    
    Public Class Form1
      Private Data_A As New List(Of String)
      Private StringToCheck As String
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        StringToCheck = String.Join(","c, "8257,5321,0181,8279,8661,2120,6641,4000,0752,6975,9849,9106,7661,4522,4287,8613,3279,6296,4498,6003,8273,6642,5201", "6235,5507,0126,9753,0354,0423,0328,6185,8449,3424,1481,1861,3234,5928,3503,1966,0513,2306,2315,3750,0190,1510,0063", "1954,8691,7223,5869,2822,3609,3259,5081,4204,3116,5806,7052,2347,9977,5773,7416,4605,0541,0548,2138,6627,0172,0617")
        Button1.PerformClick()
        Button2.PerformClick()
      End Sub
      Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim StringsToCheck As String() = StringToCheck.Split(","c)
        For Each S As String In StringsToCheck
          Dim StringsToFind As String() = {
              S(0) & S(1) & S(2), S(0) & S(2) & S(1),
              S(0) & S(1) & S(3), S(0) & S(3) & S(1),
              S(0) & S(2) & S(3), S(0) & S(3) & S(2),
              S(1) & S(0) & S(2), S(1) & S(2) & S(0),
              S(1) & S(0) & S(3), S(1) & S(3) & S(0),
              S(1) & S(2) & S(3), S(1) & S(3) & S(2),
              S(2) & S(0) & S(1), S(2) & S(1) & S(0),
              S(2) & S(0) & S(3), S(2) & S(3) & S(0),
              S(2) & S(1) & S(3), S(2) & S(3) & S(1),
              S(3) & S(0) & S(1), S(3) & S(1) & S(0),
              S(3) & S(0) & S(2), S(3) & S(2) & S(0),
              S(3) & S(1) & S(2), S(3) & S(2) & S(1)
            }
          Dim done As New List(Of String)
          For J As Integer = 0 To StringsToFind.Count - 1
            If Not done.Contains(StringsToFind(J)) Then
              Data_A.Add(StringsToFind(J))
              done.Add(StringsToFind(J))
            End If
          Next
        Next
        Data_A.Sort()
        ListBox1.DataSource = Data_A
      End Sub
      Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
    
        Dim counts As New Dictionary(Of String, Integer)
        For Each entry As String In Data_A
          counts(entry) = 0
        Next
    
        For I As Integer = 0 To Data_A.Count - 1
          Dim st As String = Data_A(I)
          Dim matchQuery As IEnumerable(Of String) = From item In Data_A Where item = st Select item
          counts(st) += 1
        Next
        For Each key As String In (From k In counts.Keys Order By k)
          ListBox2.Items.Add("Occurences (" & key & ")= " & counts(key).ToString)
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland




    • Edited by leshay Friday, February 15, 2019 2:43 AM
    • Marked as answer by mipakteh Saturday, February 16, 2019 4:51 AM
    Thursday, February 14, 2019 9:59 PM
  • Thank you very..very much Leshay.I understand .It enough already for your example.

    Hi

    You are welcome.

    BTW: here is a compact version. Only 2 ListBoxes.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Gr As New Dictionary(Of String, Integer)
      Private StringsToCheck As String()
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        StringsToCheck = String.Join(","c, "8257,5321,0181,8279,8661,2120,6641,4000,0752,6975,9849,9106,7661,4522,4287,8613,3279,6296,4498,6003,8273,6642,5201", "6235,5507,0126,9753,0354,0423,0328,6185,8449,3424,1481,1861,3234,5928,3503,1966,0513,2306,2315,3750,0190,1510,0063", "1954,8691,7223,5869,2822,3609,3259,5081,4204,3116,5806,7052,2347,9977,5773,7416,4605,0541,0548,2138,6627,0172,0617").Split(","c)
        For Each S As String In StringsToCheck
          Dim StringsToFind As IEnumerable(Of String) = {
              S(0) & S(1) & S(2), S(0) & S(2) & S(1),
              S(0) & S(1) & S(3), S(0) & S(3) & S(1),
              S(0) & S(2) & S(3), S(0) & S(3) & S(2),
              S(1) & S(0) & S(2), S(1) & S(2) & S(0),
              S(1) & S(0) & S(3), S(1) & S(3) & S(0),
              S(1) & S(2) & S(3), S(1) & S(3) & S(2),
              S(2) & S(0) & S(1), S(2) & S(1) & S(0),
              S(2) & S(0) & S(3), S(2) & S(3) & S(0),
              S(2) & S(1) & S(3), S(2) & S(3) & S(1),
              S(3) & S(0) & S(1), S(3) & S(1) & S(0),
              S(3) & S(0) & S(2), S(3) & S(2) & S(0),
              S(3) & S(1) & S(2), S(3) & S(2) & S(1)
            }.Distinct
          For J As Integer = 0 To StringsToFind.Count - 1
            If Gr.Keys.Contains(StringsToFind(J)) Then
              Gr(StringsToFind(J)) += 1
            Else
              Gr.Add(StringsToFind(J), 1)
            End If
          Next
        Next
        For Each key As String In (From k In Gr.Keys Order By k)
          ListBox1.Items.Add(key)
          ListBox2.Items.Add("Occurences (" & key & ")= " & Gr(key).ToString)
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, February 16, 2019 5:26 PM
    • Marked as answer by mipakteh Tuesday, February 19, 2019 3:24 AM
    Saturday, February 16, 2019 5:16 PM

All replies

  • Hi All,

    I have two click_button.

    Button_2 not count correctly at "017".I suspect because of "string or integer" but I don't how to do.

    button_3 show correctly but I need some improve if can.

    Thank.


    Hi mipakteh

    Why do you ALWAYS manage to confuse people? You state you have 2 buttons when yoiu actually have 3! You then fail to explain what the results are that you expect to get versus the results you do get.

    What does 'Button_2 not count correctly at "017"' mean?

    This is the output (partial) I managed to get, so what should the count at '017' be if it is not 4?


    Regards Les, Livingston, Scotland

    Sunday, February 10, 2019 3:23 PM
  • Leshay...

    Button_1 for only to show 3 digit.

    Button_2 and Button_3 to count that 3 digit but different event or way.It just for comparison for myself only.

    When I test both , I get the different result for the digit '017'.Button_2 count 4 but Button_3 count 2.

    Expected result of count is 2.

    I need click Button_2 and Button_3 to show the same result =2

    Sorry always make you confuse.


    • Edited by mipakteh Monday, February 11, 2019 3:46 PM add
    Monday, February 11, 2019 3:36 PM
  • Hi

    Not sure if this is of any help,but try it and see. Your code with a few changes.

    As can be seen in the image below, the "017" item is listed as 2 occurrances in both lists.

    Image

    Code

    Option Strict Off
    Option Explicit On
    
    Public Class Form1
      Private Data As New List(Of String)
      Private Data_A As New List(Of String)
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        'TxtFile:
        '8257,5321,0181,8279,8661,2120,6641,4000,0752,6975,9849,9106,7661,4522,4287,8613,3279,6296,4498,6003,8273,6642,5201
        '6235,5507,0126,9753,0354,0423,0328,6185,8449,3424,1481,1861,3234,5928,3503,1966,0513,2306,2315,3750,0190,1510,0063
        '1954,8691,7223,5869,2822,3609,3259,5081,4204,3116,5806,7052,2347,9977,5773,7416,4605,0541,0548,2138,6627,0172,0617
    
        Data.AddRange(IO.File.ReadAllLines(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "3Lines.txt")))
    
    
        While (Data(Data.Count - 1) = "")
          Data.RemoveAt(Data.Count - 1)
        End While
    
        For i As Integer = 0 To Data.Count - 1
          TextBox1.AppendText(Data(i) & vbCrLf)
        Next
    
    
      End Sub
    
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For I As Integer = 0 To Data.Count - 1
    
          Dim StringToCheck As String = Data(I).ToString
          Dim StringsToCheck As String() = StringToCheck.Split(","c)
          For Each S As String In StringsToCheck
            '0 1 2 3
            '012
            '013
            '123
            '023
            Dim StringsToFind As String() = {S(0) & S(1) & S(2),
                                                     S(0) & S(1) & S(3),
                                                     S(1) & S(2) & S(3),
                                                     S(0) & S(2) & S(3)}
    
            For J As Integer = 0 To StringsToFind.Count - 1
              Data_A.Add(StringsToFind(J))
            Next
          Next
        Next
        ListBox1.DataSource = Data_A
      End Sub
    
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim sb As New Text.StringBuilder
        Dim counts As New Dictionary(Of String, Integer)
        For Each entry As String In Data_A
          counts(entry) = 0
        Next
    
        For Each st As String In Data_A
          Dim matchQuery = From item In Data_A
                           Where item.ToLower = st.ToLower Select item
          counts(st) += 1
        Next
        For Each key As String In (From k In counts.Keys Order By k)
          ListBox2.Items.Add("Occurences (" & key & ")= " & counts(key).ToString)
        Next
      End Sub
    
      Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim sb_Count_A As New Text.StringBuilder
    
        For Each S As String In Data_A
    
          Dim StringsToFind As String() = {S(0), S(1), S(2)}
          For j As Integer = 0 To StringsToFind.Count - 3
            Dim Count As Integer = 0
            For Each Check As String In Data_A
              If StringsToFind(0) <> StringsToFind(1) AndAlso StringsToFind(1) <> StringsToFind(2) AndAlso StringsToFind(0) <> StringsToFind(2) Then
                If Check.Contains(StringsToFind(j)) AndAlso Check.Contains(StringsToFind(j + 1)) AndAlso Check.Contains(StringsToFind(j + 2)) Then Count += 1
              ElseIf Check = S Then
                Count += 1
              End If
            Next
            ListBox3.Items.Add("Occurances (" & S & ", " & StringsToFind(j) & StringsToFind(j + 1) & StringsToFind(j + 2) & ")= " & Count.ToString & vbCrLf)
          Next
        Next
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Monday, February 11, 2019 6:41 PM
  •   Leshay ,Thank you for feedback but have error.

    Warning 1 Using the iteration variable in a query expression may have unexpected results.  Instead, create a local variable within the loop and assign it the value of the iteration variable. C:\Users\Uset\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 58 51 WindowsApplication1

    at Line: st

      Where item.ToLower = st.ToLower Select item

    Tuesday, February 12, 2019 3:54 PM
  • Hi

    I can't imagine why you are getting that as it works fine here.

    I have changed the Option Strict Off to Option Strict On and added an 'As' clause to the query.

    Try replacing the For.....Next block of code with this code (and also change the Opotions as mentioned - see if that makes any difference.

    Changed code block

     For Each st As String In Data_A
       Dim matchQuery As IEnumerable(Of String) = From item In Data_A Where item.ToLower = st.ToLower Select item
       counts(st) += 1
     Next
    

    Full code (if needed)

    Option Strict On
    Option Explicit On
    
    Public Class Form1
      Private Data As New List(Of String)
      Private Data_A As New List(Of String)
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        'TxtFile:
        '8257,5321,0181,8279,8661,2120,6641,4000,0752,6975,9849,9106,7661,4522,4287,8613,3279,6296,4498,6003,8273,6642,5201
        '6235,5507,0126,9753,0354,0423,0328,6185,8449,3424,1481,1861,3234,5928,3503,1966,0513,2306,2315,3750,0190,1510,0063
        '1954,8691,7223,5869,2822,3609,3259,5081,4204,3116,5806,7052,2347,9977,5773,7416,4605,0541,0548,2138,6627,0172,0617
    
        Data.AddRange(IO.File.ReadAllLines(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "3Lines.txt")))
    
    
        While (Data(Data.Count - 1) = "")
          Data.RemoveAt(Data.Count - 1)
        End While
    
        For i As Integer = 0 To Data.Count - 1
          TextBox1.AppendText(Data(i) & vbCrLf)
        Next
    
    
      End Sub
    
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For I As Integer = 0 To Data.Count - 1
    
          Dim StringToCheck As String = Data(I).ToString
          Dim StringsToCheck As String() = StringToCheck.Split(","c)
          For Each S As String In StringsToCheck
            '0 1 2 3
            '012
            '013
            '123
            '023
            Dim StringsToFind As String() = {S(0) & S(1) & S(2),
                                                     S(0) & S(1) & S(3),
                                                     S(1) & S(2) & S(3),
                                                     S(0) & S(2) & S(3)}
    
            For J As Integer = 0 To StringsToFind.Count - 1
              Data_A.Add(StringsToFind(J))
            Next
          Next
        Next
        ListBox1.DataSource = Data_A
      End Sub
    
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim sb As New Text.StringBuilder
        Dim counts As New Dictionary(Of String, Integer)
        For Each entry As String In Data_A
          counts(entry) = 0
        Next
    
        For Each st As String In Data_A
          Dim matchQuery As IEnumerable(Of String) = From item In Data_A Where item.ToLower = st.ToLower Select item
          counts(st) += 1
        Next
        For Each key As String In (From k In counts.Keys Order By k)
          ListBox2.Items.Add("Occurences (" & key & ")= " & counts(key).ToString)
        Next
      End Sub
    
      Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim sb_Count_A As New Text.StringBuilder
    
        For Each S As String In Data_A
    
          Dim StringsToFind As String() = {S(0), S(1), S(2)}
          For j As Integer = 0 To StringsToFind.Count - 3
            Dim Count As Integer = 0
            For Each Check As String In Data_A
              If StringsToFind(0) <> StringsToFind(1) AndAlso StringsToFind(1) <> StringsToFind(2) AndAlso StringsToFind(0) <> StringsToFind(2) Then
                If Check.Contains(StringsToFind(j)) AndAlso Check.Contains(StringsToFind(j + 1)) AndAlso Check.Contains(StringsToFind(j + 2)) Then Count += 1
              ElseIf Check = S Then
                Count += 1
              End If
            Next
            ListBox3.Items.Add("Occurances (" & S & ", " & StringsToFind(j) & StringsToFind(j + 1) & StringsToFind(j + 2) & ")= " & Count.ToString & vbCrLf)
          Next
        Next
      End Sub
    End Class
    



    Regards Les, Livingston, Scotland

    Tuesday, February 12, 2019 4:38 PM
  • Thank you for advice,Leshay.I just change to...

    Option Strict On
    Option Explicit On
    
    Public Class Form1
    
        Private Data As New List(Of String)
        Private Data_A As New List(Of String)
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Data.AddRange(IO.File.ReadAllLines(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "3Lines.txt")))
    
    
            While (Data(Data.Count - 1) = "")
                Data.RemoveAt(Data.Count - 1)
            End While
    
            For i As Integer = 0 To Data.Count - 1
                TextBox1.AppendText(Data(i) & vbCrLf)
            Next
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For I As Integer = 0 To Data.Count - 1
    
                Dim StringToCheck As String = Data(I).ToString
                Dim StringsToCheck As String() = StringToCheck.Split(","c)
                For Each S As String In StringsToCheck
                    '0 1 2 3
                    '012
                    '013
                    '123
                    '023
                    Dim StringsToFind As String() = {S(0) & S(1) & S(2),
                                                             S(0) & S(1) & S(3),
                                                             S(1) & S(2) & S(3),
                                                             S(0) & S(2) & S(3)}
    
                    For J As Integer = 0 To StringsToFind.Count - 1
                        Data_A.Add(StringsToFind(J))
                    Next
                Next
            Next
            ListBox1.DataSource = Data_A
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim counts As New Dictionary(Of String, Integer)
            For Each entry As String In Data_A
                counts(entry) = 0
            Next
    
            For i As Integer = 0 To Data_A.Count - 1
                Dim st As String = Data_A(i)
                Dim matchQuery As IEnumerable(Of String) = From item In Data_A Where item.ToLower = st.ToLower Select item
                counts(st) += 1
            Next
    
            For Each key As String In (From k In counts.Keys Order By k)
                ListBox2.Items.Add("Occurences (" & key & ")= " & counts(key).ToString)
            Next
    
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            For Each S As String In Data_A
    
                Dim StringsToFind As String() = {S(0), S(1), S(2)}
                For j As Integer = 0 To StringsToFind.Count - 3
                    Dim Count As Integer = 0
                    For Each Check As String In Data_A
                        If StringsToFind(0) <> StringsToFind(1) AndAlso StringsToFind(1) <> StringsToFind(2) AndAlso StringsToFind(0) <> StringsToFind(2) Then
                            If Check.Contains(StringsToFind(j)) AndAlso Check.Contains(StringsToFind(j + 1)) AndAlso Check.Contains(StringsToFind(j + 2)) Then Count += 1
                        ElseIf Check = S Then
                            Count += 1
                        End If
                    Next
                    ListBox3.Items.Add("Occurances (" & S & ", " & StringsToFind(j) & StringsToFind(j + 1) & StringsToFind(j + 2) & ")= " & Count.ToString & vbCrLf)
                Next
            Next
        End Sub
    End Class
    

    It work smooth but count digit still wrong.See..

    "825"...button_2 show result 1 but button_3 show result 2

    Wednesday, February 13, 2019 1:09 PM

  • It work smooth but count digit still wrong.See..

    "825"...button_2 show result 1 but button_3 show result 2

    Hi

    So, tell me which one is correct and which is wrong - I am certainly not going to manually check myself.


    Regards Les, Livingston, Scotland

    Wednesday, February 13, 2019 1:40 PM
  • okey...

    Button_3 is correct because first line have 8257 and second line 5928.We don't care about position of digit.

    Hope Leshay guide to find how to do.

    Thank you very much.

    Wednesday, February 13, 2019 1:55 PM
  • okey...

    Button_3 is correct because first line have 8257 and second line 5928.We don't care about position of digit.

    Hope Leshay guide to find how to do.

    Thank you very much.

    Hi

    If you check your code, you will see that you have specified a subset (4) of all possible combinations (24) for the StringToFind array. Now you say that the number 825 contained in 5928 has been omitted but that would be the combination S(3) & S(2) & S(0) which you did not include in the StringToFind array.

    *

    These are the 24 possibilities.

    {0,1,2}, {0,2,1}, {0,1,3}, {0,3,1}, {0,2,3}, {0,3,2}, {1,0,2}, {1,2,0}, {1,0,3}, {1,3,0}, {1,2,3}, {1,3,2}, {2,0,1}, {2,1,0}, {2,0,3}, {2,3,0}, {2,1,3}, {2,3,1}, {3,0,1}, {3,1,0}, {3,0,2}, {3,2,0}, {3,1,2}, {3,2,1}


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, February 13, 2019 3:28 PM
    Wednesday, February 13, 2019 3:27 PM
  • Good...If digit not having same then count is correct but if digit having same it not correctly count.At least I understand.

    Next, How to do count correctly if having same digit Example;

    "662 code count 6, expected is 3.if i wrong , what is true.

    Option Strict On
    Option Infer On
    Option Explicit On
    
    Imports System.IO
    Imports System.Data
    Imports System.Text
    
    Imports System.Windows.Forms
    
    Public Class Form1
    
        Private Data As New List(Of String)
        Private Data_A As New List(Of String)
        Private Data_A1 As New List(Of String)
    
        Private ThreeDigit_A As New List(Of String)
        Private CountDigit_A As New List(Of Integer)
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TxtFile:
            '8257,5321,0181,8279,8661,2120,6641,4000,0752,6975,9849,9106,7661,4522,4287,8613,3279,6296,4498,6003,8273,6642,5201
            '6235,5507,0126,9753,0354,0423,0328,6185,8449,3424,1481,1861,3234,5928,3503,1966,0513,2306,2315,3750,0190,1510,0063
            '1954,8691,7223,5869,2822,3609,3259,5081,4204,3116,5806,7052,2347,9977,5773,7416,4605,0541,0548,2138,6627,0172,0617
    
            Data.AddRange(IO.File.ReadAllLines(IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "3Lines.txt")))
    
            While (Data(Data.Count - 1) = "")
                Data.RemoveAt(Data.Count - 1)
            End While
    
            For i As Integer = 0 To Data.Count - 1
                TextBox1.AppendText(Data(i) & vbCrLf)
            Next
    
        End Sub
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim sb As New StringBuilder
    
            For I As Integer = 0 To Data.Count - 1
    
                Dim StringToCheck As String = Data(I).ToString
                Dim StringsToCheck As String() = StringToCheck.Split(","c)
                For Each S As String In StringsToCheck
                    '0 1 2 3
                    '012
                    '013
                    '123
                    '023
                    Dim StringsToFind As String() = {S(0) & S(1) & S(2), S(0) & S(2) & S(1), S(0) & S(1) & S(3),
                        S(0) & S(3) & S(1), S(0) & S(2) & S(3), S(0) & S(3) & S(2),
                        S(1) & S(0) & S(2), S(1) & S(2) & S(0), S(1) & S(0) & S(3),
                        S(1) & S(3) & S(0), S(1) & S(2) & S(3), S(1) & S(3) & S(2),
                        S(2) & S(0) & S(1), S(2) & S(1) & S(0), S(2) & S(0) & S(3),
                        S(2) & S(3) & S(0), S(2) & S(1) & S(3), S(2) & S(3) & S(1),
                        S(3) & S(0) & S(1), S(3) & S(1) & S(0), S(3) & S(0) & S(2),
                        S(3) & S(2) & S(0), S(3) & S(1) & S(2), S(3) & S(2) & S(1)}
    
    
                    For J As Integer = 0 To StringsToFind.Count - 1
                        sb.Append(StringsToFind(J) & vbCrLf)
                        Data_A.Add(StringsToFind(J))
                    Next
                Next
            Next
            ListBox1.DataSource = Data_A
    
        End Sub
    
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
           Dim counts As New Dictionary(Of String, Integer)
            For Each entry As String In Data_A
                counts(entry) = 0
            Next
    
            For I As Integer = 0 To Data_A.Count - 1
                Dim st As String = Data_A(I)
                Dim matchQuery As IEnumerable(Of String) = From item In Data_A Where item.ToLower = st.ToLower Select item
                counts(st) += 1
            Next
            For Each key As String In (From k In counts.Keys Order By k)
                ListBox2.Items.Add("Occurences (" & key & ")= " & counts(key).ToString)
            Next
    
        End Sub

    Thursday, February 14, 2019 1:33 PM
  • Good...If digit not having same then count is correct but if digit having same it not correctly count.At least I understand.

    Next, How to do count correctly if having same digit Example;

    "662 code count 6, expected is 3.if i wrong , what is true.

    Hi

    It is very difficult to determine what you are actually wanting.

    Here is another variation which shows the desired 3 count for the value 662 - HOWEVER, check 266 and 626 as well - they are each 3 count - and they are the same values differently ordered.

    I have also altered the code significantly, but only to be an easier stand alone example. (Doesn't use the file now)

    The main change that effect the results are highlighted in the code below.

    Image

    Code

    Option Strict On
    Option Explicit On
    
    Public Class Form1
      Private Data_A As New List(Of String)
      Private StringToCheck As String
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        StringToCheck = String.Join(","c, "8257,5321,0181,8279,8661,2120,6641,4000,0752,6975,9849,9106,7661,4522,4287,8613,3279,6296,4498,6003,8273,6642,5201", "6235,5507,0126,9753,0354,0423,0328,6185,8449,3424,1481,1861,3234,5928,3503,1966,0513,2306,2315,3750,0190,1510,0063", "1954,8691,7223,5869,2822,3609,3259,5081,4204,3116,5806,7052,2347,9977,5773,7416,4605,0541,0548,2138,6627,0172,0617")
        Button1.PerformClick()
        Button2.PerformClick()
      End Sub
      Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        Dim StringsToCheck As String() = StringToCheck.Split(","c)
        For Each S As String In StringsToCheck
          Dim StringsToFind As String() = {
              S(0) & S(1) & S(2), S(0) & S(2) & S(1),
              S(0) & S(1) & S(3), S(0) & S(3) & S(1),
              S(0) & S(2) & S(3), S(0) & S(3) & S(2),
              S(1) & S(0) & S(2), S(1) & S(2) & S(0),
              S(1) & S(0) & S(3), S(1) & S(3) & S(0),
              S(1) & S(2) & S(3), S(1) & S(3) & S(2),
              S(2) & S(0) & S(1), S(2) & S(1) & S(0),
              S(2) & S(0) & S(3), S(2) & S(3) & S(0),
              S(2) & S(1) & S(3), S(2) & S(3) & S(1),
              S(3) & S(0) & S(1), S(3) & S(1) & S(0),
              S(3) & S(0) & S(2), S(3) & S(2) & S(0),
              S(3) & S(1) & S(2), S(3) & S(2) & S(1)
            }
          Dim done As New List(Of String)
          For J As Integer = 0 To StringsToFind.Count - 1
            If Not done.Contains(StringsToFind(J)) Then
              Data_A.Add(StringsToFind(J))
              done.Add(StringsToFind(J))
            End If
          Next
        Next
        Data_A.Sort()
        ListBox1.DataSource = Data_A
      End Sub
      Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
    
        Dim counts As New Dictionary(Of String, Integer)
        For Each entry As String In Data_A
          counts(entry) = 0
        Next
    
        For I As Integer = 0 To Data_A.Count - 1
          Dim st As String = Data_A(I)
          Dim matchQuery As IEnumerable(Of String) = From item In Data_A Where item = st Select item
          counts(st) += 1
        Next
        For Each key As String In (From k In counts.Keys Order By k)
          ListBox2.Items.Add("Occurences (" & key & ")= " & counts(key).ToString)
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland




    • Edited by leshay Friday, February 15, 2019 2:43 AM
    • Marked as answer by mipakteh Saturday, February 16, 2019 4:51 AM
    Thursday, February 14, 2019 9:59 PM
  • Thank you very..very much Leshay.I understand .It enough already for your example.
    Saturday, February 16, 2019 4:51 AM
  • Thank you very..very much Leshay.I understand .It enough already for your example.

    Hi

    You are welcome.

    BTW: here is a compact version. Only 2 ListBoxes.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Gr As New Dictionary(Of String, Integer)
      Private StringsToCheck As String()
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        StringsToCheck = String.Join(","c, "8257,5321,0181,8279,8661,2120,6641,4000,0752,6975,9849,9106,7661,4522,4287,8613,3279,6296,4498,6003,8273,6642,5201", "6235,5507,0126,9753,0354,0423,0328,6185,8449,3424,1481,1861,3234,5928,3503,1966,0513,2306,2315,3750,0190,1510,0063", "1954,8691,7223,5869,2822,3609,3259,5081,4204,3116,5806,7052,2347,9977,5773,7416,4605,0541,0548,2138,6627,0172,0617").Split(","c)
        For Each S As String In StringsToCheck
          Dim StringsToFind As IEnumerable(Of String) = {
              S(0) & S(1) & S(2), S(0) & S(2) & S(1),
              S(0) & S(1) & S(3), S(0) & S(3) & S(1),
              S(0) & S(2) & S(3), S(0) & S(3) & S(2),
              S(1) & S(0) & S(2), S(1) & S(2) & S(0),
              S(1) & S(0) & S(3), S(1) & S(3) & S(0),
              S(1) & S(2) & S(3), S(1) & S(3) & S(2),
              S(2) & S(0) & S(1), S(2) & S(1) & S(0),
              S(2) & S(0) & S(3), S(2) & S(3) & S(0),
              S(2) & S(1) & S(3), S(2) & S(3) & S(1),
              S(3) & S(0) & S(1), S(3) & S(1) & S(0),
              S(3) & S(0) & S(2), S(3) & S(2) & S(0),
              S(3) & S(1) & S(2), S(3) & S(2) & S(1)
            }.Distinct
          For J As Integer = 0 To StringsToFind.Count - 1
            If Gr.Keys.Contains(StringsToFind(J)) Then
              Gr(StringsToFind(J)) += 1
            Else
              Gr.Add(StringsToFind(J), 1)
            End If
          Next
        Next
        For Each key As String In (From k In Gr.Keys Order By k)
          ListBox1.Items.Add(key)
          ListBox2.Items.Add("Occurences (" & key & ")= " & Gr(key).ToString)
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, February 16, 2019 5:26 PM
    • Marked as answer by mipakteh Tuesday, February 19, 2019 3:24 AM
    Saturday, February 16, 2019 5:16 PM
  • thank Leshay,I appreciate for your advice

    Tuesday, February 19, 2019 3:20 AM