List a number's digits in J

断了今生、忘了曾经 提交于 2019-12-24 00:52:32

问题


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?


回答1:


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'



回答2:


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.




回答3:


I found the answer:

intToList =: (".@;"0@":)


来源:https://stackoverflow.com/questions/17677142/list-a-numbers-digits-in-j

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!