find sublist within larger list on a TI-84+

96 Views Asked by At

I'm making snake using the TI basic on my calculator, and that works well and all. the sublist would always be 2 long.

I am aware of:

max(not(L1-X))

but this only checks for 1 value and I need to check for the sublist {X,Y} not just X...

the issue is, the larger you get, the slower the snake moves, because the list can get over 100 characters long. the code that I'm using:

:0 -> T
:For(I,2,dim(L1)-2,2):
:(L1(I-1)=X)(L1(I)=Y) + T -> T
:if T ≥ 1:Then:1->P:End
2

There are 2 best solutions below

0
Vaelus On

You can store coordinates as complex numbers instead of pairs of adjacent numbers. Complex numbers intrinsically represent 2-dimentional values, so many of their operations map nicely to operations on 2D coordinates. For example instead of storing three coordinates (1, 1), (1, 2), (2, 2) as {1,1,1,2,2,2}, store them as {1+1i,1+2i,2+2i}.

To convert coordinates to complex numbers, you just need to use the imaginary unit, i, like X+Yi.

To extract the coordinates from a list element I, you can take the real and imaginary parts of the element, real(L1(I))→X and imag(L1(I))→Y.

You can check for inclusion of a coordinate in a list of complex numbers using a modified version of the trick you mentioned, max(not(abs(L1-(X+Yi)))). abs takes the magnitude of each complex number in the list L1-(X+Yi) which will be zero if and only if the components of the complex number are X and Y.

0
Vaelus On

This answer answers the title question, however I think it is inferior to my answer using complex numbers for the full question.

You can find the index of an element in a list using seq and SortD. Then you can use seq again to get a sublist and compare with = and min.

For example:

:L₁=X→ʟPRED
:If max(ʟPRED):Then
:seq(I,I,1,dim(L₁),1)→ʟINDEX
:SortD(ʟPRED,ʟINDEX)
:ʟINDEX(1)→I
:seq(L₁(J),J,I,I+1,1)→ʟSUBL
:If min(ʟSUBL={X,Y}):Then
:"Do stuff"
:EndIf
:EndIf