Jump to content

How to count the times one element appears continuously in a serials of arrays?


Recommended Posts

Hey ,guys, I am a new comer here. I come across an troublesome problem:

Imagine there are five arrays:

$a1[5]=[1,2,3,4,5]

$a2[5]=[2,3,4,9,10]

$a3[5]=[2,1,3,4,7]

$a4[5]=[9,8,6,5,4]

$a5[5]=[3,2,1,5,6]

… …

The Element 2 appears in $a,$b and $c, so Element 2 appears 3 times continuously. The element 1 appears in $a and $c and $c, so element 1 does not appear any time continuously. I wonder how to to count the times any element appears continuously in such a serials of arrays. It seems complicated. Is there any simple method?

Thanks for you attention and you kind help.

Edited by eastern314
Link to comment
Share on other sites

  • Moderators

eastern314,

Welcome to the AutoIt forum. :)

Interesting little problem. Probably not the most elegant solution, but it seems to work: ;)

#include <Array.au3>

Global $aArray[5][5] = [ _
                        [1, 2, 3, 4, 5], _
                        [2, 3, 4, 9, 10], _
                        [2, 1, 3, 4, 7], _
                        [9, 8, 6, 5, 4], _
                        [3, 2, 1, 5, 6]]

Global $aCount[11]
Global $aContinuous[11]
Global $aResults[11]

; For each line
For $i = 0 To 4

    ; Reset current values array
    Global $aCurrent[11]

    ; Run through elements and set Current elements
    For $j = 0 To 4
        $iItem = $aArray[$i][$j]
        $aCurrent[$iItem] = 1
    Next

    ; Go through elements and check continuity
    For $j = 1 To 10
        ; Is element present
        If $aCurrent[$j] Then
            ; Increase count
            $aContinuous[$j] += 1
        Else
            ; Not present so check for previous continuity
            If $aContinuous[$j] > 1 Then $aResults[$j] += 1
            $aContinuous[$j] = 0
        EndIf
    Next

Next

; Check for existing continuity
For $j = 1 To 10
    If $aContinuous[$j] > 1 Then $aResults[$j] += 1
Next

_ArrayDisplay($aResults, "Final Results")

I hope it does what you want. :idiot:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

eastern314,

Welcome to the AutoIt forum. :)

Interesting little problem. Probably not the most elegant solution, but it seems to work: ;)

#include <Array.au3>

Global $aArray[5][5] = [ _
                        [1, 2, 3, 4, 5], _
                        [2, 3, 4, 9, 10], _
                        [2, 1, 3, 4, 7], _
                        [9, 8, 6, 5, 4], _
                        [3, 2, 1, 5, 6]]

Global $aCount[11]
Global $aContinuous[11]
Global $aResults[11]

; For each line
For $i = 0 To 4

    ; Reset current values array
    Global $aCurrent[11]

    ; Run through elements and set Current elements
    For $j = 0 To 4
        $iItem = $aArray[$i][$j]
        $aCurrent[$iItem] = 1
    Next

    ; Go through elements and check continuity
    For $j = 1 To 10
        ; Is element present
        If $aCurrent[$j] Then
            ; Increase count
            $aContinuous[$j] += 1
        Else
            ; Not present so check for previous continuity
            If $aContinuous[$j] > 1 Then $aResults[$j] += 1
            $aContinuous[$j] = 0
        EndIf
    Next

Next

; Check for existing continuity
For $j = 1 To 10
    If $aContinuous[$j] > 1 Then $aResults[$j] += 1
Next

_ArrayDisplay($aResults, "Final Results")

I hope it does what you want. :idiot:

M23

Thanks very much for your help. But i am so sorry that I made quite a lot of mistakes in my short question and i dont know how to edit it. As a new comer, i am not familiar with this forum very much. Please forgive me. Anyway , I will the method you give and learn from you. thanks again.
Link to comment
Share on other sites

  • Moderators

eastern314,

You need a certain number of posts (5?) before you get the ability to edit your posts. :)

If that does not do what you want, then what exactly do you want to do? I like little puzzles like this - keeps the litle grey cells active. :idiot:

M23

P.S. When you reply please use the "Add Reply" button at the top and bottom of the page rather then the "Reply" button in the post itself. That way you do not get the contents of the previous post quoted in your reply and the whole thread becomes easier to read. ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks for you help,M23. I thought about the problems over and over again and I suddenly got a good idea. If you have some suggestion, pls let me know, thanks.

Attached is an ini file from which I read the serial of arrays.

#include <WindowsConstants.au3>
#include <array.au3>

    $total=IniReadSection(@ScriptDir&"\test.ini","test")
    For $v=1 To 49
        Dim $period[31]
        Dim $t=0
        Dim $j=0,$value[31]
        For $i=0 To 30
            
            $period[$i]=StringSplit($total[$i][1],",")
            
            If _ArraySearch($period[$i],$v,1)>0 Then
                $t+=1           
            EndIf
            If _ArraySearch($period[$i],$v,1)>0 Then
                $value[$j]=1
                $j+=1
            EndIf
            If _ArraySearch($period[$i],$v,1)=-1 Then
                $value[$j]=0
                $j+=1
            EndIf
            
        Next
                        
            $a=StringReplace(_ArrayToString($value),"|","")
            $b=StringSplit($a,"0")
            Dim $two=0,$three=0,$four=0,$five=0
            For $p=1 To UBound($b)-1
                If $b[$p]="11" Then
                    $two+=1
                EndIf
                If $b[$p]="111" Then
                    $three+=1
                EndIf
                If $b[$p]="1111" Then
                    $four+=1
                EndIf
                ;If $b[$p]="11111" Then
                ;   $five+=1
                ;EndIf
                Next
                
                MsgBox(0,"","Number "&$v&" appears "&$t&" times in total; "&$two&" times appears twice continuously; "&$three&" times appears tripe continuously; "&$four&" times appears quartic continuously.")
            Next

test.zip

Edited by eastern314
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...