ruby 如何遍历整数的数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13091558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I iterate through the digits of an integer?
提问by Rivasa
Possible Duplicate:
Turning long fixed number to array Ruby
可能的重复:
将长固定数转换为数组 Ruby
Well, I have to iterate over the digits of a integer in Ruby. Right now I was just splitting it up into an array, and then iterating over that. However I was wondering if there was a faster way to do this?
好吧,我必须在 Ruby 中迭代整数的数字。现在我只是将它拆分为一个数组,然后对其进行迭代。但是我想知道是否有更快的方法来做到这一点?
回答by tokland
The shortest solution probably is:
最短的解决方案可能是:
1234.to_s.chars.map(&:to_i)
#=> [1, 2, 3, 4]
A more orthodox mathematical approach:
更正统的数学方法:
class Integer
def digits(base: 10)
quotient, remainder = divmod(base)
quotient == 0 ? [remainder] : [*quotient.digits(base: base), remainder]
end
end
0.digits #=> [0]
1234.digits #=> [1, 2, 3, 4]
0x3f.digits(base: 16) #=> [3, 15]
回答by meagar
You can use the old trick of modulus/divide by 10, but this won't be measurably faster unless you have hugenumbers, and it will give the digits to you backwards:
您可以使用模数/除以 10 的老技巧,但除非您有大量数字,否则这不会更快,并且它将向后提供数字:
i = 12345
while i > 0
digit = i % 10
i /= 10
puts digit
end
Output:
输出:
5
4
3
2
1
回答by robertodecurnex
split=->(x, y=[]) {x < 10 ? y.unshift(x) : split.(x/10, y.unshift(x%10))}
split.(1000) #=> [1,0,0,0]
split.(1234) #=> [1,2,3,4]
回答by steenslag
Ruby has divmod, which will calculate both x%10and x/10in one go:
Ruby有divmod,这将同时计算x%10并x/10一气呵成:
class Integer
def split_digits
return [0] if zero?
res = []
quotient = self.abs #take care of negative integers
until quotient.zero? do
quotient, modulus = quotient.divmod(10) #one go!
res.unshift(modulus) #put the new value on the first place, shifting all other values
end
res # done
end
end
p 135.split_digits #=>[1, 3, 5]
For things like Project Euler, where speed is of some importance, this is nice to have. Defining it on Integer causes it to be available on Bignum too.
对于像 Project Euler 这样的东西,速度很重要,这很好。在 Integer 上定义它会使其在 Bignum 上也可用。
回答by Patrick Oscity
I like Enumerator goodness. I wrote this code for a project of mine:
我喜欢 Enumerator 的优点。我为我的一个项目编写了这段代码:
class Integer
def digits
Enumerator.new do |x|
to_s.chars.map{|c| x << c.to_i }
end
end
end
This gives you access to all the good Enumerator stuff:
这使您可以访问所有优秀的 Enumerator 内容:
num = 1234567890
# use each to iterate over the digits
num.digits.each do |digit|
p digit
end
# make them into an array
p num.digits.to_a # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
# or take only some digits
p num.digits.take(5) # => [1, 2, 3, 4, 5]
# you can also use next and rewind
digits = num.digits
p digits.next # => 1
p digits.next # => 2
p digits.next # => 3
digits.rewind
p digits.next # => 1
回答by Mohamed Nuur
Try mod by 10 (will give you the last digit), then divide by 10 (will give you the rest of digits), repeat this until you're down to the final digit. Of course, you'll have to reverse the order if you want to go through the digits from left to right.
尝试 mod 乘以 10(会给你最后一个数字),然后除以 10(会给你剩下的数字),重复这个直到你得到最后一个数字。当然,如果您想从左到右遍历数字,则必须颠倒顺序。

