 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Laszlo
Joined: 14 Feb 2005 Posts: 4515 Location: Boulder, CO
|
Posted: Thu Jun 18, 2009 5:18 pm Post subject: |
|
|
I meant, buggy codes are posted in other languages. The 99 bottles of beer challenge provides several examples. If you find my functions acceptable, post it, with a url to here.
| Code: | | SubStr("6-----4---2-4---2-4---6-----2",Mod(k,30),1) | This is added to k to get the next trial divisor, not divisible by 2, 3 or 5, as the comment next to function header tells. It is very easy to verify. Start with e.g. k = 7. The SubStr expression tells that the next divisor is 4 larger, k = 11. For k = 11 the SubStr expression tells the next divisor is 2 larger, 13, and so on. We get into a cycle of length 8, so we don’t need entries for never encountered k (mod 30) values, indicated by “-“ in the haystack. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4515 Location: Boulder, CO
|
Posted: Thu Jun 18, 2009 10:47 pm Post subject: Monte Carlo Simulation for pi |
|
|
Count how many of n random throws in a square falls into an inscribed circle. It yields an estimate of pi. | Code: | MsgBox % MontePi(10000) ; 3.154400
MsgBox % MontePi(100000) ; 3.142040
MsgBox % MontePi(1000000) ; 3.142096
MontePi(n) {
Loop %n% {
Random x, -1, 1.0
Random y, -1, 1.0
p += x*x+y*y < 1
}
Return 4*p/n
} |
|
|
| Back to top |
|
 |
n-l-i-d Guest
|
Posted: Fri Jun 19, 2009 12:13 pm Post subject: |
|
|
Am going through the posting so far to cleanup syntax-wise (indentation and casing). It looks like the syntax-coloring for AutoHotkey stopped working.  |
|
| Back to top |
|
 |
n-l-i-d Guest
|
Posted: Fri Jun 19, 2009 1:26 pm Post subject: |
|
|
Ok, one request: if you post something to the Rosetta Code site, please use proper indentation and casing.
| Quote: | msgbox -> MsgBox
return -> Return
if -> If
while -> While
instr() -> InStr()
etcet...
|
And please use an indentation of 2 spaces per "tab"
TY |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 309 Location: Houston, TX
|
Posted: Fri Jun 19, 2009 2:11 pm Post subject: syntax and casing |
|
|
Thanks for cleaning up the casing and indentation.
Please add autohotkey as a language on your user page, see first post for instructions.
We could write a little robot to fix the casing and indentation...
I will try to get the highlighting back online, once i update the lexer a little. |
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 378
|
|
| Back to top |
|
 |
n-l-i-d Guest
|
Posted: Fri Jun 19, 2009 3:48 pm Post subject: |
|
|
Well, it may not equal the examples of most other languages (they are usually lowercase), but it does confirm with the documentation casing. The fact that AutoHotkey is case-insensitive and generally very "forgiving" could be explained on the language page.
I suggest lowercase for variables and proper mixed case for keywords. I also suggest using descriptive variable names, even if the tendency is to create short code, I think the site is a proper outlet to clarify what is going on in the code, not necessarily provide the fastest and most condensed code. One could add variations if applicable, sure, but per default please some consistency.
To make the syntax-coloring stand out more, it helps to avoid using variable names that are also AutoHotkey keywords.
Just my 2 €cts |
|
| Back to top |
|
 |
n-l-i-d Guest
|
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 378
|
Posted: Fri Jun 19, 2009 4:06 pm Post subject: |
|
|
hmm, yeah i think that would be good for most ahk scripts posted on RosettaCode. But I lowercased mine anyway. xD _________________
Scripts - License |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4515 Location: Boulder, CO
|
Posted: Fri Jun 19, 2009 4:08 pm Post subject: Mode of lists of numbers |
|
|
Mode of a list of numbers is the number, which occurs the most frequently in the list. This function sorts and scans the list once to find the Mode. The delimiter can be given in the second parameter, default to space. | Code: | MsgBox % Mode("")
MsgBox % Mode("1 2 3")
MsgBox % Mode("1 2 0 3 0.0")
MsgBox % Mode("0.1 2.2 -0.1 0.22e1 2.20 0.1")
Mode(a, d=" ") { ; the number that occurs most frequently in a list delimited by d (space)
Sort a, ND%d%
Loop Parse, a, %d%
If (V != A_LoopField) {
If (Ct > MxCt)
MxV := V, MxCt := Ct
V := A_LoopField, Ct := 1
}
Else Ct++
Return Ct>MxCt ? V : MxV
} |
Another approach was to convert the numbers to variable names (e.g. by appending “v” and replacing “.” with “_”), and count in these variables the number of time the corresponding value occurs. We’d have difficulties with equal numbers written differently, like 10, 10.0, 1.0e1, therefore the list has to be first scanned and the numbers converted to uniform notation, e.g. with “SetFormat FastFloat, 0.16e” and adding 0 to each, making the function slow and complicated. |
|
| Back to top |
|
 |
n-l-i-d Guest
|
Posted: Fri Jun 19, 2009 4:14 pm Post subject: |
|
|
| @infogulch: I added your code. |
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 378
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4515 Location: Boulder, CO
|
Posted: Fri Jun 19, 2009 4:55 pm Post subject: |
|
|
| Can someone post my functions, too? |
|
| Back to top |
|
 |
n-l-i-d Guest
|
Posted: Fri Jun 19, 2009 5:10 pm Post subject: |
|
|
I added all of Laszlo's functions, apart from the MontePi, couldn't find that one...  |
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 378
|
Posted: Fri Jun 19, 2009 5:11 pm Post subject: |
|
|
Heh, we're gonna need a full-time Rosetta Code updater just to add stuff posted here.
I looked at editing them, but couldn't figure out how to do it.
Here's another one: Forward Difference
| Code: | list := "90,47,58,29,22,32,55,5,55,73"
msgbox % forwardDiff(list, 9, True) ; returns every step on a new line
msgbox % forwardDiff(list, 5) ; returns only the final result
forwardDiff(list, depth=1, append = False, sep=",")
{
Loop, Parse, list, % sep
{
If (lastVal != "")
newList .= A_LoopField - lastVal . sep
lastVal := A_LoopField
}
newList := SubStr(newList, 1, -1)
If ((depth -= 1) && newList != "")
return (append ? newList "`n" : "" ) forwardDiff(newList, depth, sep, append)
return newList
} |
I tried to format it nice, but it can be reformatted.
Edit: oh, and what about Laszlo's MD5? _________________
Scripts - License
Last edited by infogulch on Fri Jun 19, 2009 5:15 pm; edited 1 time in total |
|
| 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
|