List & Matrix One Liners

This page is dedicated to showcase small snippets of code may be useful. These small routines are designed to accomplish tasks involving lists, matrices, and their attributes. Unless specified, output is in Ans.

Index of Maximum List Element – jonbush

This line finds the index of first occurrence of a specific value in a list.
The list to be searched is in Ans at the beginning.

1+sum(not(cumSum(Ans=max(Ans

This can also be used to find the index of the minimum or of any other value in place of "max(Ans"

Number of Elements Less than X – kg583

This line counts the number of elements in a list less than some number X.
The list to be searched is in Ans.

sum(0<Ans-X

Replace < with > to return the number of elements greater than X, ≤ for numbers less than or equal to X and ≥ for numbers greater than or equal to X.

Shuffle a list

This routine is the fastest way to shuffle a list. L₁ is the list to be shuffled and L₂ is a temporary list used to do the shuffling. Make sure to clean up L₂ after your program.

:rand(dim(L₁->L2
:SortA(L2,L₁

Reverse a list – Xeda Elnara

This routine is the smallest way to reverse a list. L₁ is reversed without affecting Ans.

:dim(L₁
:seq(L₁(Ans-K),K,0,Ans-1

Remove list element – jonbush

This routine is the smallest way to remove a single element from a list. The position of the element to be removed from L₁ is in Ans, and the result is a list with that element removed.

:seq(L₁(A+(A≥Ans)),A,1,dim(L₁)-1

.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Noncommercial-No Derivative Works 2.5 License.