折叠阵列连续的'同'的元素 [英] Collapse consecutive 'same' elements in array

查看:121
本文介绍了折叠阵列连续的'同'的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

鉴于要素和标准的阵列,以确定是否两个元素是'相同的',返回一个新的数组,其中连续的同元件运行已被删除只留下终点。例如:

A = [{K:'A',V:1},{K:'B',V:1 },{K:'C',v:1},
      {K:'D',V:2},{K:E,V:2},
      {K:'F',V:3},{K:'G',V:3},{K:'H',V:3},{K:'我',V:3},[KP :J,ν:3},
      {K:'K',V:2},
      {K:'L',V:4},{K:'M',V:4},{K:'N',V:4},{K:'O',V:4}]
B = {a.collapse_consecutive | H | H [:V]}
#=> [{K:'一',ν:1},{k为'C',ν:1},
#=> {K:'D',V:2},{K:E,V:2},
#=> {K:'F',V:3},{K:'J',V:3},
#=> {K:'K',V:2},
#=> {K:'L',V:4},{K:'O',V:4}]

动机

绘图时的 N 的上线图的一系列连续相同值的结果有,除了在端点在图上没有影响点。在黑色的样品如下图对最终曲线没有影响。我存储精细采样图和将理想要删除所有不相关的样本。

对于这个问题,我简化了问题,只除去在水平段的黑点,因为识别沿着倾斜的直线段下跌点数为(一)困难和(b)较为少见(在我的情况)。

目前进展

这是我想出迄今最好的是这个解决方案依赖于数组索引:

类Array
  高清collapse_consecutive
    select.with_index {| O,I |
      我== 0 ||产量(自[I-1])!=产量(O)||
      !自第[i + 1] ||产量(自第[i + 1])!=产量(O)
    结束
  结束
结束

这工作,但依赖于Ruby中的数组索引通常是code闻:指示有一个可用的更优雅的实施


解决方案

使用的 可枚举#块 和的 可枚举#flat_map

  a.chunk {| H | H [:V]} {.flat_map | C | [C [1] [0],C [1] [ -  1]。uniq的}


  

[{:K =>一,V => 1},{:K =>c的,V => 1},{:K =>d的,:V => 2 },{:K =>的e,v => 2},{:K =>F,v => 3},{:K =>J,v => 3}, {K =>K,v => 2},{:K =>L,v => 4},{:K =>○,v => 4}]


Goal

Given an array of elements and criteria to determine if two elements are the 'same', return a new array where runs of consecutive 'same' elements have been removed to leave only the end points. For example:

a = [ {k:'a',v:1}, {k:'b',v:1}, {k:'c',v:1},
      {k:'d',v:2}, {k:'e',v:2},
      {k:'f',v:3}, {k:'g',v:3}, {k:'h',v:3}, {k:'i',v:3}, {k:'j',v:3},
      {k:'k',v:2},
      {k:'l',v:4}, {k:'m',v:4}, {k:'n',v:4}, {k:'o',v:4} ]
b = a.collapse_consecutive{ |h| h[:v] }
#=> [ {k:'a',v:1}, {k:'c',v:1},
#=>   {k:'d',v:2}, {k:'e',v:2},
#=>   {k:'f',v:3}, {k:'j',v:3},
#=>   {k:'k',v:2},
#=>   {k:'l',v:4}, {k:'o',v:4} ]

Motivation

When plotting n points on a line graph a series of consecutive same-valued results have no effect on the graph except at the end points. In the graph below the black samples have no effect on the final graph. I am storing finely-sampled graphs and would ideally like to remove all irrelevant samples.

For this question I am simplifying the problem to only remove the black dots on horizontal sections, since identifying points that fall along angled linear sections is (a) harder and (b) more rare (in my case).

Current Progress

The best that I've come up with so far is this solution that relies upon array indexing:

class Array
  def collapse_consecutive
    select.with_index{ |o,i|
      i==0 || yield(self[i-1])!=yield(o) ||
      !self[i+1] || yield(self[i+1])!=yield(o)
    end
  end
end

This works, but relying upon array indexing in Ruby is usually "code smell": an indication that there is a more elegant implementation available.

解决方案

Use Enumerable#chunk and Enumerable#flat_map:

a.chunk { |h| h[:v] }.flat_map { |c| [c[1][0],c[1][-1]].uniq }

[{:k=>"a", :v=>1}, {:k=>"c", :v=>1}, {:k=>"d", :v=>2}, {:k=>"e", :v=>2}, {:k=>"f", :v=>3}, {:k=>"j", :v=>3}, {:k=>"k", :v=>2}, {:k=>"l", :v=>4}, {:k=>"o", :v=>4}]

这篇关于折叠阵列连续的'同'的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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