Difference between revisions of "TI-BASIC:Useful Routines"

From Learn @ Cemetech
Jump to navigationJump to search
(→‎Most Efficient getKey: Made it more efficient.)
Line 6: Line 6:
 
** R = radius
 
** R = radius
  
:For(F,0,R,[deltax]
+
<code>:For(F,0,R,[deltax]
:sqrt(R^2-F^2
+
:sqrt(R^2-F^2
:Line(X+F,Y+Ans,X+F,Y-Ans
+
:Line(X+F,Y+Ans,X+F,Y-Ans
:Line(X-F,Y+Ans,X-F,Y-Ans
+
:Line(X-F,Y+Ans,X-F,Y-Ans
:End
+
:End</code>
  
 
=== Fraction Parts ===
 
=== Fraction Parts ===
Line 19: Line 19:
 
** Ans={numerator,denominator}
 
** Ans={numerator,denominator}
  
:{Ans,1,fpart(abs(Ans
+
<code>:{Ans,1,fpart(abs(Ans
:While e-9<Ans(3
+
:While e-9<Ans(3
:{Ans(1),Ans(3),Ans(3)fpart(Ans(2)/Ans(3
+
:{Ans(1),Ans(3),Ans(3)fpart(Ans(2)/Ans(3
:End
+
:End
:round({Ans(1),1}/Ans(2),0
+
:round({Ans(1),1}/Ans(2),0</code>
  
 
=== Graph Mode ===
 
=== Graph Mode ===
Line 35: Line 35:
 
* Destroys: GDBn
 
* Destroys: GDBn
  
:StoreGDB n
+
<code>:StoreGDB n
:1->Tmin
+
:1->Tmin
:1->[thetamin]
+
:1->[thetamin]
:2->nMin
+
:2->nMin
:ZStandard
+
:ZStandard
:not(Tmin)+2not([thetamin])+3(nMin=1
+
:not(Tmin)+2not([thetamin])+3(nMin=1
:RecallGDB n
+
:RecallGDB n
:Delvar GDBn
+
:Delvar GDBn</code>
  
 
=== Counting List Matches ===
 
=== Counting List Matches ===
Line 48: Line 48:
 
Returns how many times A appears in LIST
 
Returns how many times A appears in LIST
  
:sum(LIST=A
+
<code>:sum(LIST=A</code>
  
 
Returns how many corresponding elements of LIST1 and LIST2 are equal
 
Returns how many corresponding elements of LIST1 and LIST2 are equal
 
Note: dim(LIST1) must equal dim(LIST2)
 
Note: dim(LIST1) must equal dim(LIST2)
  
:sum(LIST1=LIST2
+
<code>:sum(LIST1=LIST2</code>
  
 
=== Mode of List ===
 
=== Mode of List ===
Line 62: Line 62:
 
** E = 1 if no unique mode, otherwise E=0
 
** E = 1 if no unique mode, otherwise E=0
  
:0->B
+
<code>:0->B
:For(A,1,dim(LIST
+
:For(A,1,dim(LIST
:sum(LIST=LIST(A->C
+
:sum(LIST=LIST(A->C
:If C=B and D/=LIST(A
+
:If C=B and D/=LIST(A
:1->E
+
:1->E
:If C>B
+
:If C>B
:LIST(A->D
+
:LIST(A->D
:Delvar EC->B
+
:Delvar EC->B
:End
+
:End
:End
+
:End</code>
  
 
=== Most Efficient getKey ===
 
=== Most Efficient getKey ===
Line 78: Line 78:
 
* Output: the key pressed in K (or you can change to another variable)
 
* Output: the key pressed in K (or you can change to another variable)
  
:Repeat Ans
+
<code>:Repeat Ans
:getkey->K
+
:getkey->K
:End
+
:End</code>
  
 
=== Text to List ===
 
=== Text to List ===
Line 88: Line 88:
 
Takes text entered from input and changes it into list
 
Takes text entered from input and changes it into list
  
:DelVar L1
+
<code>:DelVar L1
:"ABCDEFGHIJKLMNOPQRSTUVWXYZ? .→Str1
+
:"ABCDEFGHIJKLMNOPQRSTUVWXYZ? .→Str1
:ClrHome
+
:ClrHome
:Input "NAME:",Str2
+
:Input "NAME:",Str2
:For(A,1,length(Str2
+
:For(A,1,length(Str2
:inString(Str1,sub(Str2,A,1→L1(A
+
:inString(Str1,sub(Str2,A,1→L1(A
:End
+
:End</code>
  
 
=== List To Text ===
 
=== List To Text ===
Line 102: Line 102:
 
Takes numbers in list from List to text and changes it into a word on the screen
 
Takes numbers in list from List to text and changes it into a word on the screen
  
:ClrHome
+
<code>:ClrHome
:"ABCDEFGHIJKLMNOPQRSTUVWXYZ? .→Str1
+
:"ABCDEFGHIJKLMNOPQRSTUVWXYZ? .→Str1
:For(A,1,dim(L1
+
:For(A,1,dim(L1
:Output(1,A,sub(Str1,L1(A),1
+
:Output(1,A,sub(Str1,L1(A),1
:End
+
:End</code>
  
 
=== Shuffle Elements of List ===
 
=== Shuffle Elements of List ===
Line 113: Line 113:
 
* Destroys: LIST2
 
* Destroys: LIST2
  
:rand(dim(LIST1->LIST2
+
<code>:rand(dim(LIST1->LIST2
:SortA(LIST2,LIST1
+
:SortA(LIST2,LIST1</code>
  
 
=== Formatted Text on a Graph Screen ===
 
=== Formatted Text on a Graph Screen ===
Line 121: Line 121:
 
* Input: Str1
 
* Input: Str1
  
:ClrDraw
+
<code>:ClrDraw
:length(Str1->A
+
:length(Str1->A
:For(J,1,1+iPart(A/23
+
:For(J,1,1+iPart(A/23
:For(I,1,23
+
:For(I,1,23
:If 23J-24+I<A
+
:If 23J-24+I<A
:Text(7J-7,4I-4,sub(Str1,23J-23+I,1
+
:Text(7J-7,4I-4,sub(Str1,23J-23+I,1
:End
+
:End
:End
+
:End</code>
  
 
[http://www.cemetech.net/img/ss/40503.gif]
 
[http://www.cemetech.net/img/ss/40503.gif]
Line 139: Line 139:
 
** B = Divisor
 
** B = Divisor
  
:round(BfPart(A/B),0
+
<code>:round(BfPart(A/B),0</code>
  
 
=== Matrix to List ===
 
=== Matrix to List ===
Line 149: Line 149:
 
** Lsave
 
** Lsave
  
:ClrHome
+
<code>:ClrHome
:1→dim(L2
+
:1→dim(L2
:dim([A]→L1
+
:dim([A]→L1
:L1(1→A
+
:L1(1→A
:L1(2→B
+
:L1(2→B
:For(C,1,A
+
:For(C,1,A
:For(D,1,B
+
:For(D,1,B
:[A](C,D→L2(dim(L2)+1
+
:[A](C,D→L2(dim(L2)+1
:End
+
:End
:End
+
:End
:For(A,2,dim(L2)
+
:For(A,2,dim(L2)
:L2(A)→∟SAVE(A-1
+
:L2(A)→∟SAVE(A-1
:End
+
:End
:DelVar L1DelVar L2DelVar [A]DelVar BDelVar CDelVar ADelVar D
+
:DelVar L1DelVar L2DelVar [A]DelVar BDelVar CDelVar ADelVar D</code>
  
 
=== List to Matrix ===
 
=== List to Matrix ===
Line 170: Line 170:
 
* Output: Matrix [A]
 
* Output: Matrix [A]
  
:ClrHome
+
<code>:ClrHome
:Input "ROWS:",F
+
:Input "ROWS:",F
:ClrHome
+
:ClrHome
:dim(∟SAVE)/F→G
+
:dim(∟SAVE)/F→G
:{F,G→dim([A]
+
:{F,G→dim([A]
:1→A
+
:1→A
:1→B
+
:1→B
:G→C
+
:G→C
:0→D
+
:0→D
:Repeat A=F+1
+
:Repeat A=F+1
:For(E,1,G
+
:For(E,1,G
:D+1→D
+
:D+1→D
:∟SAVE(D→[A](A,E
+
:∟SAVE(D→[A](A,E
:End
+
:End
:A+1→A
+
:A+1→A
:C+1→B
+
:C+1→B
:C+G→C
+
:C+G→C
:End
+
:End
:DelVar ADelVar BDelVar CDelVar DDelVar EDelVar FDelVar G
+
:DelVar ADelVar BDelVar CDelVar DDelVar EDelVar FDelVar G</code>
  
 
=== Convert A to a String <nowiki>[</nowiki>Optimized] ===
 
=== Convert A to a String <nowiki>[</nowiki>Optimized] ===
 
* Author: '''Weregoose'''
 
* Author: '''Weregoose'''
  
:{A,A->L2
+
<code>:{A,A->L2
:{0,1->L1
+
:{0,1->L1
:LinReg(a+bx) Y1
+
:LinReg(a+bx) Y1
:Equ>String(Y1,Str1
+
:Equ>String(Y1,Str1
:sub(Str1,1,length(Str1)-3->Str1
+
:sub(Str1,1,length(Str1)-3->Str1</code>
  
 
=== Pop First List Element ===
 
=== Pop First List Element ===
Line 205: Line 205:
 
* Output: Ans
 
* Output: Ans
  
:ΔList(cumSum(Ans
+
<code>:ΔList(cumSum(Ans</code>
  
 
=== Rotate a 3x3 Matrix 90° ===
 
=== Rotate a 3x3 Matrix 90° ===
Line 212: Line 212:
 
* Output: Ans
 
* Output: Ans
  
:rowSwap(Ans<superscript T transpose>,1,3
+
<code>:rowSwap(Ans<superscript T transpose>,1,3</code>
  
 
=== Find List Element Index ===
 
=== Find List Element Index ===
Line 220: Line 220:
 
* Output: Ans=the position of the last occurrence of A in L1
 
* Output: Ans=the position of the last occurrence of A in L1
  
:max(seq(X(A=L1(X)),X,1,dim(L1
+
<code>:max(seq(X(A=L1(X)),X,1,dim(L1</code>
  
 
=== Find List Element Index (Within Bounds)
 
=== Find List Element Index (Within Bounds)
Line 229: Line 229:
 
* Output: Ans=The position of the last occurrence of A in the portion of L1 starting from element B and ending with element C
 
* Output: Ans=The position of the last occurrence of A in the portion of L1 starting from element B and ending with element C
  
:max(seq(X(A=L1(X)),X,B,C
+
<code>:max(seq(X(A=L1(X)),X,B,C</code>
  
 
=== Convert Base 10 to Any Base (2 to 16) ===
 
=== Convert Base 10 to Any Base (2 to 16) ===
Line 238: Line 238:
 
* Outputs: Str0
 
* Outputs: Str0
  
:"_
+
<code>:"_
:For(I,1,1+ln(A)/ln(B
+
:For(I,1,1+ln(A)/ln(B
:Ans+sub("0123456789ABCDEF",1+int(BfPart(AB^-I)),1
+
:Ans+sub("0123456789ABCDEF",1+int(BfPart(AB^-I)),1
:End
+
:End</code>
  
 
=== Change Case of a String ===
 
=== Change Case of a String ===
Line 251: Line 251:
 
** Str1 = Converted string
 
** Str1 = Converted string
  
:not(A)-->A
+
<code>:not(A)-->A
:length(Str1)->B
+
:length(Str1)->B
:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"->Str2
+
:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"->Str2
:For(N,1,B
+
:For(N,1,B
:inString(sub(Str2,26A + 1, 26),sub(Str1,N,1)
+
: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
+
:Str1 + sub(sub(Str1,N,1) +sub(sub(Str2,26,26)+sub(Str2,1,26),26A+1,26),Ans+1,1)-->Str1
:End
+
:End
:sub(Str1,B+1,B)-->Str1
+
:sub(Str1,B+1,B)-->Str1</code>
  
 
=== Number to Roman Numeral ===
 
=== Number to Roman Numeral ===
Line 267: Line 267:
 
** Ans & Str0 = Converted number
 
** Ans & Str0 = Converted number
  
:" →Str0
+
<code:" →Str0
:For(X,0,log(N
+
:For(X,0,log(N
:10fPart(.1N→Z
+
:10fPart(.1N→Z
:While Z
+
:While Z
:sum({4.1,4.1,9.2}(Z={4,5,9→T
+
:sum({4.1,4.1,9.2}(Z={4,5,9→T
:sub("IVXLCDMvxlcdmni",2X+10fPart(T)+1,1)+Str0→Str0
+
:sub("IVXLCDMvxlcdmni",2X+10fPart(T)+1,1)+Str0→Str0
:abs(Z-1-int(T→Z
+
:abs(Z-1-int(T→Z
:End
+
:End
:int(.1N→N
+
:int(.1N→N
:End
+
:End
:Str0
+
:Str0</code>
  
 
=== Find Unique Elements in Two Lists ===
 
=== Find Unique Elements in Two Lists ===
Line 285: Line 285:
 
* Conditions: "0" is not a unique element, and no element is repeated within an individual list
 
* Conditions: "0" is not a unique element, and no element is repeated within an individual list
  
:augment(L1,L2→L3
+
<code>:augment(L1,L2→L3
:L3seq((1=sum(not(L3-L3(X)))),X,1,dim(L3))→L3
+
:L3seq((1=sum(not(L3-L3(X)))),X,1,dim(L3))→L3
:SortD(L3
+
:SortD(L3
:sum(L3≠0→dim(L3
+
:sum(L3≠0→dim(L3</code>
  
 
[[Category: TI-BASIC]]
 
[[Category: TI-BASIC]]

Revision as of 15:35, 2 March 2016

Filled Circle

  • Author: Calc84Maniac
  • Inputs:
    • X = x-coordinate of center
    • Y = y-coordinate of center
    • R = radius

: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

Fraction Parts

  • Author: Calc84Maniac
  • Input:
    • Ans=a number
  • Output:
    • Ans={numerator,denominator}

:{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

Graph Mode

  • Author: Calc84Maniac
  • Inputs: none
  • Output: Ans=
    • 0:FUNC
    • 1:PAR
    • 2:POL
    • 3:SEQ
  • Destroys: GDBn

:StoreGDB n

1->Tmin
1->[thetamin]
2->nMin
ZStandard
not(Tmin)+2not([thetamin])+3(nMin=1
RecallGDB n
Delvar GDBn

Counting List Matches

  • Author: Calc84Maniac

Returns how many times A appears in LIST

:sum(LIST=A

Returns how many corresponding elements of LIST1 and LIST2 are equal Note: dim(LIST1) must equal dim(LIST2)

:sum(LIST1=LIST2

Mode of List

  • Author: Calc84Maniac
  • Inputs: LIST
  • Outputs:
    • D = mode(LIST
    • E = 1 if no unique mode, otherwise E=0

: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

Most Efficient getKey

  • Author: jonbush
  • Input: none
  • Output: the key pressed in K (or you can change to another variable)

:Repeat Ans

getkey->K
End

Text to List

  • Author: Lafferjm
  • Input: Str1, Str2 as shown
  • Output: L1

Takes text entered from input and changes it into list

:DelVar L1

"ABCDEFGHIJKLMNOPQRSTUVWXYZ? .→Str1
ClrHome
Input "NAME:",Str2
For(A,1,length(Str2
inString(Str1,sub(Str2,A,1→L1(A
End

List To Text

  • Author: lafferjm
  • Input: None
  • Output: List changed into text

Takes numbers in list from List to text and changes it into a word on the screen

:ClrHome

"ABCDEFGHIJKLMNOPQRSTUVWXYZ? .→Str1
For(A,1,dim(L1
Output(1,A,sub(Str1,L1(A),1
End

Shuffle Elements of List

Shuffles LIST1. Note: If you know what dim(LIST1) will be, replace it with a number.

  • Author: Calc84Maniac
  • Destroys: LIST2

:rand(dim(LIST1->LIST2

SortA(LIST2,LIST1

Formatted Text on a Graph Screen

Clears the graph screen

  • Authors: B-flat, KermMartian, and Calc84Maniac
  • Input: Str1

: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

[1]

Modulo Division

The Round( is in there in case of an infinitely repeating decimal (like 1/3).

  • Authors: The Tari, Something1990, and Calc84Maniac
  • Inputs:
    • A = Dividend
    • B = Divisor

:round(BfPart(A/B),0

Matrix to List

Converts any size matrix to a list

  • Author: lafferjm
  • Input:
    • Matrix A
  • Output:
    • Lsave

: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 D

List to Matrix

Takes any size list and stores it into a matrix

  • Author: Lafferjm
  • Inputs: Lsave
  • Output: Matrix [A]

: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 G

Convert A to a String [Optimized]

  • Author: Weregoose

:{A,A->L2

{0,1->L1
LinReg(a+bx) Y1
Equ>String(Y1,Str1
sub(Str1,1,length(Str1)-3->Str1

Pop First List Element

Input and output variables can be changed to whatever.

  • Author: Calc84Maniac
  • Input: Ans
  • Output: Ans

:ΔList(cumSum(Ans

Rotate a 3x3 Matrix 90°

  • Author: HaveACalc
  • Input: Ans
  • Output: Ans

:rowSwap(Ans<superscript T transpose>,1,3

Find List Element Index

  • Author: Ed H
  • Inputs:
    • A = a number
  • Output: Ans=the position of the last occurrence of A in L1

:max(seq(X(A=L1(X)),X,1,dim(L1

=== Find List Element Index (Within Bounds)

  • Inputs:
    • A = A number
    • B = Where it begins looking
    • C = Where it ends looking
  • Output: Ans=The position of the last occurrence of A in the portion of L1 starting from element B and ending with element C

:max(seq(X(A=L1(X)),X,B,C

Convert Base 10 to Any Base (2 to 16)

  • Author: RthProg
  • Inputs:
    • A = Number to convert
    • B = Base to convert to
  • Outputs: Str0

:"_

For(I,1,1+ln(A)/ln(B
Ans+sub("0123456789ABCDEF",1+int(BfPart(AB^-I)),1
End

Change Case of a String

  • Authors: RthProg and Ed H
  • Inputs:
    • A = (1) Change to uppercase
    • Str1 = String to convert
  • Outputs:
    • Str1 = Converted string

: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

Number to Roman Numeral

  • Author: Ed H
  • 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

Find Unique Elements in Two Lists

  • Author: rthprog
  • Input: L1 and L2
  • Output: L3 (All elements that are in L1 but not L2, and elements that are in L2 but not L1.)
  • Conditions: "0" is not a unique element, and no element is repeated within an individual list

:augment(L1,L2→L3

L3seq((1=sum(not(L3-L3(X)))),X,1,dim(L3))→L3
SortD(L3
sum(L3≠0→dim(L3