标题似乎有些混乱,但是我想做的很简单。 我有2个列表,一个是:
List(12, 14, 16, 24, 26, 32, 36, 43, 44, 46, 52, 54, 56, 62, 66, 72, 74, 76)
第二个是:
List(11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 31, 32)
我想遍历第一个列表,看看第一个列表中是否存在一个值,该值将是前一个值和第二个列表中另一个值的总和。如果存在类似的值,则应将其与第二个列表中的值相加,并检查第一个列表中是否存在类似的值,依此类推,直到达到第一个列表中的最大可获取值。
例如:如果我们从第一个列表中获取值16
,并继续将其与28
相加,我们将得到值:
16, 44 and 72
或者:我们从第一个列表中选取24
,继续向其中添加19
并获取值:
24, 43 and 62
如果包括起始值在内,通过添加相同的数字找不到至少2个以上的值,则应该只获取列表中的下一个值并进行相同的计算。
任何帮助将不胜感激,我尝试使用 fold 和 reduce 函数,但无法使其正常工作。
编辑:
尝试使用Stream.iterate
来完成这项工作,但这并没有真正作用
val iTried = Stream.iterate(0)(i => i + 1)
.takeWhile(i => coordinatesRows(i)
.toString exists coordinatesRows + valuesForDiag(i))
还尝试实现自己的功能:
def sortDiagonally(xs: List[Int]) ={
/**
**/
val start = xs.head
Stream.from(1).flatMap{ value =>
xs exists (start + valuesForDiag == xs)
}
我仍在学习语言,所以我对Scala的所有来龙去脉都不熟悉。
编辑2: 通过使用这两个列表,我希望获得以下输出:
List(List(16, 44, 72), List(24, 43, 62), List(32, 54, 76), List(36, 54, 72), List(12, 24, 36), List(12, 44, 76))
我还从第二个列表中删除了10, 20 and 30
的值,因为它不应该在那里。
答案 0 :(得分:0)
// Customizes read more link in excerpts
function new_excerpt_more($more) {
global $post;
return ‘ <strong> … on the edge of your seat? Read more! </strong> ‘; //you
can change this text to whatever you like
}
add_filter(‘excerpt_more’, ‘new_excerpt_more’);
返回:
val vals = List(12, 14, 16, 24, 26, 32, 36, 43, 44, 46, 52, 54, 56, 62, 66, 72, 74, 76)
val mods = List(10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
def find(l: List[Int]): Set[Int] = l match {
case Nil => Set.empty[Int]
case x :: xs =>
xs.filter{
i => mods.exists(j => (i - x) % j == 0)
}.toSet ++ find(xs)
}
find(vals)
所有匹配值:
Set(56, 24, 52, 46, 74, 32, 44, 54, 76, 66, 72, 43, 26, 36, 62)
返回:
def find(l: List[Int]): List[(Int, Int, Int)] = l match {
case Nil => Nil
case x :: xs =>
xs.flatMap{
i => mods.collect{ case j if (i - x) % j == 0 => (x, i, j) }
} ++ find(xs)
}
println(find(vals))
最后
List((12,24,12), (12,26,14), (12,32,10), (12,32,20), (12,36,12), (12,36,24), (12,43,31), (12,44,16), (12,44,32), (12,46,17), (12,52,10), (12,52,20), (12,54,14), (12,54,21), (12,56,11), (12,56,22), (12,62,10), (12,62,25), (12,66,18), (12,66,27), (12,72,10), (12,72,12), (12,72,15), (12,72,20), (12,72,30), (12,74,31), (12,76,16), (12,76,32), (14,24,10), (14,26,12), (14,32,18), (14,36,11), (14,36,22), (14,43,29), (14,44,10), (14,44,15), (14,44,30), (14,46,16), (14,46,32), (14,52,19), (14,54,10), (14,54,20), (14,56,14), (14,56,21), (14,62,12), (14,62,16), (14,62,24), (14,66,13), (14,66,26), (14,72,29), (14,74,10), (14,74,12), (14,74,15), (14,74,20), (14,74,30), (14,76,31), (16,26,10), (16,32,16), (16,36,10), (16,36,20), (16,43,27), (16,44,14), (16,44,28), (16,46,10), (16,46,15), (16,46,30), (16,52,12), (16,52,18), (16,54,19), (16,56,10), (16,56,20), (16,62,23), (16,66,10), (16,66,25), (16,72,14), (16,72,28), (16,74,29), (16,76,10), (16,76,12), (16,76,15), (16,76,20), (16,76,30), (24,36,12), (24,43,19), (24,44,10), (24,44,20), (24,46,11), (24,46,22), (24,52,14), (24,52,28), (24,54,10), (24,54,15), (24,54,30), (24,56,16), (24,56,32), (24,62,19), (24,66,14), (24,66,21), (24,72,12), (24,72,16), (24,72,24), (24,74,10), (24,74,25), (24,76,13), (24,76,26), (26,36,10), (26,43,17), (26,44,18), (26,46,10), (26,46,20), (26,52,13), (26,52,26), (26,54,14), (26,54,28), (26,56,10), (26,56,15), (26,56,30), (26,62,12), (26,62,18), (26,66,10), (26,66,20), (26,72,23), (26,74,12), (26,74,16), (26,74,24), (26,76,10), (26,76,25), (32,43,11), (32,44,12), (32,46,14), (32,52,10), (32,52,20), (32,54,11), (32,54,22), (32,56,12), (32,56,24), (32,62,10), (32,62,15), (32,62,30), (32,66,17), (32,72,10), (32,72,20), (32,74,14), (32,74,21), (32,76,11), (32,76,22), (36,46,10), (36,52,16), (36,54,18), (36,56,10), (36,56,20), (36,62,13), (36,62,26), (36,66,10), (36,66,15), (36,66,30), (36,72,12), (36,72,18), (36,74,19), (36,76,10), (36,76,20), (43,54,11), (43,56,13), (43,62,19), (43,66,23), (43,72,29), (43,74,31), (43,76,11), (44,54,10), (44,56,12), (44,62,18), (44,66,11), (44,66,22), (44,72,14), (44,72,28), (44,74,10), (44,74,15), (44,74,30), (44,76,16), (44,76,32), (46,56,10), (46,62,16), (46,66,10), (46,66,20), (46,72,13), (46,72,26), (46,74,14), (46,74,28), (46,76,10), (46,76,15), (46,76,30), (52,62,10), (52,66,14), (52,72,10), (52,72,20), (52,74,11), (52,74,22), (52,76,12), (52,76,24), (54,66,12), (54,72,18), (54,74,10), (54,74,20), (54,76,11), (54,76,22), (56,66,10), (56,72,16), (56,74,18), (56,76,10), (56,76,20), (62,72,10), (62,74,12), (62,76,14), (66,76,10))
返回:
def find(l: List[Int]): List[List[Int]] = l match {
case Nil => Nil
case x :: xs =>
mods.map {
mod => x :: xs.collect { case c if (c - x) % mod == 0 => c }
}.filter(_.size > 1) ++ find(xs)
}
find(vals)