AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Challenge: translate rosettacode - Was promoting autohotkey
Goto page Previous  1, 2, 3 ... 6, 7, 8 ... 10, 11, 12  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> General Chat
View previous topic :: View next topic  
Author Message
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sat Jun 20, 2009 3:10 pm    Post subject: Cocktail Sort Reply with quote

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
}
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sat Jun 20, 2009 7:49 pm    Post subject: Complex Numbers Reply with quote

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")
}
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sun Jun 21, 2009 12:55 am    Post subject: Counting sort Reply with quote

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)
}
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sun Jun 21, 2009 1:17 am    Post subject: Formatted Numeric Output Reply with quote

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)
}
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sun Jun 21, 2009 1:39 am    Post subject: Forward difference Reply with quote

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
}
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sun Jun 21, 2009 1:48 am    Post subject: Functional Composition Reply with quote

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))
}
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sun Jun 21, 2009 2:15 am    Post subject: Gnome Sort Reply with quote

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
}
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sun Jun 21, 2009 2:25 am    Post subject: Greatest common divisor Reply with quote

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))
}
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sun Jun 21, 2009 2:45 am    Post subject: Insertion Sort Reply with quote

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
}
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sun Jun 21, 2009 2:55 am    Post subject: Merge Sort Reply with quote

Merge sort was already posted to this Forum.
Back to top
View user's profile Send private message
tinku99



Joined: 03 Aug 2007
Posts: 309
Location: Houston, TX

PostPosted: Sun Jun 21, 2009 9:11 am    Post subject: doubly linked lists Reply with quote

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%
}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
tinku99



Joined: 03 Aug 2007
Posts: 309
Location: Houston, TX

PostPosted: Sun Jun 21, 2009 9:14 am    Post subject: singly linked list Reply with quote

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"
  }
}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
n-l-i-d
Guest





PostPosted: Sun Jun 21, 2009 10:54 am    Post subject: Reply with quote

Wow, Laszlo is on a roll Cool
Back to top
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sun Jun 21, 2009 4:48 pm    Post subject: Pascal's Triangle Reply with quote

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
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4514
Location: Boulder, CO

PostPosted: Sun Jun 21, 2009 5:30 pm    Post subject: Quadratic Equation Reply with quote

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
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> General Chat All times are GMT
Goto page Previous  1, 2, 3 ... 6, 7, 8 ... 10, 11, 12  Next
Page 7 of 12

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group