AutoHotkey Community

It is currently May 26th, 2012, 6:54 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 417 posts ]  Go to page Previous  1 ... 4, 5, 6, 7, 8, 9, 10 ... 28  Next
Author Message
 Post subject: Cocktail Sort
PostPosted: June 20th, 2009, 3:10 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Cocktail Sort:
Code:
MsgBox % CocktailSort("")
MsgBox % CocktailSort("xxx")
MsgBox % CocktailSort("3,2,1")
MsgBox % CocktailSort("dog,000000,xx,cat,pile,abcde,1,cat,zz,xx,z")

CocktailSort(var) {                      ; SORT COMMA SEPARATED LIST
   StringSplit array, var, `,            ; make array
   i0 := 1, i1 := array0                 ; start, end

   Loop {                                ; break when sorted
     Changed =
     Loop % i1-- -i0 {                   ; last entry will be in place
       j := i0+A_Index, i := j-1
       If (array%j% < array%i%)          ; swap?
         t := array%i%, array%i% := array%j%, array%j% := t
        ,Changed = 1                     ; change has happened
     }
     IfEqual Changed,, Break

     Loop % i1-i0++ {                    ; first entry will be in place
       i := i1-A_Index, j := i+1
       If (array%j% < array%i%)          ; swap?
         t := array%i%, array%i% := array%j%, array%j% := t
        ,Changed = 1                     ; change has happened
     }
     IfEqual Changed,, Break
   }

   Loop % array0                         ; construct string from sorted array
     sorted .= "," . array%A_Index%
   Return SubStr(sorted,2)               ; drop leading comma
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Complex Numbers
PostPosted: June 20th, 2009, 7:49 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The Complex Numbers task asks for a minimal set of complex functions:
Code:
Cset(C,1,1)
MsgBox % Cstr(C)  ; 1 + i*1
Cneg(C,C)
MsgBox % Cstr(C)  ; -1 - i*1
Cadd(C,C,C)
MsgBox % Cstr(C)  ; -2 - i*2
Cinv(D,C)
MsgBox % Cstr(D)  ; -0.25 + 0.25*i
Cmul(C,C,D)
MsgBox % Cstr(C)  ; 1 + i*0

Cset(ByRef C, re, im) {
   VarSetCapacity(C,16)
   NumPut(re,C,0,"double")
   NumPut(im,C,8,"double")
}
Cre(ByRef C) {
   Return NumGet(C,0,"double")
}
Cim(ByRef C) {
   Return NumGet(C,8,"double")
}
Cstr(ByRef C) {
   Return Cre(C) ((i:=Cim(C))<0 ? " - i*" . -i : " + i*" . i)
}
Cadd(ByRef C, ByRef A, ByRef B) {
   VarSetCapacity(C,16)
   NumPut(Cre(A)+Cre(B),C,0,"double")
   NumPut(Cim(A)+Cim(B),C,8,"double")
}
Cmul(ByRef C, ByRef A, ByRef B) {
   VarSetCapacity(C,16)
   t := Cre(A)*Cim(B)+Cim(A)*Cre(B)
   NumPut(Cre(A)*Cre(B)-Cim(A)*Cim(B),C,0,"double")
   NumPut(t,C,8,"double") ; A or B can be C!
}
Cneg(ByRef C, ByRef A) {
   VarSetCapacity(C,16)
   NumPut(-Cre(A),C,0,"double")
   NumPut(-Cim(A),C,8,"double")
}
Cinv(ByRef C, ByRef A) {
   VarSetCapacity(C,16)
   d := Cre(A)**2 + Cim(A)**2
   NumPut( Cre(A)/d,C,0,"double")
   NumPut(-Cim(A)/d,C,8,"double")
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Counting sort
PostPosted: June 21st, 2009, 12:55 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Counting sort for comma separated lists:
Code:
MsgBox % CountingSort("-1,1,1,0,-1",-1,1)

CountingSort(ints,min,max) {
   Loop % max-min+1
      i := A_Index-1, a%i% := 0
   Loop Parse, ints, `, %A_Space%%A_Tab%
      i := A_LoopField-min, a%i%++
   Loop % max-min+1 {
      i := A_Index-1, v := i+min
      Loop % a%i%
         t .= "," v
   }
   Return SubStr(t,2)
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Formatted Numeric Output
PostPosted: June 21st, 2009, 1:17 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The Formatted Numeric Output task asks to express a number in decimal as a fixed-length string with leading zeros.
Code:
MsgBox % pad(7.25,7)  ; 0007.25
MsgBox % pad(-7.25,7) ; -007.25

pad(x,len) { ; pad with 0's from left to len chars
   IfLess x,0, Return "-" pad(SubStr(x,2),len-1)
   VarSetCapacity(p,len,Asc("0"))
   Return SubStr(p x,1-len)
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Forward difference
PostPosted: June 21st, 2009, 1:39 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The n-th order Forward difference:
Code:
MsgBox % diff("2,3,4,3",1)
MsgBox % diff("2,3,4,3",2)
MsgBox % diff("2,3,4,3",3)
MsgBox % diff("2,3,4,3",4)

diff(list,ord) { ; high order forward differences of a list
   Loop %ord% {
      L =
      Loop Parse, list, `, %A_Space%%A_Tab%
         If (A_Index=1)
            p := A_LoopField
         Else
            L .= "," A_LoopField-p, p := A_LoopField
      list := SubStr(L,2)
   }
   Return list
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Functional Composition
PostPosted: June 21st, 2009, 1:48 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The closest to Functional Composition we can get with AHK:
Code:
MsgBox % compose("sin","cos",1.5)

compose(f,g,x) { ; function composition
   Return %f%(%g%(x))
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Gnome Sort
PostPosted: June 21st, 2009, 2:15 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Gnome Sort:
Code:
MsgBox % GnomeSort("")
MsgBox % GnomeSort("xxx")
MsgBox % GnomeSort("3,2,1")
MsgBox % GnomeSort("dog,000000,xx,cat,pile,abcde,1,cat,zz,xx,z")

GnomeSort(var) {                         ; SORT COMMA SEPARATED LIST
   StringSplit a, var, `,                ; make array, size = a0
   i := 2, j := 3
   While i <= a0 {                       ; stop when sorted
      u := i-1
      If (a%u% < a%i%)                   ; search for pairs to swap
         i := j, j := j+1
      Else {                             ; swap
         t := a%u%, a%u% := a%i%, a%i% := t
         If (--i = 1)                    ; restart search
            i := j, j++
      }
   }
   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: Greatest common divisor
PostPosted: June 21st, 2009, 2:25 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The Euclidean Algorithm for the Greatest common divisor of two integers can be computed with a short, single line recursive function:
Code:
GCD(a,b) {
   Return b=0 ? Abs(a) : Gcd(b,mod(a,b))
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Insertion Sort
PostPosted: June 21st, 2009, 2:45 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Insertion Sort of comma separated lists:
Code:
MsgBox % InsertionSort("")
MsgBox % InsertionSort("xxx")
MsgBox % InsertionSort("3,2,1")
MsgBox % InsertionSort("dog,000000,xx,cat,pile,abcde,1,cat,zz,xx,z")

InsertionSort(var) {                     ; SORT COMMA SEPARATED LIST
   StringSplit a, var, `,                ; make array, size = a0
   Loop % a0-1 {
      i := A_Index+1, v := a%i%, j := i-1
      While j>0 and a%j%>v
         u := j+1, a%u% := a%j%, j--
      u := j+1, a%u% := v
   }
   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: Merge Sort
PostPosted: June 21st, 2009, 2:55 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Merge sort was already posted to this Forum.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: doubly linked lists
PostPosted: June 21st, 2009, 9:11 am 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
doubly linked lists
Code:
a_prev = 0
a = 1
a_next = b
b_prev = a
b = 2
b_next = 0
c = 4
d = 5
insert_head("c", "a")
insert_tail("e", "b")
insert_between("d", "a", "b")
ListVars
msgbox % "tail= " . tail := traverse("c")
msgbox % "head= " . head := traverse("d", "reverse")
return

insert_head(new, head)
{
  local temp
  %head%_prev := new
  %new%_prev := 0
  %new%_next := head
}

insert_tail(new, tail)
{
  local temp
  %prev%_next := new
  %new%_prev := prev
  %new%_next := 0
}

insert_between(new, prev, next)
{
  local temp
  %prev%_next := new
  %new%_prev := prev
  %new%_next := next
  %next%_prev := new
}

traverse(element, direction="forward")
{
if (direction = "forward")
direction = _next
if (direction = "reverse")
direction = _prev


  name := element
  MsgBox % element . "= " . %element%
  name := element . direction
  while, %name%
  {
  element := %name%
  msgbox % %name% . "= " . %element%
  oldname := name
  name := %name% . direction
  if (%oldname% == %name%)
   goto circular
  }
return (%oldname%)
circular:
msgbox % "error: list is circular: " . name . oldname . %name% . %oldname%
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: singly linked list
PostPosted: June 21st, 2009, 9:14 am 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
singly linked list
Code:
a = 1
a_next = b
b = 2
b_next = 0
c = 3
insert_after("c", "a")
traverse("a")
return

insert_after(new, old)
{
  local temp
  temp := %old%_next
  %old%_next := new
  %new%_next := temp
}

traverse(element)
{
  name := element
  MsgBox % element . "= " . %element%
  name := element . "_next"
  while, %name%
  {
  element := %name%
  msgbox % %name% . "= " . %element%
  name := %name% . "_next"
  }
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 21st, 2009, 10:54 am 
Wow, Laszlo is on a roll 8)


Report this post
Top
  
Reply with quote  
 Post subject: Pascal's Triangle
PostPosted: June 21st, 2009, 4:48 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Pascal's Triangle, shown nicely formatted in a GUI:
Code:
n := 8, p0 := "1"        ; 1+n rows of Pascal's triangle
Loop %n% {
   p := "p" A_Index, %p% := v := 1, q := "p" A_Index-1
   Loop Parse, %q%, %A_Space%
      If (A_Index > 1)
         %p% .= " " v+A_LoopField, v := A_LoopField
   %p% .= " 1"
}
                         ; Triangular Formatted output
VarSetCapacity(tabs,n,Asc("`t"))
t .= tabs "`t1"
Loop %n% {
   t .= "`n" SubStr(tabs,A_Index)
   Loop Parse, p%A_Index%, %A_Space%
      t .= A_LoopField "`t`t"
}
Gui Add, Text,, %t%      ; Show result in a GUI
Gui Show


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

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Numerically stable solution of Quadratic Equation:
Code:
MsgBox % quadratic(u,v, 1,-3,2) ", " u ", " v
MsgBox % quadratic(u,v, 1,3,2) ", " u ", " v
MsgBox % quadratic(u,v, -2,4,-2) ", " u ", " v
MsgBox % quadratic(u,v, 1,0,1) ", " u ", " v
SetFormat FloatFast, 0.15e
MsgBox % quadratic(u,v, 1,-1.0e8,1) ", " u ", " v

quadratic(ByRef x1, ByRef x2, a,b,c) { ; -> #real roots {x1,x2} of ax²+bx+c
   If (a = 0)
      Return -1 ; ERROR: not quadratic
   d := b*b - 4*a*c
   If (d < 0) {
      x1 := x2 := ""
      Return 0
   }
   If (d = 0) {
      x1 := x2 := -b/2/a
      Return 1
   }
   x1 := (-b - (b<0 ? -sqrt(d) : sqrt(d)))/2/a
   x2 := c/a/x1
   Return 2
}


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 ... 4, 5, 6, 7, 8, 9, 10 ... 28  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: tomoe_uehara and 11 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