拆分数组,其中先前值大于当前值

时间:2018-11-08 07:14:41

标签: arrays ruby

我需要拆分此数组:

arr = [3, 4, 5, 6, 3, 4, 5, 2, 3]

其中前一个元素大于当前元素。结果应该是另一个数组:

[[3, 4, 5, 6], [3, 4, 5], [2, 3]]

3 个答案:

答案 0 :(得分:6)

arr.slice_when(&:>).to_a
# => [[3, 4, 5, 6], [3, 4, 5], [2, 3]]

答案 1 :(得分:0)

def spliter array
  splited_array = []
  temp = []
  last = array[0]
  array.each do |element|
    if element >= last
      temp << element
    else
      splited_array << temp
      temp = [element]
    end
    last = element
  end
  splited_array << temp
end

答案 2 :(得分:0)

递归示例。

// in data grid class
protected override DependencyObject GetContainerForItemOverride()
{
  return new MatrixRow();
}

class MatrixRow : DataGridRow
{       
  public MatrixRow()
  {
    var cellPresenterProperty = GetType().GetProperty("CellsPresenter", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty);
    cellPresenterProperty.SetValue(this, new MatrixCellPresenter());
  }
}

class MatrixCellPresenter : DataGridCellsPresenter
{
  protected override DependencyObject GetContainerForItemOverride()
  {
    // won't get called, the presenter is overriden by the internal one
    return new MatrixCell();
  }
}