I spent a significant amount of time making a code that would organize a player's hand, for a card game I'm working on atm, and went through about 4 different versions, each optimized upon the previous, until I found the SortA( command.

Upon request by _player1537, below are the codes I made. The format of the lists were that the values of their (constant) 13 elements would be from 1-13 for Ace to King, where suites don't matter. At most there would be 4 of a single number, for obvious reasons.

The first is 13 lines long, requires 2 lists, and 2 variables. Inputted list has to be in L2.

Code:
:SetUpEditor L1
:13→dim(L1
:Fill(0,L1
:For(A,1,13
:L2(A
:1+L1(Ans→L1(Ans
:End
:For(A,1,13
:For(B,1,L1(A
:A→L2(17-sum(not(L2
:End
:End
:DelVar L1

Optimization #1:
Used only one list, inputted list had to be as Ans, and outputted list is as Ans, and as L1.

Code:
:seq(sum(A=Ans),A,1,13→L1
:{0
:For(A,1,13
:If L1(A
:augment(Ans,seq(A,B,1,L1(A
:End
:seq(Ans(A),A,2,14→L1

Optimization #2:
Requires no lists, inputted list is Ans, and outputted list is Ans, and requires variables A and B.

Code:
:seq(sum(A=Ans),A,1,13
:For(A,1,13
:augment(Ans,seq(A,B,1,Ans(A
:End
:seq(Ans(A),A,14,26

Final Optimization:
Same requirements as before, but one less line of code, as it cleans up what parts of the list it doesn't need anymore while it goes, instead of all at once on the last line of code. Not sure if this is faster or smaller, but I liked that it was cleaning itself up while it went instead of putting it off to the end.

Code:
:seq(sum(A=Ans),A,1,13
:For(A,1,13
:augment(seq(Ans(B),B,2,dim(Ans)),seq(A,B,1,Ans(A
:End
Very nice Mufin! How fast does it run compared to SortA( out of curiosity?
Haven't tested any of them out yet.

Well, turns out seq( doesn't like to work the same way as For(, and it gets mad at you if your parameter that's supposed to be higher than the other, is lower than the other, along with the whole cleaning itself up as it goes didn't want to work well with the rest of the code, so I'm left with a sort of mix between the 3 optimizations, but we'll see how fast it compares with the actual command.

Right now, I'm timing it against SortA( each at 10,000 instances, using the same list, inputted the same way, and both being called into their own subprograms.

Just as predicted, there is no competition. My subprogram averaged at .882 seconds, and the command averaged at .014 seconds. Also SortA( will automatically store the value, so I'm just gonna stick with SortA(.
  
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