AutoHotkey Community

It is currently May 26th, 2012, 8:49 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 417 posts ]  Go to page Previous  1 ... 5, 6, 7, 8, 9, 10, 11 ... 28  Next
Author Message
 Post subject: Permutation Sort
PostPosted: June 21st, 2009, 8:54 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Roots of Unity
PostPosted: June 21st, 2009, 9:09 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject: SEDOL
PostPosted: June 21st, 2009, 9:30 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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)
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Sequence of Non-squares
PostPosted: June 21st, 2009, 9:42 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Moving Average
PostPosted: June 21st, 2009, 9:54 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Standard Deviation
PostPosted: June 21st, 2009, 10:17 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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)
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Selection Sort
PostPosted: June 21st, 2009, 10:38 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Miller-Rabin test
PostPosted: June 22nd, 2009, 12:18 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Spiral
PostPosted: June 22nd, 2009, 12:57 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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 
---------------------------


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 22nd, 2009, 3:24 pm 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
http://www.rosettacode.org/wiki/Determine_if_Only_One_Instance_is_Running

I wonder if #SingleInstance would count for this.

Ask:
Code:
#SingleInstance
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.

_________________
Scripts - License


Report this post
Top
 Profile  
Reply with quote  
 Post subject: dllcall with cpp example
PostPosted: June 22nd, 2009, 3:31 pm 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
I wonder if something like this would work:
Code:
dllcall("CreateMutex", "int", 0, "int", 1, "str", A_ScriptName)
Msgbox % ErrorLevel
msgbox % result := dllcall("GetLastError")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 22nd, 2009, 5:59 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
infogulch wrote:
http://www.rosettacode.org/wiki/Determine_if_Only_One_Instance_is_Running
I wonder if #SingleInstance would count for this.
The task sounds like we have to tell for any application if it has multiple instances, not only AHK scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: Moving Average
PostPosted: June 22nd, 2009, 8:04 pm 
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.


Report this post
Top
  
Reply with quote  
 Post subject: Re: Moving Average
PostPosted: June 22nd, 2009, 8:11 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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


Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 22nd, 2009, 9:05 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 417 posts ]  Go to page Previous  1 ... 5, 6, 7, 8, 9, 10, 11 ... 28  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group