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

From Learn @ Cemetech
Jump to navigationJump to search
m (Updated code for "Find and Replace Text in String")
 
Line 320: Line 320:
 
<code>
 
<code>
 
:length(Str2→J
 
:length(Str2→J
:While inString(Str1,Str2
+
:1→I
:inString(Str1,Str2→I
+
:While inString(Str1,Str2,I
 +
:inString(Str1,Str2,I→I
 
:Str3
 
:Str3
 
:If ln(I
 
:If ln(I
Line 328: Line 329:
 
:Ans+sub(Str1,I+J,1-I-J+length(Str1
 
:Ans+sub(Str1,I+J,1-I-J+length(Str1
 
:Ans→Str1
 
:Ans→Str1
 +
:I+length(Str3→I
 
:End
 
:End
 
</code>
 
</code>
 
[[Category: TI-BASIC]]
 
[[Category: TI-BASIC]]

Latest revision as of 07:22, 11 February 2023

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



  • Author: PT_
  • Inputs:
    • N = Number to convert
    • A = Base to convert to
  • Outputs: List with 8 decimals in base-A

:AfPart(1/Aint(NA^seq(X,X,~7,0

or

:remainder(int(NA^seq(X,X,~7,0)),A

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

:" →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

  • Authors: rthprog/lirtosiast
  • 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

Ansseq(1=sum(Ans=Ans(X)),X,1,dim(Ans->L3
SortD(L3
sum(L3≠0→dim(L3

Detect Screen Resolution

  • Author: Weregoose
  • Inputs: none
  • Output: Ans=
    • 1: Monochrome
    • 0: Color

:Xmin+E2ΔX>Xmax

Find and Replace Text in String

  • Author: Michael2_3B, optimized by Runer112
  • Inputs: Str1 (main string), Str2 (search string), Str3 (replacement string)
  • Outputs: Str1 (modified main string)

length(Str2→J
1→I
While inString(Str1,Str2,I
inString(Str1,Str2,I→I
Str3
If ln(I
sub(Str1,1,I-1)+Ans
If I+J≤length(Str1
Ans+sub(Str1,I+J,1-I-J+length(Str1
Ans→Str1
I+length(Str3→I
End