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?

有帮助吗?

解决方案 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'

其他提示

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@":)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top