[BASIC] List routine for eliminating values

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

Moose
New Member
Posts: 29
Joined: Sun 02 Sep, 2007 6:48 am
Location: Shhhhh, they'll hear me!

[BASIC] List routine for eliminating values

Post by Moose »

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
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

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
Moose
New Member
Posts: 29
Joined: Sun 02 Sep, 2007 6:48 am
Location: Shhhhh, they'll hear me!

Post by Moose »

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 :wink:

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 :wink:
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

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
Moose
New Member
Posts: 29
Joined: Sun 02 Sep, 2007 6:48 am
Location: Shhhhh, they'll hear me!

Post by Moose »

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
Thats a good idea, I'll give it a try...
User avatar
kalan_vod
Calc King
Posts: 2932
Joined: Sat 18 Dec, 2004 6:46 am
Contact:

Post by kalan_vod »

Placing it in a loop would be a little slow, but math programs are easier in basic than asm..You just have to decide speed vs. size, if you want to have speed just make it all in asm ;)
User avatar
Art_of_camelot
Regular Member
Posts: 124
Joined: Sun 09 Sep, 2007 8:50 pm
Location: The dark side of the moon
Contact:

Post by Art_of_camelot »

hmm... you could check it at the time the variable is stored to the list, but it would still require a For loop to run through the list. This would work only if the elements of the list are stored one at a time.
Projects:
Projects:TBA-Soonish. :)
Updated 5/3/12
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

Checking whether a given element is in a list does not require a For loop
example:

Code: Select all

If max(L1=A)
(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)
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

I think it's hybrid... max() seriously degenerates if you have a bunch of elements (say, 200). In assembly this would still be lightning fast but for some idiot reason it isn't...
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

I tested it on 999 elements..
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

Nah, I was thinking of another function.
Btw, have you tested it with a list filled with zeros or random numbers?
King Harold
Calc King
Posts: 1513
Joined: Sat 05 Aug, 2006 7:22 am

Post by King Harold »

1 through 999
compared with 8
User avatar
driesguldolf
Extreme Poster
Posts: 395
Joined: Thu 17 May, 2007 4:49 pm
Location: $4080
Contact:

Post by driesguldolf »

Nevermind, I mistook it with the string functions.
User avatar
Art_of_camelot
Regular Member
Posts: 124
Joined: Sun 09 Sep, 2007 8:50 pm
Location: The dark side of the moon
Contact:

Post by Art_of_camelot »

Checking whether a given element is in a list does not require a For loop
Ah, I wasn't familar with the "max" command. That's actually fairly useful, especially since it's faster.
Projects:
Projects:TBA-Soonish. :)
Updated 5/3/12
User avatar
kalan_vod
Calc King
Posts: 2932
Joined: Sat 18 Dec, 2004 6:46 am
Contact:

Post by kalan_vod »

Might I suggest looking at this site, it has useful information for the beginner and advanced user.
Post Reply