Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Challenge: translate rosettacode - Was promoting autohotkey


  • Please log in to reply
55 replies to this topic
SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
All credits to PhiLho! Function ripped from his library
Rot-13, <!-- m -->http://rosettacode.org/wiki/Rot-13<!-- m -->
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
}


tinku99
  • Members
  • 560 posts
  • Last active: Nov 18 2013 02:43 AM
  • Joined: 03 Aug 2007
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

tinku99
  • Members
  • 560 posts
  • Last active: Nov 18 2013 02:43 AM
  • Joined: 03 Aug 2007
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.
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.

tinku99
  • Members
  • 560 posts
  • Last active: Nov 18 2013 02:43 AM
  • Joined: 03 Aug 2007
Rendezvous task is a nice demo of the syntax highlighting. with geshi.
My favorite feature: commands are linked to the autohotkey documentation.

daonlyfreez
  • Members
  • 995 posts
  • Last active: Jan 23 2013 08:16 AM
  • Joined: 16 Mar 2005
Rosetta Code mashup challenge

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: <!-- m -->http://www.autohotke... ... mashup.png<!-- m -->

Source at: <!-- m -->http://www.autohotke... ... mashup.ahk<!-- m -->

Maybe it helps/you like it



tinku99
  • Members
  • 560 posts
  • Last active: Nov 18 2013 02:43 AM
  • Joined: 03 Aug 2007

Rosetta Code mashup challenge

looks interesting. Will check it out.
What a coincidence, I just now, created a mashup also: rosetta - task search

daonlyfreez
  • Members
  • 995 posts
  • Last active: Jan 23 2013 08:16 AM
  • Joined: 16 Mar 2005
Ah yes, I saw your posting.

8)

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... :p

Screenshot directly

Posted Image

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Fibonacci Numbers These scripts were already posted to the Forum:
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.

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Here are a few short recursive functions for listing all the prime divisors of a number:
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)
}
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)
}
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)
}
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:
MsgBox % PDivs(1995937)
Loop
   MsgBox % "Prime divisors of " A_Index " = " PDivs(A_Index)


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
The above functions can be simplified for primality testing. E.g.
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

}


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
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.)
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
}