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, 4, 5 ... 26, 27, 28  Next
 
Reply to topic    AutoHotkey Community Forum Index -> General Chat
View previous topic :: View next topic  
Author Message
SoLong&Thx4AllTheFish



Joined: 27 May 2007
Posts: 4999

PostPosted: Tue Jun 02, 2009 8:14 pm    Post subject: Reply with quote

All credits to PhiLho! Function ripped from his library
Rot-13, http://rosettacode.org/wiki/Rot-13
Code:
Str0=Hello, This is a sample text with 1 2 3 or other digits!@#$^&*()-_=
Str1 := Rot13(Str0)
Str2 := Rot13(Str1)
MsgBox % Str0 "`n" Str1 "`n" Str2

Rot13(string) ; ripped from http://www.autohotkey.net/~PhiLho/StringMod.ahk
{
   Loop Parse, string
   {
      char := Asc(A_LoopField)
      ; o is 'A' code if it is an uppercase letter, and 'a' code if it is a lowercase letter
      o := Asc("A") * (Asc("A") <= char && char <= Asc("Z")) + Asc("a") * (Asc("a") <= char && char <= Asc("z"))
      If (o > 0)
      {
         ; Set between 0 and 25, add rotation factor, modulus alphabet size
         char := Mod(char - o + 13, 26)
         ; Transform back to char, upper or lower
         char := Chr(char + o)
      }
      Else
      {
         ; Non alphabetic, unchanged
         char := A_LoopField
      }
      rStr := rStr char
   }
   Return rStr
}

_________________
AHK Wiki FAQ
TF : Text files & strings lib, TF Forum
Back to top
View user's profile Send private message
tinku99



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

PostPosted: Wed Jun 03, 2009 3:13 pm    Post subject: autohotkey lexer for rosettacode Reply with quote

I wrote a lexer for pygments, so ahk would be recognized as a language on github.
I just realized we need one for rosettacode also in GeShi which incidentally will work for phpbb (this forum).
and for google code, and yet another one: gnu src-highlite
Back to top
View user's profile Send private message Send e-mail Visit poster's website
tinku99



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

PostPosted: Thu Jun 11, 2009 4:19 am    Post subject: ppm functions Reply with quote

There are several tasks that require operating on an image.
I wrote some basic ppm functions.
Perhaps we can implement some of the image manipulation tasks.
I am having a little trouble initializing the image with zeros...
I think some more binary zero support in autohotkey will be usefull.
Code:

ppm := ppm_new(50, 50, 255)       ; 0 pixels are not well supported
ppm_fill(ppm, 89, 84, 85)       ; because AutoHotkey does not support
msgbox % getPixel(ppm, 1, 1)    ; binary zeroes well
setPixel(90, 90, 90, ppm, 1, 1)
msgbox % getPixel(ppm, 1, 1)
msgbox % ppm
return


ppm_read(file)
{
  fileread, ppm, % file
  return ppm
}

ppm_width(ppm)
{
 regexmatch(ppm, "\R(\d+)\s(\d+)", dim)
 return dim1
}
ppm_height(ppm)
{
 regexmatch(ppm, "\R(\d+)\s(\d+)", dim)
    return dim2
}

ppm_colors(ppm)
{
regexmatch(ppm, "\R(\d+)\D*\R", colors)  ; \R stands for any
return colors1
}

ppm_data(ppm)
{
pos :=  regexmatch(ppm, "\R(\d+)\D*\R", colors)  ; \R stands for any newline
stringtrimleft, data, ppm, pos + strlen(colors1)
return data
}
ppm_header(ppm)
{
pos :=  regexmatch(ppm, "\R(\d+)\D*\R", colors)  ; \R stands for any newline
stringleft, header, ppm, pos + strlen(colors1)
return header
}

ppm_fill(ByRef ppm, r, g, b)
{
width := ppm_width(ppm)   
height := ppm_height(ppm)
header := ppm_header(ppm)
headerLength := strlen(header) + 1
varsetcapacity(data, width * height, 0)
loop, % (width * height)
{
if r
numput(r, data, (A_Index - 1) * 3, "uchar")
if g
numput(g, data, (A_Index - 1) * 3 + 1, "uchar")
if b
numput(b, data, (A_Index - 1) * 3 + 2, "uchar")
}
VarCopy(&ppm + headerLength, &data, width * height)

}

ppm_new(width, height, colors)
{
  header = P6`n%width% %height%`n%colors%`n
  headerLength := strlen(header)
  varsetcapacity(ppm, width * height + headerLength, 1) ; a zero here in 3rd parameter causes problems.
  VarCopy(&ppm, &header, headerLength)
  return ppm
}


VarCopy(Target, Source, Length) {
    DllCall("RtlMoveMemory","uInt",Target,"uInt",Source,"uInt",Length)
}



getPixel(ppm, x, y)
{
data := ppm_data(ppm)
r := numget(data, x * y, "uchar")
g := numget(data, x * y + 1, "uchar")
b := numget(data, x * y + 2, "uchar")
pixel = %r%`n%g%`n%b%
return pixel
}

setPixel(r, g, b, ByRef ppm, x, y)
{
  header := ppm_header(ppm)
L := strlen(header)
numput(r, ppm, x * y + L, "uchar")
numput(g, ppm, x * y + L + 1, "uchar")
numput(b, ppm, x * y + L + 2, "uchar")
}

By the way, AutoHotkey is now ranked 34 in the most popular languages on rosettacode, bypassing javascript. 90 tasks are implemented. half way to top 10.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
tinku99



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

PostPosted: Mon Jun 15, 2009 5:12 am    Post subject: rendezvous task completed, ahk lexer in service Reply with quote

Rendezvous task is a nice demo of the syntax highlighting. with geshi.
My favorite feature: commands are linked to the autohotkey documentation.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
daonlyfreez



Joined: 16 Mar 2005
Posts: 949
Location: Berlin

PostPosted: Mon Jun 15, 2009 5:32 pm    Post subject: Reply with quote

Rosetta Code mashup challenge

Quote:
I’m not sure what it is that you are looking for, but I created a quick-and-dirty little application playing with your data, created in AutoHotkey:

Screenshot at: http://www.autohotkey.net/~daonlyfreez/scripting/rsmashup.png

Source at: http://www.autohotkey.net/~daonlyfreez/scripting/rsmashup.ahk

Maybe it helps/you like it

_________________
mirror 1mirror 2mirror 3ahk4.me • PM or
Back to top
View user's profile Send private message
tinku99



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

PostPosted: Mon Jun 15, 2009 5:56 pm    Post subject: rosetta mashup Reply with quote

daonlyfreez wrote:
Rosetta Code mashup challenge

looks interesting. Will check it out.
What a coincidence, I just now, created a mashup also: rosetta - task search
Back to top
View user's profile Send private message Send e-mail Visit poster's website
daonlyfreez



Joined: 16 Mar 2005
Posts: 949
Location: Berlin

PostPosted: Mon Jun 15, 2009 6:33 pm    Post subject: Reply with quote

Ah yes, I saw your posting.

Cool

It would have helped if they had stated more specifically what kind of mashup they wanted. I hope they don't mean something like Yahoo Pipes... Razz

Screenshot directly


_________________
mirror 1mirror 2mirror 3ahk4.me • PM or
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Wed Jun 17, 2009 6:35 pm    Post subject: Fibonacci Numbers Reply with quote

Fibonacci Numbers These scripts were already posted to the Forum:
Code:
FibR(n) {       ; n-th Fibonacci number (n>=0, recursive with static array Fibo)
   Static
   Return n<2 ? n : Fibo%n% ? Fibo%n% : Fibo%n% := FibR(n-1)+FibR(n-2)
}

Fib(n) {        ; n-th Fibonacci number (n < 0 OK, iterative)
   a := 0, b := 1
   Loop % abs(n)-1
      c := b, b += a, a := c
   Return n=0 ? 0 : n>0 || n&1 ? b : -b
}
Important note: the recursive version would be very slow without a global or static array. The iterative version handles also negative arguments properly.
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Wed Jun 17, 2009 9:18 pm    Post subject: prime divisors Reply with quote

Here are a few short recursive functions for listing all the prime divisors of a number:
Code:
PDivs(n,k=2) { ; short code for small numbers
   Return Mod(n,k) ? (k < n ? PDivs(n,k+1+(k>2)) : "") : k " " PDivs(n//k,k)
}

Code:
PDivs(n,k=2) { ; faster for larger numbers: stop search at sqrt(n)
   Return n < 2 ? "" : Mod(n,k) ? (k*k < n ? PDivs(n,k+1+(k>2)) : n) : k " " PDivs(n//k,k)
}

Code:
PDivs(n,k=2) { ; using exact limit: 1 less iteration
   d := k+1+(k>2)
   Return n < 2 ? "" : Mod(n,k) ? (d*d <= n ? PDivs(n,d) : n) : k " " PDivs(n//k,k)
}

Code:
PDivs(n,k=2) { ; still faster by selecting trial divisors not multiple of 2,3,5
   d := k+(k<7 ? 1+(k>2) : SubStr("6-----4---2-4---2-4---6-----2",Mod(k,30),1))
   Return n < 2 ? "" : Mod(n,k) ? (d*d <= n ? PDivs(n,d) : n) : k " " PDivs(n//k,k)
}

Tests:
Code:
MsgBox % PDivs(1995937)
Loop
   MsgBox % "Prime divisors of " A_Index " = " PDivs(A_Index)


Last edited by Laszlo on Wed Jun 17, 2009 11:25 pm; edited 1 time in total
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Wed Jun 17, 2009 10:42 pm    Post subject: primality test Reply with quote

The above functions can be simplified for primality testing. E.g.
Code:
MsgBox % IsPrime(1995937)
Loop
   MsgBox % A_Index-3 . " is " . (IsPrime(A_Index-3) ? "" : "not ") . "prime."

IsPrime(n,k=2) { ; testing primality with trial divisors not multiple of 2,3,5, up to sqrt(n)
   d := k+(k<7 ? 1+(k>2) : SubStr("6-----4---2-4---2-4---6-----2",Mod(k,30),1))
   Return n < 3 ? n>1 : Mod(n,k) ? (d*d <= n ? IsPrime(n,d) : 1) : 0
}
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Wed Jun 17, 2009 11:27 pm    Post subject: Sieve of Eratosthenes Reply with quote

This function computing the Sieve of Eratosthenes returns a long string of 0|1 characters. “1” at position k means that k is prime, “0” means that k is composite. (This time there is no recursion.)
Code:
MsgBox % "12345678901234567890`n" Sieve(20)

Sieve(n) { ; Sieve of Eratosthenes => string of 0|1 chars, 1 at position k: k is prime
   Static zero := 48, one := 49 ; Asc("0"), Asc("1")
   VarSetCapacity(S,n,one)
   NumPut(zero,S,0,"char")
   i := 2
   Loop % sqrt(n)-1 {
      If (NumGet(S,i-1,"char") = one)
         Loop % n//i
            If (A_Index > 1)
               NumPut(zero,S,A_Index*i-1,"char")
      i += 1+(i>2)
   }
   Return S
}
Back to top
View user's profile Send private message
Laszlo



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

PostPosted: Thu Jun 18, 2009 2:49 am    Post subject: incomplete Gamma function Reply with quote

Here is the upper incomplete Gamma function. Omitting or setting the second parameter to 0 we get the (complete) Gamma function. The code is based on: "Computation of Special Functions" Zhang and Jin, John Wiley and Sons, 1996
Code:
SetFormat FloatFast, 0.9e

Loop 10
   MsgBox % GAMMA(A_Index/3) "`n" GAMMA(A_Index*10)

GAMMA(a,x=0) {  ; upper incomplete gamma: Integral(t**(a-1)*e**-t, t = x..inf)
   If (a > 171 || x < 0)
      Return 2.e308   ; overflow

   xam := x > 0 ? -x+a*ln(x) : 0
   If (xam > 700)
      Return 2.e308   ; overflow

   If (x > 1+a) {     ; no need for gamma(a)
      t0 := 0, k := 60
      Loop 60
          t0 := (k-a)/(1+k/(x+t0)), --k
      Return exp(xam) / (x+t0)
   }

   r := 1, ga := 1.0  ; compute ga = gamma(a) ...
   If (a = round(a))  ; if integer: factorial
      If (a > 0)
         Loop % a-1
            ga *= A_Index
      Else            ; negative integer
         ga := 1.7976931348623157e+308 ; Dmax
   Else {             ; not integer
      If (abs(a) > 1) {
         z := abs(a)
         m := floor(z)
         Loop %m%
             r *= (z-A_Index)
         z -= m
      }
      Else
         z := a

      gr := (((((((((((((((((((((((       0.14e-14
          *z - 0.54e-14)             *z - 0.206e-13)          *z + 0.51e-12)
          *z - 0.36968e-11)          *z + 0.77823e-11)        *z + 0.1043427e-9)
          *z - 0.11812746e-8)        *z + 0.50020075e-8)      *z + 0.6116095e-8)
          *z - 0.2056338417e-6)      *z + 0.1133027232e-5)    *z - 0.12504934821e-5)
          *z - 0.201348547807e-4)    *z + 0.1280502823882e-3) *z - 0.2152416741149e-3)
          *z - 0.11651675918591e-2)  *z + 0.7218943246663e-2) *z - 0.9621971527877e-2)
          *z - 0.421977345555443e-1) *z + 0.1665386113822915) *z - 0.420026350340952e-1)
          *z - 0.6558780715202538)   *z + 0.5772156649015329) *z + 1

      ga := 1.0/(gr*z) * r
      If (a < -1)
         ga := -3.1415926535897931/(a*ga*sin(3.1415926535897931*a))
   }

   If (x = 0)         ; complete gamma requested
      Return ga

   s := 1/a           ; here x <= 1+a
   r := s
   Loop 60 {
      r *= x/(a+A_Index)
      s += r
      If (abs(r/s) < 1.e-15)
         break
   }
   Return ga - exp(xam)*s
}

The 10 results shown:
Code:
2.678938535e+000  1.354117939e+000  1.0               8.929795115e-001  9.027452930e-001
3.628800000e+005  1.216451004e+017  8.841761994e+030  2.039788208e+046  6.082818640e+062

1.000000000e+000  1.190639348e+000  1.504575489e+000  2.000000000e+000  2.778158479e+000
1.386831185e+080  1.711224524e+098  8.946182131e+116  1.650795516e+136  9.332621544e+155
Back to top
View user's profile Send private message
tinku99



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

PostPosted: Thu Jun 18, 2009 5:33 am    Post subject: code contributions Reply with quote

Please go ahead and post to rosettacode directly at the same time as posting here.
It is a wiki, can always be edited if needed later, if people submit suggestions here.
I don't know what to do about code that is already on the forum.
I do not feel comfortable posting other people's code.
Especially because it is kind of awkward to give credit on a wiki, as traditionally contributions are without too much expectations in terms of credit.

Regardless, We are rapidly approaching the top 10 mark, now at 18.
147 tasks implemented, 54 more tasks to go to pass Haskell for the 10th spot.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Laszlo



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

PostPosted: Thu Jun 18, 2009 1:40 pm    Post subject: Reply with quote

I was hoping that the scripts get tested and we get feedback before posting them to rosettacode (I have no time for thorough tests). There are too many buggy functions there already.
Back to top
View user's profile Send private message
tinku99



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

PostPosted: Thu Jun 18, 2009 3:13 pm    Post subject: testing Reply with quote

Laszlo wrote:
I was hoping that the scripts get tested and we get feedback before posting them to rosettacode (I have no time for thorough tests). There are too many buggy functions there already.

"thorough" testing is not indicated for these examples. If the functions work on your samples, and you have not deliberately cheated, its sufficient.

If you see buggy examples, particularly AutoHotkey examples, please mark them as incomplete: template with
Code:
{{incorrect|lang}} or {{incorrect|lang|Explanation of the problem.}}, where lang is the programming language used.

And I will attempt to fix them myself.

Feedback:
1. Fibonacchi sequence: Your implementation is definitely better than the one i already posted on rosettacode a while ago. I like using assume static as a memoizing tool. You could add a comment to explain that:
Code:
static ; do not through away computed numbers for later reuse
2. prime numbers:
Code:
SubStr("6-----4---2-4---2-4---6-----2",Mod(k,30)
I am having trouble following this... For readability, you might either add explanatory comments or just submit the simpler examples.

Thanks for doing these.

On a related note: I did matrix transpose:
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> General Chat All times are GMT
Goto page Previous  1, 2, 3, 4, 5 ... 26, 27, 28  Next
Page 4 of 28

 
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