I know I wrote it, but this routine sucks. In terms of big-oh efficiency, it takes O(n2) operations on an n-element list, in the worst case. There's gotta be a better way to do this (and it probably relies on clever use of built-in commands, because accessing each element one-by-one is slow).
Date: 05 Apr 2008 21:18
Number of posts: 4
RSS: New posts
Even worse, the following routine is faster. And since the following routine is also disgusting code, I don't want to put it into the article.
This routine works by first creating a list of the same size that's entirely 0, then comparing it to the original list (getting a list of true and false), turning the result into a string, searching for the letter "f" in the string, and converting the index into an index for the list by division. Ugh!
Edit: a slightly slower but more intuitive method:
:firstnz3(list) :Func :Local i :For i,1,dim(list) : If list≠0 : Return i :rotate(list)→list :EndFor :0 :EndFunc
Edit 2: my fastest routine so far (still quite ugly, but less so) is
You know I don't know 68k Basic, but here's something you can possibly toy with:
:1/max((Ans xor 0)/seq(I,I,1,dim(Ans
By swapping out the 0 for a 1, this quickly turns into a routine that finds the first "zero" element.