How to split a number into your digits in APL
In APL, how can I split an integer or number by a vector containing its digits? What's the shortest (shortest) way to do this?
You can use inversion Decode
with base 10:
10β₯β£Β―1β’
since it Decode
will take as many digits as possible and decode them, its inverse will take a number and encode it for as many digits as needed,
or, with βIOβ0
, you can try to find the indices of the formatted number inside a vector of digits:
βDβ³β
Demo for both solutions.
This is better than uglier use Encode
with custom length obtained by forming an array of 10 in length log ten for input:
{β΅β€β¨10β΄β¨β10β1+β΅}
I did it in APL2 by first applying FORMAT and then EXECUTE EACH (although it can be limited to positive integers):
βΒ¨β
Try it online!
Not the shortest, but the strength for this was in the earliest APL. The 1962 book shows how to work with positional number systems using only basic functions and matrix multiplication: