 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
tidbit
Joined: 10 Mar 2008 Posts: 676 Location: USA
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4517 Location: Boulder, CO
|
Posted: Tue Feb 02, 2010 6:08 pm Post subject: |
|
|
| infogulch wrote: | | I have issue with the current moving average code. The code itself is excellent; however, I think its not ideal for the purpose of rosettacode. Keeping static copies of the different values inside the function doesn't conform to the spirit of the objective imo, and is not universal (i.e. it couldn't be used in a library). | Well, you don't even need a function: | Code: | avg := i := 0
MsgBox % avg += (3-avg)/++i
MsgBox % avg += (8-avg)/++i
MsgBox % avg += (16-avg)/++i
MsgBox % avg += (53-avg)/++i |
|
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 378
|
Posted: Tue Feb 02, 2010 6:24 pm Post subject: |
|
|
| Laszlo wrote: | | Well, you don't even need a function: | You are absolutely correct.
...
Actually: | Quote: | Create a stateful function/class/instance that takes a number as argument and returns a simple moving average of its arguments so far.
Note that the moving average has a period and numbers beyond the period must be dropped from the calculation of the average. |
I didn't read the goal correctly, so I misinterpreted moving average for cumulative average. I don't have any issues anymore, sorry.  _________________
Scripts - License |
|
| Back to top |
|
 |
ih57452
Joined: 06 Feb 2010 Posts: 9
|
Posted: Sat Feb 06, 2010 6:17 pm Post subject: Print a Multiplication Table |
|
|
Print a Multiplication Table
| Code: | SendMode, Input
Run, Notepad.exe,, Max
WinWaitActive, ahk_class Notepad
size = 12
x = 0
y = -1
Loop, % size + 2 {
If (y = -1)
Send, % format("X|")
Else If (y = 0)
Send, % format("|", "-")
Else Send, % format(y . "|")
Loop, %size% {
x += 1
If (y = -1)
Send, % format(x)
Else If (y = 0)
Send, % format("", "-")
Else If (x < y)
Send, % format("")
Else Send, % format(x * y)
}
x = 0
y += 1
Send, {Enter}
}
format(str, fill = " ") {
global size
While, (StrLen(str) <= StrLen(size * size + 1))
str := fill . str
Return, str
} |
I was trying to make the table look like the Perl example, but when I tried to put a "+" where the two lines intersected, my format() function apparently interpreted it as an operator instead of just a character in a string. I haven't figured out how to fix that yet, but a pipe will work. It works fine for the required 12x12 table, but turning it up to about 32x32 can cause some weird things to happen (looks like possibly a buffer getting filled up?). I've also considered making a version to display it in a AutoHotkey GUI and possibly another one to make it put the numbers into OpenOffice Calc. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4517 Location: Boulder, CO
|
Posted: Sat Feb 06, 2010 7:47 pm Post subject: |
|
|
Nice! You can write it little shorter: | Code: | size = 24
Loop % size + 2 {
t .= (y:=A_Index-2)<0 ? format("X|") : y=0 ? "`n" format("+", "-") : "`n" format(y . "|")
Loop %size%
t .= y<0 ? format(A_Index) : y=0 ? format("", "-") : A_Index<y ? format("") : format(A_Index * y)
}
Gui Font, s10, Courier New
Gui Add, Text,,%t%
Gui Show
format(str, fill = " ") {
Global size
VarSetCapacity(f, n:=StrLen(size*size)+1-StrLen(str), Asc(fill))
Return SubStr(f,1,n) str
} |
|
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 312 Location: Houston, TX
|
|
| 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
|