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:
Optimization #1:
Used only one list, inputted list had to be as Ans, and outputted list is as Ans, and as L1.
Code:
Optimization #2:
Requires no lists, inputted list is Ans, and outputted list is Ans, and requires variables A and B.
Code:
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:
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