[BASIC] List routine for eliminating values
Moderator: MaxCoderz Staff
[BASIC] List routine for eliminating values
Hello Does anyone know if there is a routine for eliminating identical values from a list:
Turning this:
{1,2,2,2,3,3,4,5,6
into {1,2,3,4,5,6
and this:
{1,2,3,4,5,6,2
into
{1,2,3,4,5,6
Turning this:
{1,2,2,2,3,3,4,5,6
into {1,2,3,4,5,6
and this:
{1,2,3,4,5,6,2
into
{1,2,3,4,5,6
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
I know a way, but it is rather inefficient
For each element, add it to a new list unless it is already in there (which can be checked with something like If not(max(LIST=newval) Then add End)
If your lists were strictly sorted it would be easier.. Sorting it first and using a better routine ("If not(last=new) Than add End") may be better, give it a try?
Of course someone else knows a good trick so I'd wait for them
ps: the code given is pseudo-basic and obviously you'll have to rename the variables: last could be L1(dim(L1)) for example
For each element, add it to a new list unless it is already in there (which can be checked with something like If not(max(LIST=newval) Then add End)
If your lists were strictly sorted it would be easier.. Sorting it first and using a better routine ("If not(last=new) Than add End") may be better, give it a try?
Of course someone else knows a good trick so I'd wait for them
ps: the code given is pseudo-basic and obviously you'll have to rename the variables: last could be L1(dim(L1)) for example
Thanks, I was temporarily obsessed with making the fastest polynomial solver I could imagine (no loops, just a crap load of list operations)
In he end I got the right answers (and then I got more of those right answers in the same list in a different order), but I didn't want to ruin it with a for( loop at the end
Basically when I typed in:
POLY
order=3
(clrhome)
a3^3+...+a0=0
a3=1
a2=0
a1=-7
a0=-6
and It gave me something like
{-3,-3,2,1,1,1}
BTW if you haven't noticed I'm a big fan of the ti 86
In he end I got the right answers (and then I got more of those right answers in the same list in a different order), but I didn't want to ruin it with a for( loop at the end
Basically when I typed in:
POLY
order=3
(clrhome)
a3^3+...+a0=0
a3=1
a2=0
a1=-7
a0=-6
and It gave me something like
{-3,-3,2,1,1,1}
BTW if you haven't noticed I'm a big fan of the ti 86
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
Thats a good idea, I'll give it a try...King Harold wrote:I don't see a way around a loop here.. (unless you'd use recursion but that's a bit loopy - and that's not something you'd want to do in BASIC)
You could write an assembly program to do it for you, it will also loop but an assembly loop is much faster than a BASIC loop
- Art_of_camelot
- Regular Member
- Posts: 124
- Joined: Sun 09 Sep, 2007 8:50 pm
- Location: The dark side of the moon
- Contact:
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
Checking whether a given element is in a list does not require a For loop
example:
(tested on my TI84 to be approx. 63 times as fast as a for loop over the same elements)
The problem is that when you have the entire list already, you'll have to do this for every element. Max has a loop inside it of course, but that's hopefully an assembly loop (I hope TI didn't decide to build some of their functions on top of other BASIC functions.. that would be terribly inefficient)
example:
Code: Select all
If max(L1=A)
The problem is that when you have the entire list already, you'll have to do this for every element. Max has a loop inside it of course, but that's hopefully an assembly loop (I hope TI didn't decide to build some of their functions on top of other BASIC functions.. that would be terribly inefficient)
- driesguldolf
- Extreme Poster
- Posts: 395
- Joined: Thu 17 May, 2007 4:49 pm
- Location: $4080
- Contact:
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
- driesguldolf
- Extreme Poster
- Posts: 395
- Joined: Thu 17 May, 2007 4:49 pm
- Location: $4080
- Contact:
-
- Calc King
- Posts: 1513
- Joined: Sat 05 Aug, 2006 7:22 am
- driesguldolf
- Extreme Poster
- Posts: 395
- Joined: Thu 17 May, 2007 4:49 pm
- Location: $4080
- Contact:
- Art_of_camelot
- Regular Member
- Posts: 124
- Joined: Sun 09 Sep, 2007 8:50 pm
- Location: The dark side of the moon
- Contact:
Might I suggest looking at this site, it has useful information for the beginner and advanced user.