 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Paddy3118 Guest
|
Posted: Tue Jun 23, 2009 5:48 am Post subject: Re: Moving Average |
|
|
| Laszlo wrote: | | Last N rather than all. wrote: | | Unfortunately you need to average the last N rather than all the numbers. - See other entries on the R.C. site. |
| rosettacode wrote: | | function/class/instance that takes a number as argument and returns a simple moving average of its arguments so far |
|
Ta!  |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 7181
|
Posted: Tue Jun 23, 2009 8:38 am Post subject: |
|
|
| I feel Scripts & Functions suits this topic.. |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 1875 Location: MN, USA
|
Posted: Tue Jun 23, 2009 3:22 pm Post subject: |
|
|
I was set against that idea a few weeks ago when tinku99 asked me... | I wrote: | | S&F is a showcase for completed work and discussion of that work. The Rosetta thread is a combination of philosophical debate and work in progress. Its intended showcase is the Rosetta code wiki. | ...but in light of Laszlo's recent activity there is an awful lot of working code here. It would seem my argument no longer holds water. SKAN, if you believe it belongs in S&F, I don't object. _________________ http://autohotkey.net/~jaco0646/ |
|
| Back to top |
|
 |
sinkfaze
Joined: 19 Mar 2008 Posts: 2697 Location: the tunnel(?=light)
|
Posted: Tue Jun 23, 2009 4:37 pm Post subject: |
|
|
I think this thread should remain here since, as you noted, its purpose is not directly related to that forum. I think it would be better to create a separate thread in S&F which hosts the finished RosettaCode functions. _________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 1875 Location: MN, USA
|
Posted: Tue Jun 23, 2009 6:00 pm Post subject: |
|
|
I oppose redundant posting of code, especially within the forums. From my correspondence with tinku99... | I wrote: | | In my opinion, posting code of completed tasks in the Scripts & Functions forum is redundant. The purpose of completing these tasks is to post them on Rosetta. Copying each one to Scripts & Functions clutters the forum. | The same scripts should definitely not be posted here in General Chat and also in S&F.
I do believe the Rosetta codes need to be promoted as much as possible, but not by duplicating every script. Several people have done an amazing job with this project; and it was a brilliant idea by tinku99 to begin with.
I have thought for some time that something should be done with Jon's "Catalogue of Scripts" sticky topic in the S&F forum. Since Jon moved his catalogue to the wiki years ago, the only purpose of that thread is to redirect people to the wiki. I propose to do the same with that sticky topic as Titan did with the AutoHotkey.net sticky: cut it down to one post to inform users of the many working code examples on the Wiki and on Rosetta code. It should serve as an advertisement so to speak. _________________ http://autohotkey.net/~jaco0646/ |
|
| Back to top |
|
 |
sinkfaze
Joined: 19 Mar 2008 Posts: 2697 Location: the tunnel(?=light)
|
Posted: Tue Jun 23, 2009 7:00 pm Post subject: |
|
|
| jaco0646 wrote: | | I oppose redundant posting of code, especially within the forums...the same scripts should definitely not be posted here in General Chat and also in S&F. |
While I agree that redundancy is typically bad form, this thread has already constructed itself in a manner that is not conducive to the spirit of threads you would typically find in S&F so simply moving the thread would equally be bad form, IMO (danged if we do, danged if we don't).
Here's my thought along the lines of your idea: Make a sticky note in S&F that redirects to our section of the Rosetta Code Wiki and the original thread, like this:
| Wiki Sticky wrote: | AutoHotKey members are participating in a programming project hosted by Rosetta Code, whose idea is "to present solutions to the same task in as many different languages as possible, to demonstrate how languages are similar and different, and to aid a person with a grounding in one language in learning another." Please go visit AutoHotKey's main page, which also contains the list of programming tasks completed in the AutoHotKey language. To see tasks which have not yet been completed, visit AutoHotKey's unimplemented task page.
If you would like to participate with other AutoHotKey members in completing some of our unimplemented tasks or on other items pertaining to the Rosetta Code project, please visit the Rosetta Code challenge thread in the General Forum. |
That way the sticky can present the information with a more reader-friendly logic that the thread itself simply doesn't have while leaving the thread in its original forum and avoiding code rendundancy.
 _________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 7181
|
Posted: Tue Jun 23, 2009 7:59 pm Post subject: |
|
|
| jaco0646 wrote: | | in light of Laszlo's recent activity there is an awful lot of working code here. It would seem my argument no longer holds water. SKAN, if you believe it belongs in S&F, I don't object. |
I could not login for past few hours owing to some phpBB Critical Error and so I was unable to respond.. IMHO, the gems from Laszlo always belong to Scripts and Functions. Since sinkfaze feels otherwise, I will remember to bump and move this topic to Scripts and Functions when it fades out to the second page. |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 1875 Location: MN, USA
|
Posted: Tue Jun 23, 2009 9:36 pm Post subject: |
|
|
@sinkfaze
I agree with everything in your latest post. That's what I propose to do.
| SKAN wrote: | | ...the gems from Laszlo always belong to Scripts and Functions. | While I certainly agree with this statement, as sinkfaze alluded: this thread is a mess, and contains as much material that doesn't belong in S&F as useful code.
Also, even if this thread is moved to S&F, unless it is made sticky (which I strongly oppose: five stickies in one forum is more than enough) this thread won't stay visible there either. I believe that combining an explanation of Rosetta code with Jon's first post in the Catalogue thread is the most consistent and productive option. The revised thread could then be titled something like, "AutoHotkey Wiki + RosettaCode Challenges." _________________ http://autohotkey.net/~jaco0646/ |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 7181
|
Posted: Tue Jun 23, 2009 9:50 pm Post subject: |
|
|
| jaco0646 wrote: | | Also, even if this thread is moved to S&F, unless it is made sticky ... this thread won't stay visible there either. |
That was not the point. People tend to NOT CLICK General Chat topic in Forum search results. As for me, I always confine my search to "Scripts & Functions" and/or "Ask for Help"
| jaco0646 wrote: | | That's what I propose to do. |
Anyways! I have no problem with that.. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4515 Location: Boulder, CO
|
Posted: Tue Jun 23, 2009 11:56 pm Post subject: Shell sort |
|
|
Shell sort of comma separated lists: | Code: | MsgBox % ShellSort("")
MsgBox % ShellSort("xxx")
MsgBox % ShellSort("3,2,1")
MsgBox % ShellSort("dog,000000,xx,cat,pile,abcde,1,cat,zz,xx,z")
MsgBox % ShellSort("12,11,10,9,8,4,5,6,7,3,2,1,10,13,14,15,19,17,18,16,20,10")
ShellSort(var) { ; SORT COMMA SEPARATED LIST
StringSplit a, var, `, ; make array (length = a0)
inc := a0
While inc:=round(inc/2.2) ; geometric gap sequence
Loop % a0-inc { ; insertion sort:
i := A_Index+inc, t := a%i%, j := i, k := j-inc
While j > inc && a%k% > t
a%j% := a%k%, j := k, k -= inc
a%j% := t
}
Loop % a0 ; construct string from sorted array
s .= "," . a%A_Index%
Return SubStr(s,2) ; drop leading comma
} |
|
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 1875 Location: MN, USA
|
Posted: Wed Jun 24, 2009 12:01 am Post subject: |
|
|
| SKAN wrote: | | I have no problem with that. | Awesome, I've done it.
| SKAN wrote: | | That was not the point. | You could split this topic by individually selecting all of the useful code posts and moving them to a new Scripts & Functions thread, leaving the discussion posts behind.  _________________ http://autohotkey.net/~jaco0646/ |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4515 Location: Boulder, CO
|
Posted: Wed Jun 24, 2009 1:08 am Post subject: FIFO |
|
|
FIFO's, or stacks can be implemented via global arrays. Their names are passed to the stack handling functions, so many stacks can be used at any time: | Code: | push("st",2),push("st",4) ; TEST: push 2 and 4 onto stack named "st"
While !empty("st") ; Repeat until stack is not empty
MsgBox % pop("st") ; Print popped values (4, 2)
MsgBox % pop("st") ; Empty
MsgBox %ErrorLevel% ; ErrorLevel = 1: popped too much
push(stack,x) { ; push x onto stack named "stack"
Local _ ;
_ := %stack%0 := %stack%0="" ? 1 : %stack%0+1
%stack%%_% := x
}
pop(stack) { ; pop value from stack named "stack"
Local _ ;
_ := %stack%0
If (_ < 1)
Return "", ErrorLevel := 1
Return %stack%%_%, %stack%0 := _-1
}
empty(stack) { ; check if stack named "stack" is empty
Global
Return %stack%0<1
} |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4515 Location: Boulder, CO
|
Posted: Wed Jun 24, 2009 1:38 am Post subject: Knuth shuffle |
|
|
The function for the Knuth shuffle creates the array internally from a list, and returns the shuffled list, for easy display: | Code: | MsgBox % shuffle("1,2,3,4,5,6,7,8,9")
MsgBox % shuffle("1,2,3,4,5,6,7,8,9")
shuffle(list) { ; shuffle comma separated list, converted to array
StringSplit a, list, `, ; make array (length = a0)
Loop % a0-1 {
Random i, A_Index, a0 ; swap item 1,2... with a random item to the right of it
t := a%i%, a%i% := a%A_Index%, a%A_Index% := t
}
Loop % a0 ; construct string from sorted array
s .= "," . a%A_Index%
Return SubStr(s,2) ; drop leading comma
} |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4515 Location: Boulder, CO
|
Posted: Wed Jun 24, 2009 2:00 am Post subject: Longest Common Subsequence |
|
|
A short recursive (slow) function for the Longest Common Subsequence task (based on the Python code): | Code: | MsgBox % lcs("1234","24") ; 24
MsgBox % lcs("thisisatest","testing123testing") ; tsitest
lcs(a,b) { ; Recursive Longest Common Subsequence of strings
Return a="" || b="" ? "" : Asc(a)=Asc(b) ? SubStr(a,1,1) lcs(SubStr(a,2),SubStr(b,2))
: StrLen(u:=lcs(a,SubStr(b,2))) > StrLen(v:=lcs(SubStr(a,2),b)) ? u : v
} |
The following function uses dynamic programming, and it is much faster. It is based on the Java program posted to rosettacode: | Code: | lcs(a,b) { ; Longest Common Subsequence of strings, using Dynamic Programming
Loop % StrLen(a)+2 { ; Initialize
i := A_Index-1
Loop % StrLen(b)+2
j := A_Index-1, len%i%_%j% := 0
}
Loop Parse, a ; scan a
{
i := A_Index, i1 := i+1, x := A_LoopField
Loop Parse, b ; scan b
{
j := A_Index, j1 := j+1, y := A_LoopField
len%i1%_%j1% := x=y ? len%i%_%j% + 1
: (u:=len%i1%_%j%) > (v:=len%i%_%j1%) ? u : v
}
}
x := StrLen(a)+1, y := StrLen(b)+1
While x*y { ; construct solution from lengths
x1 := x-1, y1 := y-1
If (len%x%_%y% = len%x1%_%y%)
x := x1
Else If (len%x%_%y% = len%x%_%y1%)
y := y1
Else
x := x1, y := y1, t := SubStr(a,x,1) t
}
Return t
} |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4515 Location: Boulder, CO
|
Posted: Wed Jun 24, 2009 3:44 pm Post subject: Non Continuous Subsequences |
|
|
We can find all Non Continuous Subsequences by selecting those 01 sequences, which do not consist of only one block of 1’s. It is very easy with regular expressions. At the end we have to translate 1’s to the sequence entries corresponding to their position: | Code: | n := 4 ; sequence length
Loop % (1<<n) - 1 { ; try all 0-1 candidate sequences
If !RegExMatch(b:=ToBin(A_Index,n),"^0*1*0*$") { ; drop continuous subsequences
Loop Parse, b
t .= A_LoopField ? A_Index " " : "" ; position -> number
t .= "`n" ; new sequences in new lines
}
}
MsgBox % t
ToBin(n,W=16) { ; LS W-bits of Binary representation of n
Return W=1 ? n&1 : ToBin(n>>1,W-1) . n&1
} |
|
|
| 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
|