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?

+3


source to share


3 answers


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+⍡}

      

+4


source


I did it in APL2 by first applying FORMAT and then EXECUTE EACH (although it can be limited to positive integers):

βŽΒ¨β•

      



Try it online!

+3


source


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:

enter image description here

+1


source







All Articles