 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Sun Jun 21, 2009 7:54 pm Post subject: Permutation Sort |
|
|
The horribly slow Permutation Sort uses the useful permutation generator: NextPerm(array_name): | Code: | MsgBox % PermSort("")
MsgBox % PermSort("xxx")
MsgBox % PermSort("3,2,1")
MsgBox % PermSort("dog,000000,xx,cat,pile,abcde,1,cat")
PermSort(var) { ; SORT COMMA SEPARATED LIST
Local i, sorted
StringSplit a, var, `, ; make array, size = a0
v0 := a0 ; auxiliary array for permutations
Loop %v0%
v%A_Index% := A_Index
While unSorted("a","v") ; until sorted
NextPerm("v") ; try new permutations
Loop % a0 ; construct string from sorted array
i := v%A_Index%, sorted .= "," . a%i%
Return SubStr(sorted,2) ; drop leading comma
}
unSorted(a,v) {
Loop % %a%0-1 {
i := %v%%A_Index%, j := A_Index+1, j := %v%%j%
If (%a%%i% > %a%%j%)
Return 1
}
}
NextPerm(v) { ; the lexicographically next LARGER permutation of v1..v%v0%
Local i, i1, j, t
i := %v%0, i1 := i-1
While %v%%i1% >= %v%%i% {
--i, --i1
IfLess i1,1, Return 1 ; Signal the end
}
j := %v%0
While %v%%j% <= %v%%i1%
--j
t := %v%%i1%, %v%%i1% := %v%%j%, %v%%j% := t, j := %v%0
While i < j
t := %v%%i%, %v%%i% := %v%%j%, %v%%j% := t, ++i, --j
} |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Sun Jun 21, 2009 8:09 pm Post subject: Roots of Unity |
|
|
One can directly list the complex Roots of Unity: | Code: | n := 8, a := 8*atan(1)/n
Loop %n%
i := A_Index-1, t .= cos(a*i) ((s:=sin(a*i))<0 ? " - i*" . -s : " + i*" . s) "`n"
Msgbox % t |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Sun Jun 21, 2009 8:30 pm Post subject: SEDOL |
|
|
SEDOL is a checksum digit attached to 6 character words: | Code: | MsgBox % SEDOL("710889") ;7108899
MsgBox % SEDOL("B0YBKJ") ;B0YBKJ7
MsgBox % SEDOL("406566") ;4065663
MsgBox % SEDOL("B0YBLH") ;B0YBLH2
MsgBox % SEDOL("228276") ;2282765
MsgBox % SEDOL("B0YBKL") ;B0YBKL9
MsgBox % SEDOL("557910") ;5579107
MsgBox % SEDOL("B0YBKR") ;B0YBKR5
MsgBox % SEDOL("585284") ;5852842
MsgBox % SEDOL("B0YBKT") ;B0YBKT7
SEDOL(w) {
Static w1:=1,w2:=3,w3:=1,w4:=7,w5:=3,w6:=9
Loop Parse, w
s += ((c:=Asc(A_LoopField))>=Asc("A") ? c-Asc("A")+10 : c-Asc("0")) * w%A_Index%
Return w mod(10-mod(s,10),10)
} |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Sun Jun 21, 2009 8:42 pm Post subject: Sequence of Non-squares |
|
|
Very simple code verifies the validity of the formula for the Sequence of Non-squares: | Code: | Loop 22
t .= (A_Index + floor(0.5 + sqrt(A_Index))) " "
MsgBox %t%
s := 0
Loop 1000000
x := A_Index + floor(0.5 + sqrt(A_Index)), s += x = round(sqrt(x))**2
Msgbox Number of bad squares = %s% ; 0 |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Sun Jun 21, 2009 8:54 pm Post subject: Moving Average |
|
|
Moving Average:
| Code: | MsgBox % MovingAverage() ; reset: blank
MsgBox % MovingAverage(1) ; 1
MsgBox % MovingAverage(3) ; 2
MsgBox % MovingAverage(-1) ; 1
MovingAverage(x="") {
Static sum:=0, n:=0
If (x="") ; blank parameter: reset
sum := 0, n := 0
Else
sum += x, n++ ; update state
Return sum/n
} |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Sun Jun 21, 2009 9:17 pm Post subject: Standard Deviation |
|
|
The running Standard Deviation of a series of numbers: | Code: | std(2),std(4),std(4),std(4),std(5),std(5),std(7)
MsgBox % std(9) ; 2
std(x="") {
Static sum:=0, sqr:=0, n:=0
If (x="") ; blank parameter: reset
sum := 0, sqr := 0, n := 0
Else
sum += x, sqr += x*x, n++ ; update state
Return sqrt((sqr-sum*sum/n)/n)
} |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Sun Jun 21, 2009 9:38 pm Post subject: Selection Sort |
|
|
Selection Sort of comma separated lists: | Code: | MsgBox % SelecSort("")
MsgBox % SelecSort("xxx")
MsgBox % SelecSort("3,2,1")
MsgBox % SelecSort("dog,000000,xx,cat,pile,abcde,1,cat,zz,xx,z")
SelecSort(var) { ; SORT COMMA SEPARATED LIST
StringSplit a, var, `, ; make array, size = a0
Loop % a0-1 {
i := A_Index, mn := a%i%, j := m := i
Loop % a0-i { ; find minimum
j++
If (a%j% < mn)
mn := a%j%, m := j
}
t := a%i%, a%i% := a%m%, a%m% := t ; swap first with minimum
}
Loop % a0 ; construct string from sorted array
sorted .= "," . a%A_Index%
Return SubStr(sorted,2) ; drop leading comma
} |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Sun Jun 21, 2009 11:18 pm Post subject: Miller-Rabin test |
|
|
The Miller-Rabin test tells with large probability whether a number is prime. When it tells otherwise, the number is composite for sure. The chance of not finding a composite number depends on a parameter k (p < 4**-k). For 32 bit numbers k=3 suffice, if random trials are replaced with certain deterministic ones: | Code: | MsgBox % MillerRabin(999983,10) ; 1
MsgBox % MillerRabin(999809,10) ; 1
MsgBox % MillerRabin(999727,10) ; 1
MsgBox % MillerRabin(52633,10) ; 0
MsgBox % MillerRabin(60787,10) ; 0
MsgBox % MillerRabin(999999,10) ; 0
MsgBox % MillerRabin(999995,10) ; 0
MsgBox % MillerRabin(999991,10) ; 0
MillerRabin(n,k) { ; 0: composite, 1: probable prime (n < 2**31)
d := n-1, s := 0
While !(d&1)
d>>=1, s++
Loop %k% {
Random a, 2, n-2 ; if n < 4,759,123,141, it is enough to test a = 2, 7, and 61.
x := PowMod(a,d,n)
If (x=1 || x=n-1)
Continue
Cont := 0
Loop % s-1 {
x := PowMod(x,2,n)
If (x = 1)
Return 0
If (x = n-1) {
Cont = 1
Break
}
}
IfEqual Cont,1, Continue
Return 0
}
Return 1
}
PowMod(x,n,m) { ; x**n mod m
y := 1, i := n, z := x
While i>0
y := i&1 ? mod(y*z,m) : y, z := mod(z*z,m), i >>= 1
Return y
} |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Sun Jun 21, 2009 11:57 pm Post subject: Spiral |
|
|
The Spiral task is to fill up a square matrix with increasing numbers around and inward spiral. This script is based on the Python code: | Code: | n := 5, dx := x := y := v := 1, dy := 0
Loop % n*n {
a_%x%_%y% := v++
nx := x+dx, ny := y+dy
If (1 > nx || nx > n || 1 > ny || ny > n || a_%nx%_%ny%)
t := dx, dx := -dy, dy := t
x := x+dx, y := y+dy
}
Loop %n% { ; generate printout
y := A_Index ; for each row
Loop %n% ; and for each column
s .= a_%A_Index%_%y% "`t" ; attach stored index
s .= "`n" ; row is complete
}
MsgBox %s% ; show output |
The output: | Code: | ---------------------------
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
--------------------------- |
|
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 352
|
Posted: Mon Jun 22, 2009 2:24 pm Post subject: |
|
|
http://www.rosettacode.org/wiki/Determine_if_Only_One_Instance_is_Running
I wonder if #SingleInstance would count for this.
Ask:Replace former instance: | Code: | | #SingleInstance Force | Ignore rerun attempts: | Code: | | #SingleInstance Ignore | Allow multiple instances: | Code: | | #SingleInstance Off |
Technically... | Quote: | | This task is to determine if there is only one instance of an application running. | ... not necessarily to take action based on this information.
I think I've seen solutions for this posted on the forums before, but couldn't find it right away. If a solution is found, it may be informative to include a note about #SingleInstance. _________________
A great Beginner's Tutorial |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 308 Location: Houston, TX
|
Posted: Mon Jun 22, 2009 2:31 pm Post subject: dllcall with cpp example |
|
|
I wonder if something like this would work:
| Code: | dllcall("CreateMutex", "int", 0, "int", 1, "str", A_ScriptName)
Msgbox % ErrorLevel
msgbox % result := dllcall("GetLastError") |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Mon Jun 22, 2009 4:59 pm Post subject: |
|
|
| The task sounds like we have to tell for any application if it has multiple instances, not only AHK scripts. |
|
| Back to top |
|
 |
Last N rather than all. Guest
|
Posted: Mon Jun 22, 2009 7:04 pm Post subject: Re: Moving Average |
|
|
| Laszlo wrote: | Moving Average:
| Code: | MsgBox % MovingAverage() ; reset: blank
MsgBox % MovingAverage(1) ; 1
MsgBox % MovingAverage(3) ; 2
MsgBox % MovingAverage(-1) ; 1
MovingAverage(x="") {
Static sum:=0, n:=0
If (x="") ; blank parameter: reset
sum := 0, n := 0
Else
sum += x, n++ ; update state
Return sum/n
} |
|
Unfortunately you need to average the last N rather than all the numbers. - See other entries on the R.C. site. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Mon Jun 22, 2009 7:11 pm Post subject: Re: Moving Average |
|
|
| Last N rather than all. wrote: | | Unfortunately you need to average the last N rather than all the numbers. - See other entries on the R.C. site. |
| rosettacode wrote: | | function/class/instance that takes a number as argument and returns a simple moving average of its arguments so far |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4474 Location: Boulder, CO
|
Posted: Mon Jun 22, 2009 8:05 pm Post subject: Moving Average of last m entries |
|
|
Here is a more useful version of the Moving Average function, which averages the last m inputs, instead of all. With a non-empty second parameter the internal state can be cleared and the length of averaging set. | Code: | MsgBox % MovingAverage(5,3) ; 5, averaging length <- 3
MsgBox % MovingAverage(1) ; 3
MsgBox % MovingAverage(-3) ; 1
MsgBox % MovingAverage(8) ; 2
MsgBox % MovingAverage(7) ; 4
MovingAverage(x,len="") { ; for integers (faster)
Static
Static sum:=0, n:=0, m:=10 ; default averaging length = 10
If (len>"") ; non-blank 2nd parameter: set length, reset
sum := n := i := 0, m := len
If (n < m) ; until the buffer is not full
sum += x, n++ ; keep summing data
Else ; when buffer is full
sum += x-v%i% ; add new, subtract oldest
v%i% := x, i := mod(i+1,m) ; remember last m inputs, cycle insertion point
Return sum/n
} | At floating point numbers the rounding error can accumulate, so we should not use the “sum” variable, but before return we always sum all saved entries. AHK uses proper rounding and double precision, so we can usually work with the faster version. | Code: | MovingAverage(x,len="") { ; for floating point numbers
Static
Static n:=0, m:=10 ; default averaging length = 10
If (len>"") ; non-blank 2nd parameter: set length, reset
n := i := 0, m := len
n += n < m, sum := 0
v%i% := x, i := mod(i+1,m) ; remember last m inputs, cycle insertion point
Loop %n% ; recompute sum to avoid error accumulation
j := A_Index-1, sum += v%j%
Return sum/n
} |
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|