I have been woking on a routine that removes an element from anywhere in a list and appending it to the end of the list.
Input: Ans=number to move to the end of the list
Output: Ans=list with moved element
Destroyed: three real variables and modifies the list


This code prevents the addition of elements that are not already in the list.

Code:
Ans->A
dim(ʟQ->Q
max(seq(X(A=ʟQ(X)),X,1,Q→B
If Ans=1:DeltaList(cumSum(ʟQ→ʟQ
If B≥2 and B≤Q-1:augment(seq(ʟQ(X),X,1,Ans-1),seq(ʟQ(X),X,Ans+1,Q→ʟQ
If B≠0:Q-1→dim(ʟQ
ʟQ
If B≠0:augment(Ans,{A→ʟQ


Are there any optimizations? Is there a better way to do this? Could SortA() be used instead?
If this is the most efficient method, then I may post this in the Code Fragments and Routines topic.
List operations are fun to implemenet, because there's often an elegant, one-line solution to the problem. Or at least, one-line.

The following line moves the Ans-th element of ʟQ to the end of the list, retaining the order of all other elements.

Code:
augment(seq(ʟQ(X+(X≥Ans)),X,1,dim(ʟQ)-1),{ʟQ(Ans→ʟQ
Runer112 wrote:
List operations are fun to implemenet, because there's often an elegant, one-line solution to the problem. Or at least, one-line.

The following line moves the Ans-th element of ʟQ to the end of the list, retaining the order of all other elements.

Code:
augment(seq(ʟQ(X+(X≥Ans)),X,1,dim(ʟQ)-1),{ʟQ(Ans→ʟQ


I've been meaning to ask this before, but you did it too so I might as well ask it here: what does this bit do? (off-topic btw, sorry)


Code:
(X+(X≥Ans))

is it a shorthand conditional statement or something? I'd have googled it but I have no idea what to search for.
Thanks Runner112! I did not know that you could create a list while omitting an element.

I did not clarify that I first searched for the number and moved that element. To perfom the same task as my code, here is your method modified:

Code:
max(seq(X(Ans=ʟQ(X)),X,1,dim(ʟQ
If Ans≥1 and Ans≤dim(ʟQ:augment(seq(ʟQ(X+(X≥Ans)),X,1,dim(ʟQ)-1),{ʟQ(Ans→ʟQ


M. I. Wright: It is indeed a conditional statement. Here is some information about this technique.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement