数值列表中的循环数,在Spark和Scala中混合使用正数和负数 [英] Number of Cycles from list of values, which are mix of positives and negatives in Spark and Scala
问题描述
需要根据这些数据计算周期数。
例如,
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秒,这是一个循环。
循环数:逻辑 范围是用例中的RDD。它看起来像 c> range.sliding(3).count {case f :: s :: t :: Nil => f< 0&& s> 0&& t> 0} 这会生成长度为3的所有子序列并计算有多少个是-ve,+ ve ,+ ve 概括周期长度 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, 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. Number of cycles: Logic range is a RDD in the use case. It looks like A one liner This generates all sub-sequences of length 3 and counts how many are -ve, +ve, +ve Generalising cycle length
这篇关于数值列表中的循环数,在Spark和Scala中混合使用正数和负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
示例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秒。所以一个周期。
scala> range
range:Seq [com.Range] = List(XtreamRange(858,890,899,920,StartEngage,-758,-790,-890,-720,920,940,950))
def countCycles(n: Int,xs:List [Int])= xs.sliding(n + 1)
.count(ys => ys.head< 0& ys.tail.forall(_> 0) )
val range = List(sampleRange(2020,2030,2040,2050,-1000,-1010,-1020,Starting point,-1030,2040,-1020,2050,2040,2020,end point,-1060,-1030,-1010)
If >= 2 seconds it is a cycle.
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.
scala> range
range: Seq[com.Range] = List(XtreamRange(858,890,899,920,StartEngage,-758,-790,-890,-720,920,940,950))range.sliding(3).count{case f::s::t::Nil => f < 0 && s > 0 && t > 0}
def countCycles(n:Int, xs:List[Int]) = xs.sliding(n+1)
.count(ys => ys.head < 0 && ys.tail.forall(_ > 0))