Question

I use the programming language: J.

I want to put all of the digit of a number in a list.

From:

12345

to:

1 2 3 4 5

What can I do?

Was it helpful?

Solution 3

Another approach:

intToList =: 3 : '((>. 10 ^. y)#10) #: y'

This doesn't convert to string and back, which can be potentially costly, but counts the digits with a base-10 log, then uses anti-base (#:) to get each digit.

EDIT:

Better, safer version based on Dan Bron's comment:

intToList =: 3 : '10 #.^:_1 y'

OTHER TIPS

The way I'd write this is

   10&#.^:_1 

which we can see in use with this sentence:

   (10&#.^:_1) 123456789 
1 2 3 4 5 6 7 8 9

That program relies on the reshaping built in to Base. It uses the (built-in) obverse of Base as a synonym for Antibase.

I found the answer:

intToList =: (".@;"0@":)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top