Is there a way to find the position of the first of a number in a list? Like inString for a list, so this code would return 4 looking for 5 in {0,1,6,5,5}. I can think of using a for statement, but it seems like there should be a faster way. Any ideas?

this requires O(n) time, aka a for loop is the best/only way to do this. sorting wont get it done. binary search wont either

Visit Calccrypto for info on crypto

KC2ZOF

A For( loop is the best/only way to do this? You sure about that? I need to start handing out trophies for the Wrong Answer of the Week.

Use `1/max((L1=N)/cumSum(not(0L1`.

Edit: Since that would return an error if N isn't in the list:

```
:max((L1=N)/cumSum(not(0L1
:If Ans:Ansֿ¹
```

This produces a 0 if N isn't found, like inString( does.

i guess i shouldnt have said 'only' way, because there's bound to be many ways to do anything.

sorry for not knowing about shortcuts in tibasic. its been some time since i last used tibasic, but smaller code is not necessarily better, even on calcs. if i am not mistaken, your code would run through L1 at least 3 times: cumSum iterates through all of L1. you then divide the list, causing the calc to loop through it again. then you use max, looping through it again. so why not have 1 for loop? it should be faster

Visit Calccrypto for info on crypto

KC2ZOF

First of all: `seq(X,X,1,256→L1`

If you use a `For(` loop, the worst case here is if N=256; the best case is N=1. Agreed?

I'll concede that the best case is instantaneous. So, let's go with the worst case.

```
:256→N
:startTmr→T
:For(X,1,dim(L1
:If N=L1(X
:Goto 0
:End
:Return
:Lbl 0
:Disp X
:checkTmr(T
```

(Normally, I'd code that differently, but this seemed to be what you were going towards. If it's wrong, let me know.)

This takes 5-6 seconds on my 84+SE. If I were to write a second colon before the condition, it would fix a delay-inducing bug, and shorten the time to 3-4 seconds.

The alternative:

```
:256→N
:startTmr→T
:max((L1=N)/cumSum(not(0L1
:If Ans:Ansֿ¹
:Disp Ans
:checkTmr(T
```

…takes 0-1 seconds. (This time is a constant for this list's dimension, no matter our choice of N.)

If we bump the list up to 512 elements, the worst-case for the (fixed) `For(` loop now takes over 7 seconds (non-fixed is 18), while the alternative takes 1-2. Needless to say, your hypothesis doesn't stand up under the weight of testing. If you want to apply big-O notation as a general rule, you'd have to be fully aware of what's happening under the hood, and I think that most of us aren't.

Actually, i posted another solution to this a few days ago on UTI for an unrelated topic :D

Here was what I came up with:

`1+sum(not(cumSum(N=L1`

N is the number to search for, so how does that work?

If the list is p elements and N is not in the list, p+1 will be returned, though.

Z80 Assembly>English>TI-BASIC>Python>French>C>0

Do these work when N appears multiple times in the list?

Also, if you just want to check if a value is in the list, max(N=L1 would work.

Sunrise 3 Progress: 30%

Size: around 20 KB, not including the save lists and in-game RAM.

Yes, my code works if there are multiple elements of N :) And using max( is a good way to check if the value is in the list! I typically use sum( for that because I didn't use max( or min( for the first few years of BASIC programming XD Old habits die hard, I guess… Also, max( is probably faster.

Z80 Assembly>English>TI-BASIC>Python>French>C>0