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 ... 5, 6, 7 ... 10, 11, 12  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> General Chat
View previous topic :: View next topic  
Author Message
n-l-i-d
Guest





PostPosted: Fri Jun 19, 2009 4:14 pm    Post subject: Reply with quote

@infogulch: added Wink
Back to top
n-l-i-d
Guest





PostPosted: Fri Jun 19, 2009 4:26 pm    Post subject: Reply with quote

If you want to contribute yourself, here is the MediaWiki code

Code:
=={{Header|AutoHotkey}}==
{{AutoHotkey case}}
Source: [http://www.autohotkey.com/forum/topic44657.html AutoHotkey forum] by yournick
<lang autohotkey>
Your code here
</lang>


Make sure you put it in the right alphabetical order in the page with the other languages.

HTH
Back to top
Laszlo



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

PostPosted: Fri Jun 19, 2009 4:44 pm    Post subject: Reply with quote

n-l-i-d wrote:
I added all of Laszlo's functions, apart from the MontePi, couldn't find that one... Confused
Monte Carlo Simulation
infogulch wrote:
what about Laszlo's MD5?
It could be posted there, too.
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 352

PostPosted: Fri Jun 19, 2009 5:03 pm    Post subject: Reply with quote

infogulch wrote:
what about the Tasks Not Implemented in AutoHotkey ?
Bottom of Unimplemented Page wrote:
And remember that this list is not entirely live; It's updated periodically by ImplSearchBot, currently every four hours.
Oh.
_________________
A great Beginner's Tutorial
Back to top
View user's profile Send private message
tinku99



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

PostPosted: Fri Jun 19, 2009 5:26 pm    Post subject: disclaimers Reply with quote

Quote:
Note: AutoHotkey is case-insensitive. Keywords are capitalized and variables lowercased for clarity and consistency with the documentation.

This disclaimer is unnecessary on the task pages.
It may be more appropriate on the main autohotkey page or its talk page: here or here

looks light syntax highlighting is back online.
I have also updated the geshi lexer: here, and passed it on to rosettacode. Hopefully they will update it soon.

46 more tasks till the 200 tasks / top 10 milestone !
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Laszlo



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

PostPosted: Fri Jun 19, 2009 5:57 pm    Post subject: Combinations Reply with quote

We can write a single line function to list all (n,t) combinations, and sort the result with another function.
Code:
MsgBox % Comb(1,1)
MsgBox % Comb(3,3)
MsgBox % Comb(3,2)
MsgBox % Comb(2,3)
MsgBox % Comb(5,3)

CR(n,t) { ; List all n choose t combinations of 1..n, recursively
   Return t<1 || t>n ? "" : n=1 ? 1 : CR(n-1,t) (n-1<t ? "" : "`n") RegExReplace(CR(n-1,t-1),"`am)$",t=1 ? n : " " n)
}

Comb(n,t) { ; List all n choose t combinations of 1..n, lexicographically
   c := CR(n,t)
   Sort c
   Return c
}

It is kind of cheating, so here is a traditional, iterative version (although combinations are listed in reverse order). I posted it earlier to the AHK Forum.
Code:
Comb(n,t) { ; Generate all n choose t combinations of 1..n, lexicographically
   IfLess n,%t%, Return
   Loop %t%
      c%A_Index% := A_Index
   i := t+1, c%i% := n+1

   Loop {
      Loop %t%
         i := t+1-A_Index, c .= c%i% " "
      c .= "`n"     ; combinations in new lines
      j := 1, i := 2
      Loop
         If (c%j%+1 = c%i%)
             c%j% := j, ++j, ++i
         Else Break
      If (j > t)
         Return c
      c%j% += 1
   }
}
Back to top
View user's profile Send private message
tinku99



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

PostPosted: Fri Jun 19, 2009 6:17 pm    Post subject: data structures: list functions Reply with quote

There are half a dozen tasks related to list manipulation on rosetta: data structures
If someone wants to take a hack at these, could use: Laszlo's list manipulation library

I started an ask for help thread for Doubly linked list, based on code by rulfzid: discussion.
related:
ahkdll: improvements to numget, numput
If I can build in memory management versions of numget, numput; it may allow for more efficient scripted implementations of compound data structures such as arrays and lists.


Last edited by tinku99 on Fri Jun 19, 2009 6:23 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Laszlo



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

PostPosted: Fri Jun 19, 2009 6:23 pm    Post subject: Common number base formatting Reply with quote

The Common number base formatting task asks about language support of output of numbers in different bases. Calls to the Windows standard runtime dll might qualify:
Code:
MsgBox % BC("FF",16,3) ; -> 100110 in base 3 = FF in hex = 256 in base 10

BC(NumStr,InputBase=8,OutputBase=10) {
  Static S = 12345678901234567890123456789012345678901234567890123456789012345
  DllCall("msvcrt\_i64toa","Int64",DllCall("msvcrt\_strtoui64","Str",NumStr,"Uint",0,"UInt",InputBase,"CDECLInt64"),"Str",S,"UInt",OutputBase,"CDECL")
  Return S
}


The same code can be posted to the common number base parsing task, too.
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Fri Jun 19, 2009 6:46 pm    Post subject: Number base conversion Reply with quote

For the Number base conversion task we could post shorter (single line) recursive functions:
Code:
MsgBox % ToBase(29,3)
MsgBox % ToBase(255,16)

MsgBox % FromBase("100",8)
MsgBox % FromBase("ff",16)

ToBase(n,b) { ; n >= 0, 1 < b <= 36
   Return (n < b ? "" : ToBase(n//b,b)) . ((d:=mod(n,b)) < 10 ? d : Chr(d+87))
}

FromBase(s,b) { ; convert base b number s=strings of 0..9,a..z, to AHK number
   Return (L:=StrLen(s))=0 ? "":(L>1 ? FromBase(SubStr(s,1,L-1),b)*b:0) + ((c:=Asc(SubStr(s,0)))>57 ? c-87:c-48)
}
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Fri Jun 19, 2009 8:07 pm    Post subject: Random Numbers Reply with quote

Normally distributed random numbers of given mean and standard deviation by Box-Muller method:
Code:
Loop 40
   R .= RandN(1,0.5) "`n"  ; mean = 1.0, standard deviation = 0.5
MsgBox %R%

RandN(m,s) { ; Normally distributed random numbers of mean = m, std.dev = s by Box-Muller method
   Static i, Y
   If (i := !i) { ; every other call
      Random U, 0, 1.0
      Random V, 0, 6.2831853071795862
      U := sqrt(-2*ln(U))*s
      Y := m + U*sin(V)
      Return m + U*cos(V)
   }
   Return Y
}
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 352

PostPosted: Fri Jun 19, 2009 8:13 pm    Post subject: Reply with quote

Column Aligner. With left, center, and right options.

Center was a pain in the butt. Mad

Rolling Eyes

Code:
lines =
(
|$|$|$|$|$|$|$|$|$|$|$|
Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
column$are$separated$by$at$least$one$space.
Further,$allow$for$each$word$in$a$column$to$be$either$left$
justified,$right$justified,$or$center$justified$within$its$column.
)


MsgBox % ColumnJustify(lines, "r")


ColumnJustify(lines, lcr = "l", del="$")
{
   Loop, Parse, lines, `n, `r
      Loop, Parse, A_LoopField, %del%
         c%A_Index% := c%A_Index% > StrLen(A_LoopField) ? c%A_Index% : StrLen(A_LoopField)
         , max := max > c%A_Index% ? max : c%A_Index%
   blank := Fill( " ", max )
   Loop, Parse, lines, `n, `r
   {
      Loop, Parse, A_LoopField, %del%
         out .= (A_Index = 1 ? "" : " ")
         . (lcr = "l"
            ? SubStr(A_LoopField blank, 1, c%A_Index%)
         : lcr = "r"
            ? SubStr(blank A_LoopField, -c%A_Index%+1)
         : SubStr(blank A_LoopField blank
         , (Ceil((max * 2 + StrLen(A_LoopField))/2) - Ceil(c%A_Index%/2) + 1)
         , c%A_Index%))
      out .= "`n"
   }
   return out
}


Fill(chr, len)
{
   static y
   if !y
      VarSetCapacity(x, 64), VarSetCapacity(x, 0), y := True
   return x, VarSetCapacity(x, len, Asc(chr))
}


Edit: Oops. Found an error, center isn't working right. Pls hold...
Edit Again: Fixed, sry bout that.

Edit: Here's another version that separates the different alignments into different loops. It's probably faster, and definately easier for people to read (e.g. on rosettacode) but it's longer.. >_>
Code:
lines =
(
|$|$|$|$|$|$|$|$|$|$|$|
Given$a$text$file$of$many$lines,$where$fields$within$a$line$
are$delineated$by$a$single$'dollar'$character,$write$a$program
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
column$are$separated$by$at$least$one$space.
Further,$allow$for$each$word$in$a$column$to$be$either$left$
justified,$right$justified,$or$center$justified$within$its$column.
)


MsgBox % ColumnJustify(lines, "l")


ColumnJustify(lines, lcr = "l", del="$")
{
   Loop, Parse, lines, `n, `r
      Loop, Parse, A_LoopField, %del%
      {
         If ((t := StrLen(A_LoopField)) > c%A_Index% )
            c%A_Index% :=  t
         If (t > max)
            max := t
      }
   blank := Fill( " ", max )
   If (lcr = "l") ;left-justify
      Loop, Parse, lines, `n, `r
         Loop, Parse, A_LoopField, %del%
            out .= (A_Index = 1 ? "`n" : " ") SubStr(A_LoopField blank, 1, c%A_Index%)
   Else If (lcr = "r") ;right-justify
      Loop, Parse, lines, `n, `r
         Loop, Parse, A_LoopField, %del%
            out .= (A_Index = 1 ? "`n" : " ") SubStr(blank A_LoopField, -c%A_Index%+1)
   Else If (lcr = "c") ;center-justify
      Loop, Parse, lines, `n, `r
         Loop, Parse, A_LoopField, %del%
            out .= (A_Index = 1 ? "`n" : " ") SubStr(blank A_LoopField blank
               , (Ceil((max * 2 + StrLen(A_LoopField))/2) - Ceil(c%A_Index%/2) + 1)
               , c%A_Index%)
   return SubStr(out, 2)
}


Fill(chr, len)
{
   static y
   if !y
      VarSetCapacity(x, 64), VarSetCapacity(x, 0), y := True
   return x, VarSetCapacity(x, len, Asc(chr))
}

_________________
A great Beginner's Tutorial


Last edited by infogulch on Tue Feb 02, 2010 7:18 pm; edited 1 time in total
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Fri Jun 19, 2009 10:00 pm    Post subject: Power Set Reply with quote

The Power Set task can also be solved with a single line recursive function, although the result will not be sorted. We touch up the data before and after for a nicer look.
Code:
MsgBox % PowerSet("")
MsgBox % PowerSet("-")
MsgBox % PowerSet("a bb")
MsgBox % PowerSet("4,1,2,3,4,5",",")

PS(s,d=" ") { ; The power set of a list s, separated by d (space), with extra delimiters
   Return s="" ? "{}" : (p:=InStr(s,d)) ? (w:=PS(SubStr(s,p+1),d)) "`n" RegExReplace(w,"}",d SubStr(s,1,p-1) "}") : "{}`n{" s "}"
}
PowerSet(s,d=" ") { ; The power set of a list s, separated by d (space)
   Sort s, RUD%d%   ; Sort input for nicer output, remove duplicates
   P := PS(s,d)
   StringReplace P, P, {%d%, {, All
   Return P
}
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Fri Jun 19, 2009 10:37 pm    Post subject: Probabilistic Choice Reply with quote

Here is a straightforward implementation of the Probabilistic Choice task:
Code:
s1 := "aleph",   p1 := 1/5.0                       ; Input
s2 := "beth",    p2 := 1/6.0
s3 := "gimel",   p3 := 1/7.0
s4 := "daleth",  p4 := 1/8.0
s5 := "he",      p5 := 1/9.0
s6 := "waw",     p6 := 1/10.0
s7 := "zayin",   p7 := 1/11.0
s8 := "heth",    p8 := 1-p1-p2-p3-p4-p5-p6-p7
n := 8, r0 := 0, r%n% := 1                         ; auxiliary data

Loop % n-1
   i := A_Index-1, r%A_Index% := r%i% + p%A_Index% ; cummulative distribution

Loop 1000000 {
   Random R, 0, 1.0
   Loop %n%                                        ; linear search
      If (R < r%A_Index%) {
          c%A_Index%++
          Break
      }
}
                                                   ; Output
Loop %n%
   t .= s%A_Index% "`t" p%A_Index% "`t" c%A_Index%*1.0e-6 "`n"
Msgbox %t%

The result:
Code:
---------------------------
aleph  0.200000   0.199960
beth   0.166667   0.166146
gimel  0.142857   0.142624
daleth 0.125000   0.124924
he     0.111111   0.111226
waw    0.100000   0.100434
zayin  0.090909   0.091344
heth   0.063456   0.063342
---------------------------
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Sat Jun 20, 2009 2:54 am    Post subject: Zig Zag Reply with quote

The script for the Zig Zag task was modeled after the Common Lisp code, with formatted output:
Code:
n = 5                           ; size
v := x := y := 1                ; initial values
Loop % n*n {                    ; for every array element
   a_%x%_%y% := v++             ; assign the next index
   If ((x+y)&1)                 ; odd diagonal
      If (x < n)                ; while inside the square
         y -= y>1, x++          ; move right-up
      Else y++                  ; on the edge increment y, but not x: to even diagonal
   Else                         ; even diagonal
      If (y < n)                ; while inside the square
         x -= x>1, y++          ; move left-down
      Else x++                  ; on the edge increment x, but not y: to odd diagonal
}

Loop %n% {                      ; generate printout
   x := A_Index                 ; for each row
   Loop %n%                     ; and for each column
      t .= a_%x%_%A_Index% "`t" ; attach stored index
   t .= "`n"                    ; row is complete
}
MsgBox %t%                      ; show output


Edit 20090620: minor simplifications
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Sat Jun 20, 2009 1:33 pm    Post subject: Anagrams Reply with quote

Anagrams:
Code:
MsgBox % anagrams("able")

anagrams(word) {
   Static dict
   IfEqual dict,, FileRead dict, unixdict.txt ; file in the script directory
   w := sort(word)
   Loop Parse, dict, `n, `r
      If (w = sort(A_LoopField))
         t .= A_LoopField "`n"
   Return t
}

sort(word) {
   a := RegExReplace(word,".","$0`n")
   Sort a
   Return a
}
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 ... 5, 6, 7 ... 10, 11, 12  Next
Page 6 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