现在,我被认为是我的函数不正确,因为我得到的布尔输出不止1个。
import static java.util.stream.IntStream.rangeClosed;
class Benchmark {
public static void main(String[] args) {
final int iterations = 100_000_000;
long start = System.currentTimeMillis();
rangeClosed(1, 50).parallel()
.forEach(i -> rangeClosed(1, iterations).mapToDouble(Math::sqrt).sum());
System.out.println(System.currentTimeMillis() - start);
}
}
答案 0 :(得分:2)
您不想为每个项目打印 True
或False
;您想在循环遍历的过程中创建一个布尔值。或者更简单地说,只要找到一个未通过测试的元素,就可以返回False
,只有在整个循环中都返回而不返回的情况下,才返回True
。
def checkStringLength(searchInteger, lstStrings):
'return Boolean value if the strings are shorter/longer than the first argument'
for i in lstStrings:
if len(i) < searchInteger:
return False
return True
这自然是使用all
函数编写的:
def checkStringLength(searchInteger, lstStrings):
return all(len(i) >= searchInteger for i in lstStrings)