Search code examples
arrayslistti-basic

Testing if value is in list/array (Ti-Basic)


Is there a way to test if a value is in a list? In Python, I think you can do something like 'if n in myList: print("Value N is in the list.")'

I don't want to use a for loop to check each value seperately, unless it's the only option. I'm using a Ti-84 Plus.


Solution

  • This should work assuming I've thought through it correctly. It's very simple, where L₁ is the list to search and X is the value to look for.

    max(not(L₁-X
    

    Step-by-step analysis:

    1. L₁-X: Subtract the value from everything in the list. Now, if this list contains a zero, it means our value was in L₁.
    2. not(L₁-X: Invert everything in the list. This converts all zeroes to ones, and everything else to zeroes. Now, if this list contains a one, it means our value was in L₁. If the list is all zeroes, it was not.
    3. max(not(L₁-X: Get the maximum value in the list. As stated above, the list will be all zeroes if the value was not inside L₁, so the maximum value will be zero. If L₁ had the value inside, the maximum will be a one.

    This makes a check as simple as this:

    If max(not(L₁-X
    Disp "The value was found:",X