- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
想象一下,我有一个未排序的正整数和负整数列表。我想返回一个包含所有正整数和第一个负数的列表,然后忽略列表中所有后续的负数,同时保留排序。
当务之急我可以这样做:
l = [1, 2, -4, 5, -6, -1, 3]
out = []
first = true
for n in l:
if n >= 0:
out.push(n)
else if first:
out.push(n)
first = false
// out = [1, 2, -4, 5, 3]
val l = List(1, 2, -4, 5, -6, -1, 3)
val posl = l.map(_ >= 0)
val negl = l.zipWithIndex.map((n, i) => if (n < 0) (i, n) else (None, None)).head
// now split posl at negl._1, and create a new list of leftSlice :: negl._2 :: rightSlice?
最佳答案
如果没有稍微过于巧妙的递归+模式匹配答案,这将不是一个合适的函数式编程问题。
def firstNegAllPos(l:List[Int]):List[Int] = {
l match{
case x::xs if x>=0 => x::firstNegAllPos(xs)
case x::xs if x<0 => x::xs.filter(_>=0)
case Nil => Nil
}
}
关于scala - 如何使用函数式编程返回列表中的所有正数和第一个负数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27668713/
所以我目前正在代码 war 中解决以下问题: 返回一个数组,其中第一个元素是正数的计数,第二个元素是负数的总和。如果输入数组为空或 null,则返回一个空数组。 我想出了下面的代码,它并不漂亮,但我知
我想禁用文本框中的字母,这就是我使用此表达式的原因: /^[0-9]*$/ 问题是我也想允许负数, 这个表达式不允许我使用减号。(-)..我该怎么办? 最佳答案 尝试使用以下正则表达式: /^-?[0
这是一道面试题。我们有一个正整数数组,我们必须重新排列和连接数组元素,以便生成的数字是可以使用此数组形成的最大数字。 例如: [884 88] -> 88884 [20 19 90] -> 90201
所以我有点无奈。我想我了解如何对 IEEE-754 数字进行加法,但我在减法方面遇到了很大的问题。我感觉我的想法是对的,但我想和大家确认一下。 因此我们有以下两个 IEEE-754 编号: x: 0
我想将一个正数转换成相应的数字列表——数字也应该是整数。 转换时,比如1024,应该返回[1,0,2,4] 最佳答案 在 Swift 4.1 或更高版本中 let number = 1024 let
如果 float 可以用来正确表示我将使用的参数范围,我正在尝试为我的音频应用程序弄清楚。 它需要的“最大”掩码用于频率参数,它是正的,并且允许最多两位数字作为尾数(即从 20.00 hz 到 220
我是一名优秀的程序员,十分优秀!