Jump to content

Min Max, how?


Recommended Posts

... I was working on a way to sort certain data by preference.

AutoIt was my choice because of that ".. new frontiers" stuff in humans :)

Anyways, problem is how to sort this (number at right represents preference number, higher the number, lower the preference):

John - 3

Malcom - 7

Barbara - 1

Frank - 2345

Diane - 99

...

Output should be (most preferenced at the top and so on):

Barbara

John

Malcom

Diane

Frank

AutoIt have no function for this (checked UDF-s, nothing there too), or am I wrong?

After resolving the problem I eager to show how ( muttley ), but I'm under 5 posts to post elsewhere but here.

I hope nobody would mind.

This function will sort list of numbers by value (that is the most problematic part of the endeavour). It is made as some Math.au3 part. _Min and _Max functions of Math.au3 can be swamped with this, allowing it to deal with no limitation on how many numbers is to compare (not by this function, anyway)

Opt("MustDeclareVars", 1)


Dim $list = "-6, 5, 5, -1.457, 125.456, -13768.14159, -234, -5678.945, 456.45, 3"

Dim $example_max = Max($list, 0)
Dim $example_min = Min($list, 1)


MsgBox(0, "", "In list of numbers: " & $list & @CRLF & "the biggest is: " & $example_max & @CRLF & "and the smallest absolute value is: " & $example_min )




Func Min($expression, $abs=0) 
    
Local $array = StringSplit(StringReplace($expression, " ", ""), ",", 1)

For $af = 1 To $array[0]
    If $array[$af]<>"0" And Number($array[$af])=0 Then
        Return ("")
        SetError(1)   ; at least one element of provided list is not a number 
    EndIf
    If $abs = 1 Then $array[$af] = Abs($array[$af]) ; absolute value if required
Next

Local $min = InternalMath($array)

Return $min[1]

EndFunc






Func Max($expression, $abs=0)
    
Local $array = StringSplit(StringReplace($expression, " ", ""), ",", 1)

For $af = 1 To $array[0]
    If $array[$af]<>"0" And Number($array[$af])=0 Then
        Return ("")
        SetError(1)   ; at least one element of provided list is not a number 
    EndIf
    If $abs = 1 Then $array[$af] = Abs($array[$af]) ; absolute value if required
Next

Local $max = InternalMath($array)

Return $max[$max[0]]

EndFunc










Func InternalMath($array)
    
ReDim $array[2*$array[0]+1]  ;expanding original array (doubling it), new elements will store
                             ;indexing information about ones in original array

For $ij = $array[0]+1 To 2*$array[0]
    $array[$ij] = 1               ;asigning all new elements with initial index ("1")         
Next


For $bf = 1 To $array[0]      
    For $cf = 1 To $array[0]                                                          ;indexing (if indexed element is
        If $array[$cf]-$array[$bf] > 0 Then $array[$cf + $array[0]] += 1              ;higher than compared one, its 
    Next                                                                              ;index will rise by one)
Next


#cs
    Original array could contain elements that are equal.
    Index of that elements would return the same. For example:
    
                                             Original array having for elements; 34, 56, 34, 98
                                                        This would produce indexes; 1, 3, 1, 4
    
    Next part will assign every other same element with +1 resulting in new indexes. 
    
                                       For example above it will adjust indexes to; 1, 3, 2, 4  
#ce

For $df = $array[0]+1 To 2*$array[0]
    For $ef = $array[0]+1 To 2*$array[0]
        If $ef = $df Then ContinueLoop  ; do not compare the same elements
        If $array[$ef]-$array[$df] = 0 Then $array[$ef] += 1   ; and deal with equals       
    Next
Next

; Follows the obvious

Dim $aux_array[$array[0]+1] ; creating new array in size of the original one

$aux_array[0] = $array[0]   ; asigning [0]- element

For $ff = 1 To $array[0]
    $aux_array[$array[$array[0]+$ff]] = $array[$ff] ; and every other depending on index number
Next
    
Return $aux_array  ; this goes out (original array could be replaced with this one but this way opens some other possibilities)

EndFunc

I'm new at AutoIt and would appreciate comments on that script.

Once again, sorry for the wrong forum.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

... I was working on a way to sort certain data by preference.

AutoIt was my choice because of that ".. new frontiers" stuff in humans :)

Anyways, problem is how to sort this (number at right represents preference number, higher the number, lower the preference):

John - 3

Malcom - 7

Barbara - 1

Frank - 2345

Diane - 99

...

Output should be (most preferenced at the top and so on):

Barbara

John

Malcom

Diane

Frank

AutoIt have no function for this (checked UDF-s, nothing there too), or am I wrong?

After resolving the problem I eager to show how ( muttley ), but I'm under 5 posts to post elsewhere but here.

I hope nobody would mind.

This function will sort list of numbers by value (that is the most problematic part of the endeavour). It is made as some Math.au3 part. _Min and _Max functions of Math.au3 can be swamped with this, allowing it to deal with no limitation on how many numbers is to compare (not by this function, anyway)

Opt("MustDeclareVars", 1)


Dim $list = "-6, 5, 5, -1.457, 125.456, -13768.14159, -234, -5678.945, 456.45, 3"

Dim $example_max = Max($list, 0)
Dim $example_min = Min($list, 1)


MsgBox(0, "", "In list of numbers: " & $list & @CRLF & "the biggest is: " & $example_max & @CRLF & "and the smallest absolute value is: " & $example_min )




Func Min($expression, $abs=0) 
    
Local $array = StringSplit(StringReplace($expression, " ", ""), ",", 1)

For $af = 1 To $array[0]
    If $array[$af]<>"0" And Number($array[$af])=0 Then
        Return ("")
        SetError(1)  ; at least one element of provided list is not a number 
    EndIf
    If $abs = 1 Then $array[$af] = Abs($array[$af]); absolute value if required
Next

Local $min = InternalMath($array)

Return $min[1]

EndFunc






Func Max($expression, $abs=0)
    
Local $array = StringSplit(StringReplace($expression, " ", ""), ",", 1)

For $af = 1 To $array[0]
    If $array[$af]<>"0" And Number($array[$af])=0 Then
        Return ("")
        SetError(1)  ; at least one element of provided list is not a number 
    EndIf
    If $abs = 1 Then $array[$af] = Abs($array[$af]); absolute value if required
Next

Local $max = InternalMath($array)

Return $max[$max[0]]

EndFunc




Func InternalMath($array)
    
ReDim $array[2*$array[0]+1] ;expanding original array (doubling it), new elements will store
                            ;indexing information about ones in original array

For $ij = $array[0]+1 To 2*$array[0]
    $array[$ij] = 1              ;asigning all new elements with initial index ("1")          
Next


For $bf = 1 To $array[0]      
    For $cf = 1 To $array[0]                                                         ;indexing (if indexed element is
        If $array[$cf]-$array[$bf] > 0 Then $array[$cf + $array[0]] += 1             ;higher than compared one, its 
    Next                                                                             ;index will rise by one)
Next


#cs
    Original array could contain elements that are equal.
    Index of that elements would return the same. For example:
    
                                             Original array having for elements; 34, 56, 34, 98
                                                        This would produce indexes; 1, 3, 1, 4
    
    Next part will assign every other same element with +1 resulting in new indexes. 
    
                                       For example above it will adjust indexes to; 1, 3, 2, 4  
#ce

For $df = $array[0]+1 To 2*$array[0]
    For $ef = $array[0]+1 To 2*$array[0]
        If $ef = $df Then ContinueLoop ; do not compare the same elements
        If $array[$ef]-$array[$df] = 0 Then $array[$ef] += 1  ; and deal with equals        
    Next
Next

; Follows the obvious

Dim $aux_array[$array[0]+1]; creating new array in size of the original one

$aux_array[0] = $array[0]  ; asigning [0]- element

For $ff = 1 To $array[0]
    $aux_array[$array[$array[0]+$ff]] = $array[$ff]; and every other depending on index number
Next
    
Return $aux_array ; this goes out (original array could be replaced with this one but this way opens some other possibilities)

EndFunc

I'm new at AutoIt and would appreciate comments on that script.

Once again, sorry for the wrong forum.

Look in help file abut _ArrayMax(), _ArrayMin() and _ArraySort().

When the words fail... music speaks.

Link to comment
Share on other sites

#include <Array.au3>
Global $test_A[5] = ["John - 3","Malcom - 7", "Barbara - 1", "Frank - 2345", "Diane - 99"]

$re = _sortNummeric($test_A)

_ArrayDisplay($re)

Func _sortNummeric(ByRef $array)
    Local $t_A[UBound($array)][2], $re_A[UBound($array)]
    For $i = 0 To UBound($array) - 1
        $t_A[$i][0] = Int(StringRegExpReplace($array[$i], '[^0-9]', ''))
        $t_A[$i][1] = $array[$i]
    Next
    _ArraySort($t_A, 0, 0, 0, 0)
    For $i = 0 To UBound($array) - 1
        $re_A[$i] = $t_A[$i][1]
    Next
    Return $re_A
EndFunc   ;==>_sortNummeric

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

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...