【问题标题】:Ruby index of array elements数组元素的 Ruby 索引
【发布时间】:2019-01-14 10:50:44
【问题描述】:

假设我有一个整数元素数组。我试图在其中找到一长串重复数字开始的索引。

my_array = [100, 101, 100, 102, 100, 100, 101, 100, 250, 251, 253, 260, 250, 200, 100, 100, 100, 100, 100, 100, 100, 100, 100, 120]

波纹管是我试图找到索引的方式。任何人都可以建议我更优化和正确的方法吗?

my_array.each with_index do |e, x|
  match = e.to_s * 5
  next_10 = my_array[x + 1, 5].join()

  if match == next_10
    puts "index #{x}"
    break
  end
end

#index 14

【问题讨论】:

  • 你对数组标准化的定义是什么?
  • @ray 相同的元素开始出现。
  • @Aparichith “相同的元素开始出现” 有点含糊。这几乎听起来像“当你看到它时你就会知道它”。请说得更具体一些。
  • @Stefan 我们可以定义函数来查找最长重复数字序列的起始索引,但它没有将其标准化。
  • “谁能建议我 [a] 更多 [...] 正确的方法” – 你的方法有什么问题?

标签: ruby-on-rails arrays ruby


【解决方案1】:
my_array.index.with_index{|value,index| my_array[index,6].uniq.size==1}

这是一种调整,如果您的意思是“优化”代码的样子。如果您的意思是优化性能。不合适。

【讨论】:

  • 您能详细说明一下 6 号是如何工作的吗?似乎它不适用于其他整数组合。
  • 问题已更改。此答案基于演示代码。
【解决方案2】:

在第一次迭代中,我得到重复元素序列的数组,然后我继续进行逻辑,

groups = my_array[1..-1].inject([[my_array[0]]]) { |m, n| m.last[0] == n ? m.last << n : m << [n]; m }
# => [[100], [101], [100], [102], [100, 100], [101], [100], [250], [251], [253], [260], [250], [200], [100, 100, 100, 100, 100, 100, 100, 100, 100], [120]]

groups[0,groups.index(groups.sort { |a,b| a.count <=> b.count }.last)].flatten.count
# => 14

使用正则表达式,既精确又简单。

【讨论】:

    【解决方案3】:

    我假设目标是找到给定数组中最长相等元素序列的第一个元素的索引。

    my_array = [100, 101, 100, 102, 100, 100, 101, 100, 250, 251, 253, 260, 250, 200,
                100, 100, 100, 100, 100, 100, 100, 100, 100,
                120]
    

    这里是14100 的索引,后面跟着 8 个100

    我们可以这样做。

    my_array.each_index.chunk { |i| my_array[i] }.
             max_by { |_,a| a.size }.
             last.
             first
               #=> 14
    

    步骤如下。

    enum0 = my_array.each_index
      #=> #<Enumerator: [100, 101, 100,..., 100, 120]:each_index> 
    

    我们可以通过将这个枚举器转换为数组来查看将要生成的元素。

    enum0.to_a
      #=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
      #    17, 18, 19, 20, 21, 22, 23]
    

    继续,

    enum1 = enum0.chunk { |i| my_array[i] }
      #=> #<Enumerator: #<Enumerator::Generator:0x000058d8d09ec8a0>:each> 
    

    鉴于上述表达式的返回值,enum1 可以被认为是一个复合枚举器,尽管 Ruby 没有这样的概念。让我们看看enum1 将生成的值。

    enum1.to_a
      #=> [[100, [0]], [101, [1]], [100, [2]], [102, [3]], [100, [4, 5]],
      #    [101, [6]], [100, [7]], [250, [8]], [251, [9]], [253, [10]],
      #    [260, [11]], [250, [12]], [200, [13]],
      #    [100, [14, 15, 16, 17, 18, 19, 20, 21, 22]],
      #    [120, [23]]]
    

    继续,

    a = enum1.max_by { |v,a| a.size }
      #=> [100, [14, 15, 16, 17, 18, 19, 20, 21, 22]]
    

    由于 v 没有在块中使用,因此习惯上会写成这样:

    a = enum1.max_by { |_,a| a.size }
    

    下划线(有效的局部变量)的存在向读者表明该块变量未用于块计算。最后两步如下。

    b = a.last
      #=> [14, 15, 16, 17, 18, 19, 20, 21, 22] 
    b.first
      #=> 14 
    

    请参阅 Enumerable#chunkEnumerable#max_by

    【讨论】:

      【解决方案4】:
      my_array = [100, 101, 100, 102, 100, 100, 101, 100, 250, 251, 253, 260, 250, 200, 100, 100, 100, 100, 100, 100, 100, 100, 100, 120]
      
      
      index_and_repetitions = lambda { |my_array|
        stk = {}
        previous = my_array[0]
        last_index = 0
        stk[last_index] = 1
        my_array.drop(0).each_with_index{|item, index|
          if item == previous
            stk[last_index] += 1
          else
            last_index = index
            stk[last_index] = 1
            previous = item
          end
        }
        stk
      }
      
      stk = index_and_repetitions.call(my_array)
      puts stk.key(stk.values.max)
      

      你可以从这里找到benchmark results(compared to others answers)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-15
        • 2015-08-19
        • 2011-06-25
        • 2018-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多