gpt4 book ai didi

Int 的 Scala 序列

转载 作者:行者123 更新时间:2023-12-05 08:27:39 26 4
gpt4 key购买 nike

我有这样的东西:

    case class FunctionsTest(lowerBound: Int = 1,
upperBound: Int = 1000,
factor: Int = 2) {
require(lowerBound < upperBound)

/**
* implement a sequence of ints, which start with lowerBound and end with
* upperbound.
*
* for all elements following should be true:
*
* xs(i) < xs(i+1)
* xs(i) + factor == xs(i + 1) (for i > 0 and i <= 1000)
*
*/
val xs: Seq[Int] = Seq.range(lowerBound,upperBound +1)

所以我需要这个类的序列来构成这些标准..我用

试过了

Seq.range()

但它为我创建了适合第一个标准的序列,但我不知道现在如何应用评论中提到的第二个标准?

最佳答案

Seq.range[T](start: T, end: T, step)step 参数允许您按因子增加。

scala> Seq.range(1,10,2)
res0: Seq[Int] = List(1, 3, 5, 7, 9)

这两个条件都满足。

scala> res0.zip(res0.tail).forall(t => t._1 < t._2)
res4 Boolean = true

scala> res0(0) + 2 == res0(0 + 1)
res5: Boolean = true

关于Int 的 Scala 序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33426488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com