- Code Fragments and Useful Routines
- 28 Aug 2006 05:44:13 pm
- Last edited by KermMartian on 24 Sep 2016 08:42:48 pm; edited 2 times in total
Post 'em up here, like this:
Routine Title
Details about routine, including inputs, outputs, comments, destroyed, etc.
Code:
A compiled list of routines:
Filled Circle
X=x-coordinate of center
Y=y-coordinate of center
R=radius
Code:
Calc84Maniac
Fraction Parts
Input: Ans=a number
Output: Ans={numerator,denominator}
Code:
Calc84Maniac
Graph Mode
Inputs: none
Output: Ans=
0:FUNC
1:PAR
2:POL
3:SEQ
Destroys:GDBn
Code:
Calc84Maniac
Counting List Matches
Returns how many times A appears in LIST
Code:
Returns how many corresponding elements of LIST1 and LIST2 are equal
Note: dim(LIST1) must equal dim(LIST2)
Code:
Calc84Maniac
Mode of List
Inputs: LIST
Outputs: D=mode(LIST
E=1 if no unique mode, otherwise E=0
Code:
Calc84Maniac
Most efficient getkey
input: none
output: the key pressed in k (or you can change it)
Code:
Harq
Text to List
Input: List, Str1, Str2
Output: none
Takes text entered from input and changes it into list
Code:
Lafferjm
List To Text
Input: None
Output: List changed into text
Takes numbers in list from List to text and changes it into a word on the screen
Code:
Lafferjm
Shuffle Elements of List
Shuffles LIST1
Destroys LIST2
Code:
Note: If you know what dim(LIST1) will be, replace it with a number.
Calc84Maniac
Formatted Text on a graph Screen
Clears the graph screen
Uses Str1 as input string
Code:
B-flat, KermMartian, and Calc84Maniac
Modulo division
Input:
A=Dividend
B=Divisor
Code:
The Round( is in there in case of an infinitely repeating decimal (like 1/3).
The Tari, Something1990, and Calc84Maniac
Matrix to List
Input: Matrix A
Output: Lsave
Converts any size matrix to a list
Code:
Lafferjm
List to Matrix
Inputs: Lsave
output:Matrix [A]
Takes any size list and stores it into a matrix
Code:
Lafferjm
Convert A to a String [optimized]
Code:
Weregoose
Chop off first element of list
Input: Ans
Output: Ans
Input and output variables can be changed to whatever.
Code:
Calc84Maniac
Rotate a 3x3 matrix 90°
Input: Ans
Output: Ans
Code:
HaveACalc
Find where a number occurs in a list
Inputs:
A = A number
Output: the position of the last occurrence of A in L1
Code:
Ed H
Find where a number occurs in a list (Within index)
Inputs:
A = A number
B = Where it begins looking
C = Where it ends looking
Output: The position of the last occurrence of A in the portion of L1 starting from element B and ending with element C
Code:
Ed H
Convert Base10 to any base from 2 to 16
Inputs:
A = Number to convert
B = Base to convert to
Outputs: Str0
Code:
RthProg
Change Case of a String
Inputs:
A = (1) Change to uppercase
Str1 = String to convert
Outputs:
Str1 = Converted string
Code:
RthProg and Ed H
Number to Roman Numeral
Inputs:
N = Number to convert
Outputs:
Ans & Str0 = Converted number
Code:
Ed H
Find Unique Elements in Two Lists
Input L1 and L2
Output L3 (All elements that are in L1 but not L2, and elements that are in L2 but not L2.
Conditions: "0" is not a unique element, and no element is repeated within an individual list
Code:
RthProg
Get Letter Routine
Inputs:
None
Outputs:
Ans = Letter (or '?' if not a letter)
Code:
_Player1537 and RthProg
8X8 Sprite to Hex
Inputs:
X- x position of sprite
Y- y position of sprite
Code:
RthProg
Resistor Value to Color Code
Inputs:
X = Ohm value (such as 5700)
Outputs:
A,B,C = Color code (5,7,2 = 57*10^2)
Code:
KermMartian
Check Code Speed
Inputs:
None
Outputs:
Ans = Time it took to run once
Code:
_Player1537 and MufinMcFlufin
Base-N to Decimal Converter
Inputs:
N = Base to convert from
Str1 = Number to convert
Outputs:
Ans = Converted number
Code:
MufinMcFlufin
Base-N to Base-M converter
Inputs:
N = Base to convert from
M = Base to convert to
Str1 = Number to convert
Outputs:
Str1 & Ans = Converted number[i]
Code:
[i]MufinMcFlufin
Routine Title
Details about routine, including inputs, outputs, comments, destroyed, etc.
Code:
:Here you have
:The code to your
:fragment or routine
A compiled list of routines:
Filled Circle
X=x-coordinate of center
Y=y-coordinate of center
R=radius
Code:
:For(F,0,R,[deltax]
:sqrt(R^2-F^2
:Line(X+F,Y+Ans,X+F,Y-Ans
:Line(X-F,Y+Ans,X-F,Y-Ans
:End
Calc84Maniac
Fraction Parts
Input: Ans=a number
Output: Ans={numerator,denominator}
Code:
:{Ans,1,fpart(abs(Ans
:While e-9<Ans(3
:{Ans(1),Ans(3),Ans(3)fpart(Ans(2)/Ans(3
:End
:round({Ans(1),1}/Ans(2),0
Calc84Maniac
Graph Mode
Inputs: none
Output: Ans=
0:FUNC
1:PAR
2:POL
3:SEQ
Destroys:GDBn
Code:
:StoreGDB n
:1->Tmin
:1->[thetamin]
:2->nMin
:ZStandard
:not(Tmin)+2not([thetamin])+3(nMin=1
:RecallGDB n
:Delvar GDBn
Calc84Maniac
Counting List Matches
Returns how many times A appears in LIST
Code:
:sum(LIST=A
Returns how many corresponding elements of LIST1 and LIST2 are equal
Note: dim(LIST1) must equal dim(LIST2)
Code:
:sum(LIST1=LIST2
Calc84Maniac
Mode of List
Inputs: LIST
Outputs: D=mode(LIST
E=1 if no unique mode, otherwise E=0
Code:
:0->B
:For(A,1,dim(LIST
:sum(LIST=LIST(A->C
:If C=B and D/=LIST(A
:1->E
:If C>B
:LIST(A->D
:Delvar EC->B
:End
:End
Calc84Maniac
Most efficient getkey
input: none
output: the key pressed in k (or you can change it)
Code:
Repeat Ans
getkey
end
Ans->K
Harq
Text to List
Input: List, Str1, Str2
Output: none
Takes text entered from input and changes it into list
Code:
:DelVar L1
:"ABCDEFGHIJKLMNOPQRSTUVWXYZ? .→Str1
:ClrHome
:Input "NAME:",Str2
:For(A,1,length(Str2
:inString(Str1,sub(Str2,A,1→L1(A
:End
Lafferjm
List To Text
Input: None
Output: List changed into text
Takes numbers in list from List to text and changes it into a word on the screen
Code:
:ClrHome
:"ABCDEFGHIJKLMNOPQRSTUVWXYZ? .→Str1
:For(A,1,dim(L1
:Output(1,A,sub(Str1,L1(A),1
:End
Lafferjm
Shuffle Elements of List
Shuffles LIST1
Destroys LIST2
Code:
:rand(dim(LIST1->LIST2
:SortA(LIST2,LIST1
Note: If you know what dim(LIST1) will be, replace it with a number.
Calc84Maniac
Formatted Text on a graph Screen
Clears the graph screen
Uses Str1 as input string
Code:
:ClrDraw
:length(Str1->A
:For(J,1,1+iPart(A/23
:For(I,1,23
:If 23J-24+I<A:Text(7J-7,4I-4,sub(Str1,23J-23+I,1
:End
:End
B-flat, KermMartian, and Calc84Maniac
Modulo division
Input:
A=Dividend
B=Divisor
Code:
:Round(BfPart(A/B),0
The Round( is in there in case of an infinitely repeating decimal (like 1/3).
The Tari, Something1990, and Calc84Maniac
Matrix to List
Input: Matrix A
Output: Lsave
Converts any size matrix to a list
Code:
:ClrHome
:1→dim(L2
:dim([A]→L1
:L1(1→A
:L1(2→B
:For(C,1,A
:For(D,1,B
:[A](C,D→L2(dim(L2)+1
:End
:End
:For(A,2,dim(L2)
:L2(A)→∟SAVE(A-1
:End
:DelVar L1DelVar L2DelVar [A]DelVar BDelVar CDelVar ADelVar DOutput(1,1,"
:0
Lafferjm
List to Matrix
Inputs: Lsave
output:Matrix [A]
Takes any size list and stores it into a matrix
Code:
:ClrHome
:Input "ROWS:",F
:ClrHome
:dim(∟SAVE)/F→G
:{F,G→dim([A]
:1→A
:1→B
:G→C
:0→D
:Repeat A=F+1
:For(E,1,G
:D+1→D
:∟SAVE(D→[A](A,E
:End
:A+1→A
:C+1→B
:C+G→C
:End
:DelVar ADelVar BDelVar CDelVar DDelVar EDelVar FDelVar GOutput(1,1,"
:0
Lafferjm
Convert A to a String [optimized]
Code:
{A,A->L2
{0,1->L1
LinReg(a+bx) Y1
Equ>String(Y1,Str1
sub(Str1,1,length(Str1)-3->Str1
Weregoose
Chop off first element of list
Input: Ans
Output: Ans
Input and output variables can be changed to whatever.
Code:
ΔList(cumSum(Ans
Calc84Maniac
Rotate a 3x3 matrix 90°
Input: Ans
Output: Ans
Code:
rowSwap(Ans<superscript T thing>,1,3
HaveACalc
Find where a number occurs in a list
Inputs:
A = A number
Output: the position of the last occurrence of A in L1
Code:
:max(seq(X(A=L1(X)),X,1,dim(L1
Ed H
Find where a number occurs in a list (Within index)
Inputs:
A = A number
B = Where it begins looking
C = Where it ends looking
Output: The position of the last occurrence of A in the portion of L1 starting from element B and ending with element C
Code:
:max(seq(X(A=L1(X)),X,B,C
Ed H
Convert Base10 to any base from 2 to 16
Inputs:
A = Number to convert
B = Base to convert to
Outputs: Str0
Code:
:"_
:For(I,1,1+ln(A)/ln(B
:Ans+sub("0123456789ABCDEF",1+int(BfPart(AB^-I)),1
:End
RthProg
Change Case of a String
Inputs:
A = (1) Change to uppercase
Str1 = String to convert
Outputs:
Str1 = Converted string
Code:
:not(A)-->A
:length(Str1)->B
:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"->Str2
:For(N,1,B
:inString(sub(Str2,26A + 1, 26),sub(Str1,N,1)
:Str1 + sub(sub(Str1,N,1) +sub(sub(Str2,26,26)+sub(Str2,1,26),26A+1,26),Ans+1,1)-->Str1
:End
:sub(Str1,B+1,B)-->Str1
RthProg and Ed H
Number to Roman Numeral
Inputs:
N = Number to convert
Outputs:
Ans & Str0 = Converted number
Code:
:" →Str0
:For(X,0,log(N
:10fPart(.1N→Z
:While Z
:sum({4.1,4.1,9.2}(Z={4,5,9→T
:sub("IVXLCDMvxlcdmni",2X+10fPart(T)+1,1)+Str0→Str0
:abs(Z-1-int(T→Z
:End
:int(.1N→N
:End
:Str0
Ed H
Find Unique Elements in Two Lists
Input L1 and L2
Output L3 (All elements that are in L1 but not L2, and elements that are in L2 but not L2.
Conditions: "0" is not a unique element, and no element is repeated within an individual list
Code:
:augment(L1,L2-->L3
:L3*seq((1=sum(not(L3-L3(X)))),X,1,dim(L3))-->L3
:SortD(L3
:sum(L3!=0)-->dim(L3
RthProg
Get Letter Routine
Inputs:
None
Outputs:
Ans = Letter (or '?' if not a letter)
Code:
:Repeat Ans
:GetKey
:End
:sub("ABC DEFGHIJKLMNOPQRSTUVWXYZθ :?",Ans-5iPart(.1Ans+4),1
_Player1537 and RthProg
8X8 Sprite to Hex
Inputs:
X- x position of sprite
Y- y position of sprite
Code:
" .one space
For(A,0,15
Ans+sub("0123456789ABCDEF",sum(seq(2^(3-B)pxl-Test(Y+iPart(A/2),X+B+8fPart(A/2)),B,0,3))+1,1
End
sub(Ans,2,16
RthProg
Resistor Value to Color Code
Inputs:
X = Ohm value (such as 5700)
Outputs:
A,B,C = Color code (5,7,2 = 57*10^2)
Code:
X/10^(int(log(X->A
int(10fPart(A->B
int(A->A
int(log(.1X->C
KermMartian
Check Code Speed
Inputs:
None
Outputs:
Ans = Time it took to run once
Code:
Repeat Ans=2
CheckTimer(K
End
For(A,1,Iterations
YOUR CODE TO TEST HERE
End
CheckTimer(K
(Ans-2)/Iterations
_Player1537 and MufinMcFlufin
Base-N to Decimal Converter
Inputs:
N = Base to convert from
Str1 = Number to convert
Outputs:
Ans = Converted number
Code:
sum(seq(inString(sub("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",1,N),sub(Str1,A,1)),A,1,length(Str1))seq(N^A,A,length(Str1)-1,0,~1->B
MufinMcFlufin
Base-N to Base-M converter
Inputs:
N = Base to convert from
M = Base to convert to
Str1 = Number to convert
Outputs:
Str1 & Ans = Converted number[i]
Code:
sum(seq(N^A,A,length(Str1)-1,0,~1)seq(inString(sub("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",1,N),sub(Str1,A,1)),A,1,length(Str1->B
Pause B
seq(0,A,1,int(log(B)/log(M->L1
While B
int(log(B)/log(M->C
int(BM^~Ans->L1(dim(L1)-Ans+1
B-AnsM^C->B
End
" ->Str1
For(A,1,dim(L1
Str1+sub("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",1+L1(A),1->Str1
End
DelVar L1sub(Str1,2,length(Str1)-1->Str1
[i]MufinMcFlufin