遍历整数数字数组内 [英] Iterate over digits of integers inside of array

查看:154
本文介绍了遍历整数数字数组内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code以下:

I have this code below:

 a = [435,276,434]

 def product(a)
   final = 1
   for e in a
     for p in a[e]
       final*=p
     end
   end
   final
 end

 puts product(a)

我不知道我怎么可以在这个数组迭代,其中两次的结果是4 * 3 * 5 = 60,2 * 7 * 6 = 85,以及4 * 3 * 4 = 48

I'm wondering how I can iterate over this array twice where the result is 4*3*5 = 60, 2*7*6 = 85, and 4*3*4 = 48

我写了上面,我以为会在这里做的伎俩一些code了,但红宝石不断返回一个错误。

I wrote some code up above which I thought would do the trick here but Ruby keeps on returning an error.

推荐答案

的几点考虑:

在Ruby中,你基本上都不使用循环遍历的事情。 #each 更好。你传递一个块的是,它给你的各种灵活的余地和前pressiveness。

In Ruby you basically never use a for loop to iterate over things. #each is better. You get to pass a block to that, which gives you all kinds of room for flexibility and expressiveness.

此外,你不能 - 正常 - 遍历一个整数。请记住,一个整数数值的存储,而不是一个特定的再presentation,所以它必须依赖你想重新presentation的基础上。如果你想要一个的字符串的字符,每一个碰巧是数字,好了,你猜怎么着?你想要一个字符串,像SEPH的解决方案在这里。或阵列,这很可能会做得更有道理,因为每个整数将保持一个整数,就不必被解析来回。

Also, you can't - normally - iterate over an Integer. Remember that an Integer is a store of numerical value, not a particular representation, so it would have to be dependent on the base of the representation you wanted. If you want a string of characters, each of which happen to be numbers, well, guess what? You want a String, like seph's solution here. Or an Array, which would probably make even more sense because each integer would remain an integer and wouldn't have to be parsed back and forth.

告诉你吧,让我们建立一个非常酷的方法来做到这一点,这个主食到整数,并希望演示一些Ruby的很酷的功能。

Tell you what, let's build a really cool method to do this and staple this on to Integer, and hopefully demonstrate some of Ruby's cool features.

class Integer
  include Enumerable
  def each(base = 10, &blk)
    front, back = abs.divmod(base)
    if front > 0
      front.each(base, &blk)
    end
    yield back
  end
end

此数量少需要一个碱和块,得到整数的绝对值(因为技术上减去不是数字),然后使用divmod分裂数目,斩去最终数字。我们存放在正面和背面的作品。我们检查,看看是否有任何更多的数字,由前为0表示,如果有就是我们递归调用此方法,用该块。然后,我们刚刚得到回,发送数字到块。

This little number takes a base and a block, gets the absolute value of the integer (since technically the minus isn't a digit), then uses divmod to split the number, chopping off the final digit. We store the pieces in front and back. We check to see if there are any more digits, indicated by front being 0, and if there is we recursively call this method, with that block. Then we just yield the back, sending the digit to the block.

由于我们现在已经定义了一个每个方法,我们现在可以自由地包括可枚举这会让我们一吨的东西!

Since we have now defined an each method, we are now free to include Enumerable which gets us a ton of stuff!

只要该修改是积极的,你的产品方法就变成了:

As long as that modification is active, your product method becomes:

(如果你想打印60 84 48): a.map {| N | n.reduce(*)}

(if you wanted to print 60 84 48): a.map {|n| n.reduce(:*)}

(或者,如果你想打印241920): a.reduce(*)减少。(*)

(or if you wanted to print 241920): a.reduce(:*).reduce(:*)

pretty不错!

所以,这个整体解决方案是相当长的时间比SEPH的单行,和真理,如果我需要真正做一些事情我只想 to_s 。是我的解决方案更快地执行呢?谁知道?这当然是更前pressive,虽然,这就是为什么你会在第一时间使用Ruby。

So, this total solution is quite a bit longer than seph's one-liner, and in truth if I needed to actually do something I would just to_s. Is my solution quicker to execute? Who knows? It's certainly more expressive, though, and that's why you're using Ruby in the first place.

如果你想解决一个问题,是的,绝对的, to_s 。但是,如果你希望你的code到前preSS哲学您对数字,他们如何真的只是太收藏 - 他们是在一个奇怪的集理论的一种方式,红宝石让你赋予他们权力要做好自己。而这样一来,不需要串可言,他们是完全免费的吝啬援助。你可以通过不同的基础迭代,如果你正在做十六进制或二进制,其中preserves更多他们的numbery本质是超级有用。

If you want to solve a problem, yeah, absolutely, to_s. But if you want your code to express a philosophy you have about numbers, about how really they're just collections too - and they are, in a weird set theory kind of way, Ruby lets you empower them to be that. And this way that doesn't need Strings at all, they're totally free of their grudging assistance. And you can iterate through different bases, which is super useful if you're doing hex or binary, which preserves more of the numbery essence of them.

在这个世界上,你和我共同建立,贾马尔,小整数贯穿的大男孩森林野生。这是美好的。

In this world that you and I have built together, Jamaal, little integers run wild through the forests with the big boys. And that's wonderful.

这篇关于遍历整数数字数组内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆