Reputation:
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?
Upvotes: 2
Views: 306
Reputation: 3140
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.
Upvotes: 5
Reputation: 16697
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'
Upvotes: 0