 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
nick
Joined: 24 Aug 2005 Posts: 503 Location: Berlin / Germany
|
Posted: Fri May 29, 2009 9:49 pm Post subject: |
|
|
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
denick @ http://de.autohotkey.com/forum/ |
|
| Back to top |
|
 |
rulfzid
Joined: 27 Nov 2008 Posts: 62
|
Posted: Fri May 29, 2009 10:13 pm Post subject: Knuth shuffle |
|
|
| 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
} |
|
|
| Back to top |
|
 |
rulfzid
Joined: 27 Nov 2008 Posts: 62
|
Posted: Sat May 30, 2009 12:07 am Post subject: Greates common divisor |
|
|
| 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
} |
|
|
| Back to top |
|
 |
Frankie
Joined: 02 Nov 2008 Posts: 828
|
Posted: Sat May 30, 2009 2:33 am Post subject: |
|
|
Are we also trying to complete the Tasks not implemented in AutoHotkey section? _________________ Click here to join #AHK channel in IRC for general chat and quick help. |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 312 Location: Houston, TX
|
Posted: Sat May 30, 2009 3:09 am Post subject: status update |
|
|
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 Sat May 30, 2009 5:00 am; edited 2 times in total |
|
| Back to top |
|
 |
tkoi
Joined: 26 Jun 2008 Posts: 56
|
Posted: Sat May 30, 2009 3:31 am Post subject: |
|
|
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
}
|
|
|
| Back to top |
|
 |
Frankie
Joined: 02 Nov 2008 Posts: 828
|
Posted: Sat May 30, 2009 3:49 am Post subject: |
|
|
IMO its better to write the if-else statements out for readability purposes. _________________ Click here to join #AHK channel in IRC for general chat and quick help. |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 312 Location: Houston, TX
|
Posted: Sat May 30, 2009 4:57 am Post subject: readability |
|
|
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. |
|
| Back to top |
|
 |
rulfzid
Joined: 27 Nov 2008 Posts: 62
|
Posted: Sun May 31, 2009 1:16 pm Post subject: Doubly linked list |
|
|
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)
} |
|
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 312 Location: Houston, TX
|
Posted: Sun May 31, 2009 1:49 pm Post subject: palindrome task |
|
|
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
}
|
|
|
| Back to top |
|
 |
Frankie
Joined: 02 Nov 2008 Posts: 828
|
Posted: Sun May 31, 2009 5:08 pm Post subject: |
|
|
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. _________________ Click here to join #AHK channel in IRC for general chat and quick help. |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 1890 Location: MN, USA
|
Posted: Sun May 31, 2009 9:04 pm Post subject: |
|
|
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.  _________________ http://autohotkey.net/~jaco0646/ |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 312 Location: Houston, TX
|
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 1890 Location: MN, USA
|
Posted: Sun May 31, 2009 11:58 pm Post subject: |
|
|
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? _________________ http://autohotkey.net/~jaco0646/ |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 312 Location: Houston, TX
|
Posted: Mon Jun 01, 2009 12:30 am Post subject: fileexist |
|
|
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. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|