【发布时间】:2012-10-16 05:22:25
【问题描述】:
ruby 中有没有一种方法可以将74239 之类的fixnum 转换为[7,4,2,3,9] 之类的数组?
【问题讨论】:
-
mbds 的回答,使用 Ruby 2.4 中引入的
digits方法,让其他的都过时了。
ruby 中有没有一种方法可以将74239 之类的fixnum 转换为[7,4,2,3,9] 之类的数组?
【问题讨论】:
digits 方法,让其他的都过时了。
也许不是最优雅的解决方案:
74239.to_s.split('').map(&:to_i)
输出:
[7, 4, 2, 3, 9]
【讨论】:
.map(&:to_i) 让它更优雅一点?
-23.to_s.split('').map(&:to_i) 会发生什么?
74239.to_s.chars 似乎比74239.to_s.split('') 更合适,尤其是与.map(&:to_i) 一起使用时:)
each_char 会更合适,因为它可以避免chars 创建的中间Array。
.chars 目前在枚举器中,而不是数组。
你不需要为了这种事情来回穿越字符串土地:
def digits(n)
Math.log10(n).floor.downto(0).map { |i| (n / 10**i) % 10 }
end
ary = digits(74239)
# [7, 4, 2, 3, 9]
这确实假设n 是肯定的,当然,如果需要,将n = n.abs 加入混合可以解决这个问题。如果你需要覆盖非正值,那么:
def digits(n)
return [0] if(n == 0)
if(n < 0)
neg = true
n = n.abs
end
a = Math.log10(n).floor.downto(0).map { |i| (n / 10**i) % 10 }
a[0] *= -1 if(neg)
a
end
【讨论】:
divmod 方法可用于一次提取一个数字
def digits n
n= n.abs
[].tap do |result|
while n > 0
n,digit = n.divmod 10
result.unshift digit
end
end
end
快速基准测试表明,这比使用 log 提前查找位数更快,这本身比基于字符串的方法更快。
bmbm(5) do |x|
x.report('string') {10000.times {digits_s(rand(1000000000))}}
x.report('divmod') {10000.times {digits_divmod(rand(1000000000))}}
x.report('log') {10000.times {digits(rand(1000000000))}}
end
#=>
user system total real
string 0.120000 0.000000 0.120000 ( 0.126119)
divmod 0.030000 0.000000 0.030000 ( 0.023148)
log 0.040000 0.000000 0.040000 ( 0.045285)
【讨论】:
您可以转换为字符串并使用chars方法:
74239.to_s.chars.map(&:to_i)
输出:
[7, 4, 2, 3, 9]
比拆分要优雅一点。
【讨论】:
从 Ruby 2.4 开始,整数 (FixNum is gone in 2.4+) 有一个内置的 digits 方法,可以将它们提取到数字数组中:
74239.digits
=> [9, 3, 2, 4, 7]
如果你想保持数字的顺序,只需链接reverse:
74239.digits.reverse
=> [7, 4, 2, 3, 9]
文档:https://ruby-doc.org/core-2.4.0/Integer.html#method-i-digits
【讨论】:
您也可以使用Array.new 代替map:
n = 74239
s = Math.log10(n).to_i + 1 # Gets the size of n
Array.new(s) { d = n % 10; n = n / 10; d }.reverse
【讨论】:
在 Ruby 2.4 中,整数将具有 digits method。
【讨论】: