数值列表中的循环数,在Spark和Scala中混合使用正数和负数 [英] Number of Cycles from list of values, which are mix of positives and negatives in Spark and Scala

查看:238
本文介绍了数值列表中的循环数,在Spark和Scala中混合使用正数和负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用包含正值和负值组合的值列表的RDD。
需要根据这些数据计算周期数。



例如,

val range = List(sampleRange(2020,2030,2040,2050,-1000,-1010,-1020,Start point,-1030,2040,-1020,2050,2040,2020,end point ,-1060,-1030,-1010)



上面列表中每个值之间的间隔为1秒,即2020和2030以1秒为间隔记录等等。

从负值变为正值并保持正值> 2秒的次数。

如果> = 2秒,这是一个循环。



循环数:逻辑
示例1: 列表(1,2,3,4,5,6,-15,-66)

周期数为 1

原因:当我们从列表的第一个元素移动到第六个元素时,我们有5个区间,这意味着5秒,所以一个循环
当我们移动到列表的第6个元素时,它是一个负值。从第6个元素开始计数,移动到第7个元素,负值只有2且间隔仅为1.因此不计为循环。

示例2:列表(11,22,33,-25, -36,-43,20,25,28)

周期数是 3

原因:当我们从列表的第一个元素到第三个元素,我们有2个间隔,这意味着2秒。所以一个周期当我们移动到列表的第四个元素时,它是一个负值。所以我们从第四元素开始计算,移动到第五元素,第六元素。我们有2个间隔,这意味着2秒。所以一个周期当我们移动到列表的第7个元素时,它是一个正值。所以我们从第7个元素开始计数,移动到第8个元素,第9个元素。我们有2个间隔,这意味着2秒。所以一个周期。


范围是用例中的RDD。它看起来像

scala> range

range:Seq [com.Range] = List(XtreamRange(858,890,899,920,StartEngage,-758,-790,-890,-720,920,940,950))

解决方案

c> range.sliding(3).count {case f :: s :: t :: Nil => f< 0&& s> 0&& t> 0}

这会生成长度为3的所有子序列并计算有多少个是-ve,+ ve ,+ ve

概括周期长度

  def countCycles(n: Int,xs:List [Int])= xs.sliding(n + 1)
.count(ys => ys.head< 0& ys.tail.forall(_> 0) )


Have an RDD with List of values, which are mix of positives and negatives. Need to compute number of cycles from this data.

For example,

val range = List(sampleRange(2020,2030,2040,2050,-1000,-1010,-1020,Starting point,-1030,2040,-1020,2050,2040,2020,end point,-1060,-1030,-1010)

the interval between each value in above list is 1 second. ie., 2020 and 2030 are recorded in 1 second interval and so on.

how many times it turns from negative to positive and stays positive for >= 2 seconds.
If >= 2 seconds it is a cycle.

Number of cycles: Logic
Example 1: List(1,2,3,4,5,6,-15,-66)
No. of cycles is 1.
Reason: As we move from 1st element of list to 6th element, we had 5 intervals which means 5 seconds. So one cycle. As we move to 6th element of list, it is a negative value. So we start counting from 6th element and move to 7th element. The negative values are only 2 and interval is only 1. So not counted as cycle.
Example 2:
List(11,22,33,-25,-36,-43,20,25,28)
No. of cycles is 3.
Reason: As we move from 1st element of list to 3rd element, we had 2 intervals which means 2 seconds. So one cycle As we move to 4th element of list, it is a negative value. So we start counting from 4th element and move to 5th, 6th element. we had 2 intervals which means 2 seconds. So one cycle As we move to 7th element of list, it is a positive value. So we start counting from 7th element and move to 8th, 9th element. we had 2 intervals which means 2 seconds. So one cycle.

range is a RDD in the use case. It looks like
scala> range
range: Seq[com.Range] = List(XtreamRange(858,890,899,920,StartEngage,-758,-790,-890,-720,920,940,950))

解决方案

A one liner

range.sliding(3).count{case f::s::t::Nil => f < 0 && s > 0 && t > 0}

This generates all sub-sequences of length 3 and counts how many are -ve, +ve, +ve

Generalising cycle length

def countCycles(n:Int, xs:List[Int]) = xs.sliding(n+1)
                                         .count(ys => ys.head < 0 && ys.tail.forall(_ > 0))

这篇关于数值列表中的循环数,在Spark和Scala中混合使用正数和负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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