AutoHotkey Community

It is currently May 26th, 2012, 7:11 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 417 posts ]  Go to page Previous  1, 2, 3, 4, 5 ... 28  Next
Author Message
 Post subject:
PostPosted: May 29th, 2009, 9:49 pm 
Offline

Joined: August 24th, 2005, 5:29 pm
Posts: 549
Location: Berlin / Germany
Happy Number:
Code:
HowMutchHappyNumbersDoYouWant := 8
IsHappyNumber(I) {
   N := 0
   Loop {
      Loop, Parse, I
         N += A_LoopField ** 2
      If (N = 1)
         Return True
      If (A%N%)
         Return False
      A%N% := True, I := N, N := 0
   }
}
C := 0, HN := ""
While (C < HowMutchHappyNumbersDoYouWant) {
   If IsHappyNumber(A_Index)
      C++, HN .= A_Index . "|"
}
R := (HowMutchHappyNumbersDoYouWant > 30 ? 30 : HowMutchHappyNumbersDoYouWant)
Gui, Add, ListBox, w400 r%R%, %HN%
Gui, Show, , First %HowMutchHappyNumbersDoYouWant% Happy Numbers
Return
GuiClose:
ExitApp

_________________
nick :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Knuth shuffle
PostPosted: May 29th, 2009, 10:13 pm 
Offline

Joined: November 27th, 2008, 9:44 am
Posts: 62
Code:
; http://rosettacode.org/wiki/Knuth_shuffle
; http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

list1 := "1,2,3,4,5,6,7,8,9,10"
list2 := "alpha,beta,gamma,delta,epsilon,zeta,eta,theta,iota,kappa"

Loop, 5
   shuffletest .= FisherYatesShuffle(list1) "`n"
Loop, 5
   shuffletest .= FisherYatesShuffle(list2) "`n"
   
msgbox % shuffletest


FisherYatesShuffle(l)
{
   StringSplit, l, l, `,, %A_Space%%A_Tab%
   n := l0
   While (n > 1)
   {
      Random, k, 1, l0
      temp := l%n%
      l%n% := l%k%
      l%k% := temp
      n--      
   }
   Loop, % l0
      randomlist .= l%A_Index% ","
   StringTrimRight, result, result, 1
   return result
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Greates common divisor
PostPosted: May 30th, 2009, 12:07 am 
Offline

Joined: November 27th, 2008, 9:44 am
Posts: 62
Code:
; http://rosettacode.org/wiki/Greatest_common_divisor

GCD_iterative(a, b)
{
   While (b)
   {
      t := b
      b := Mod(a, b)
      a := t
   }
   return a
}

GCD_recursive(a,b)
{
   return b ? GCD_recursive(b, Mod(a,b)) : a
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2009, 2:33 am 
Offline
User avatar

Joined: November 2nd, 2008, 4:23 pm
Posts: 2906
Location: 127.0.0.1
Are we also trying to complete the Tasks not implemented in AutoHotkey section?

_________________
aboutscriptappsscripts
Any code ⇈ above ⇈ requires AutoHotkey_L to run


Report this post
Top
 Profile  
Reply with quote  
 Post subject: status update
PostPosted: May 30th, 2009, 3:09 am 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
Frankie wrote:
Are we also trying to complete the Tasks not implemented in AutoHotkey section?

AutoHotkey is already upto number 58 in the most linked categories list. I would like to see it in the top 10 atleast.
Besides translating tasks, we can also add new tasks. See gui automation.
For example: Window_management, Keyboard_macros, Get_Pixel_Information
If you have contributed translations, edit your user page with something like:
Code:
{{mylangbegin}}
{{mylang|AutoHotkey| proficiency level here }}
{{mylangend}}
so you show up in the AutoHotkey_User list


Last edited by tinku99 on May 30th, 2009, 5:00 am, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2009, 3:31 am 
Offline

Joined: June 26th, 2008, 3:58 am
Posts: 56
I did the palindrome task
http://rosettacode.org/wiki/Palindrome#AutoHotkey
Code:
isPalindrome(str) {
   return StrLen(str) < 2 ? true : SubStr(str, 1, 1) = SubStr(str, 0, 1) ? %A_ThisFunc%(SubStr(str, 2, -1)) : false
   }



Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2009, 3:49 am 
Offline
User avatar

Joined: November 2nd, 2008, 4:23 pm
Posts: 2906
Location: 127.0.0.1
IMO its better to write the if-else statements out for readability purposes.

_________________
aboutscriptappsscripts
Any code ⇈ above ⇈ requires AutoHotkey_L to run


Report this post
Top
 Profile  
Reply with quote  
 Post subject: readability
PostPosted: May 30th, 2009, 4:57 am 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
also, very long lines start to look like complex regular expressions to me even if they are in english. Shorter lines with indentation, much more readable.
Ofcourse I am no expert on programming etiquette.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Doubly linked list
PostPosted: May 31st, 2009, 1:16 pm 
Offline

Joined: November 27th, 2008, 9:44 am
Posts: 62
This code is moderately tested. I'd appreciate it if anybody would take a look at it/poke at it before i put it up on the rosetta code list.

With some inspiration and ideas from animeaime's Class library, I made this.

I'd certainly love any suggestions to make it better.

Code:
; http://rosettacode.org/wiki/Doubly-Linked_List

;======================================+
;     DOUBLY LINKED LIST FUNCTIONS     |
;================================================================================

; Create new doubly linked list
dll_new()
{
   dll := HeapAlloc(8)
   NumPut(dll_node(""), dll+0, 0)
   NumPut(dll_node(""), dll+0, 4)
   dll_node_setnext(dll_head(dll), dll_tail(dll))
   dll_node_setprev(dll_tail(dll), dll_head(dll))
   return dll   
}

; Return the head node of the list
dll_head(dll)
{
   return NumGet(dll+0,0)
}

; Return the tail node of the list
dll_tail(dll)
{
   return NumGet(dll+4,0)
}

; Insert node after prevnode
dll_insertafter(node, prevnode)
{
   dll_node_setnext(node, dll_node_getnext(prevnode))
   dll_node_setprev(node, prevnode)
   dll_node_setprev(dll_node_getnext(prevnode), node)
   dll_node_setnext(prevnode, node)   
}

; Insert node before nextnode
dll_insertbefore(node, nextnode)
{
   dll_node_setprev(node, dll_node_getprev(nextnode))
   dll_node_setnext(node, nextnode)
   dll_node_setnext(dll_node_getprev(nextnode), node)
   dll_node_setprev(nextnode, node)
}

; Insert node at the beginning of dll
dll_insertfirst(dll, node)
{
   dll_insertafter(node, dll_head(dll))
}

; Insert node at the end of dll
dll_insertlast(dll, node)
{
   dll_insertbefore(node, dll_tail(dll))
}

; Remove node from dll
dll_remove(node)
{
   prevnode := dll_node_getprev(node)
   nextnode := dll_node_getnext(node)
   dll_node_setnext(prevnode, nextnode)
   dll_node_setprev(nextnode, prevnode)
   HeapFree(node)
}

; Return a comma-delimited list of all values in dll
dll_dump(dll)
{
   node := dll_head(dll)
   While ((node := dll_node_getnext(node)) != dll_tail(dll))
      dll_dump .= dll_node_getdata(node) ","
   StringTrimRight, dll_dump, dll_dump, 1
   return dll_dump   
}


;========================+
;     NODE FUNCTIONS     |
;================================================================================

; Create and return a node
dll_node(data, prev=0, next=0)
{
   ; allocate memory for the node
   data .= A_Space ; append space so numbers will be treated as strings
   node := HeapAlloc(8 + StrLen(data))
   
   ; fill the node
   NumPut(prev, node+0, 0)
   NumPut(next, node+0, 4)
   lstrcpy(node+8, data)
   
   return node
}

; Return the previous node
dll_node_getprev(node)
{
   return NumGet(node+0, 0)
}

; Return the next node
dll_node_getnext(node)
{
   return NumGet(node+0, 4)
}

; Return the node data
dll_node_getdata(node)
{
   s := stratadr(node+8)
   StringTrimRight, s, s, 1 ; remove space we added before
   return s
}

; Set the previous node
dll_node_setprev(node, newval)
{
   NumPut(newval, node+0, 0)
}

; Set the next node
dll_node_setnext(node, newval)
{
   NumPut(newval, node+0, 4)
}


;==========================+
;     HELPER FUNCTIONS     |
;================================================================================

; Allocate <size> bytes in the default process heap and return the address
HeapAlloc(size)
{
    static HeapAlloc
   HeapAlloc := HeapAlloc ? HeapAlloc : DllCall("GetProcAddress", uint, DllCall("GetModuleHandle", str, "Kernel32"), str, "HeapAlloc")
    return DllCall(HeapAlloc, uint, hProcessHeap(), uint, 0x8, uint, size)
}

; Free the memory in the default process heap starting at <adr>
HeapFree(adr)
{
   static HeapFree
   HeapFree := HeapFree ? HeapFree : DllCall("GetProcAddress", uint, DllCall("GetModuleHandle", str, "Kernel32"), str, "HeapFree")
    return DllCall(HeapFree, uint, hProcessHeap(), uint, 0, uint, adr)
}

; Return a handle to the default process heap
hProcessHeap()
{
   static hProcessHeap
   return hProcessHeap ? hProcessHeap : hProcessHeap := DllCall("GetProcessHeap")   
}

; Copy string <s> to the block of memory starting at address <dest>
lstrcpy(dest, s)
{
   static lstrcpy   
   lstrcpy := lstrcpy ? lstrcpy : DllCall("GetProcAddress", uint, DllCall("GetModuleHandle", str, "Kernel32"), str, "lstrcpy")
   return DllCall(lstrcpy, uint, dest, str, s)
}

; Return the string starting at address <adr>
; stratadr MulDiv trick by Laszlo of the AHK forum:
; http://www.autohotkey.com/forum/viewtopic.php?t=20349&postdays=0&postorder=asc&start=11
stratadr(adr)
{
   static Muldiv
   MulDiv := MulDiv ? MulDiv : DllCall("GetProcAddress", uint, DllCall("GetModuleHandle", str, "Kernel32"), str, "MulDiv")
   Return DllCall(MulDiv, int, adr, int, 1, int, 1, str)
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject: palindrome task
PostPosted: May 31st, 2009, 1:49 pm 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
I indented the code and added code to remove punctuation and whitespace as required by the task.
Code:
msgbox % isPalindrome("in girum imus nocte et consumimur igni") ; returns 1 for true

isPalindrome(str) {
str := RegexReplace(str, "\W+")
if (StrLen(str) < 2)  ; single character strings are palindromes
   return true
else
  if (SubStr(str, 1, 1) = SubStr(str, 0, 1))  ; if the first character
    Return isPalindrome(SubStr(str, 2, -1))   ; is same as last
                                              ; character, recurse
  else
    return False                       
}



Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 31st, 2009, 5:08 pm 
Offline
User avatar

Joined: November 2nd, 2008, 4:23 pm
Posts: 2906
Location: 127.0.0.1
Is there any way to set up syntax highlighting for rosetta code pages? I noticed that when we post we tell the page the language is AutoHotkey but it doesn't do anything special about it.

_________________
aboutscriptappsscripts
Any code ⇈ above ⇈ requires AutoHotkey_L to run


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 31st, 2009, 9:04 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
Just a note to everyone who contributes to Rosetta code: please take the time to double and triple check everything you add. Since the intention is to promote AHK, it looks bad when our examples have spelling errors and are listed out of order. I've already seen several examples of this. :x


Report this post
Top
 Profile  
Reply with quote  
 Post subject: bogosort changes
PostPosted: May 31st, 2009, 9:31 pm 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
sinkfaze wrote:
Here is my solution to the Bogosort task...

I started a separate thread with some improvements to this example:
http://www.autohotkey.com/forum/viewtop ... 341#272341

Its still a little buggy.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 31st, 2009, 11:58 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
The AHK example for File Exists does not complete the requested task, which is to differentiate between files and folders. This task is more complicated for AHK because FileExist() alone can't tell the difference.

This code fulfills the requirements of the task
Code:
attrib := FileExist("input.txt")
MsgBox,% attrib ? InStr(FileExist("input.txt"),"D") ? 0:1:0

attrib := FileExist("\input.txt")
MsgBox,% attrib ? InStr(FileExist("\input.txt"),"D") ? 0:1:0

MsgBox,% InStr(FileExist("docs"),"D")
MsgBox,% InStr(FileExist("\docs"),"D")
...but it's convoluted. Is this worth posting, or should the ineffective code remain since it's more readable?


Report this post
Top
 Profile  
Reply with quote  
 Post subject: fileexist
PostPosted: June 1st, 2009, 12:30 am 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
The task doesn't saying anything about "differentiating". It just happens to ask for the existence of a file and a directory.

If the given solution is modified, there should probably be two user defined functions: fexist, dexist which internally check the "D" attribute.

you could ask for a clarification on the task's talk page.


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, 2, 3, 4, 5 ... 28  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 12 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