如果我有一个数组 Candy[] type;
和 type = new Candy[capacity];
如果数组已满是capacity = type.length?
我有2个不同的代码:
def bar(a):
return [ x**3 for x in a ]
Run Code Online (Sandbox Code Playgroud)
和
def cube(a):
for x in a:
return x**3
Run Code Online (Sandbox Code Playgroud)
如果我将bar(a)定义为([1,2,3,4]),我的输出将是[1,8,27,64].如果我将cube(a)定义为([1,2,3,4]),我的输出只是1.为什么?他们不是假设给出相同的输出吗?不是"for a in a"对于a中的每个x是否意味着执行以下迭代?
我正在创建一个1-100的随机数,我正在查看一些Stackoverflow问题以寻找正确的方法,我对许多不同的建议感到困惑.使用它有什么区别:
int random= (int)(Math.random()*((100-1)+1));
Run Code Online (Sandbox Code Playgroud)
这个:
int random= (int)(Math.random()*(100);
Run Code Online (Sandbox Code Playgroud)
还有这个:
int random= 1+ (int)(Math.random()*((100-1)+1));
Run Code Online (Sandbox Code Playgroud) 如果我在表达式的开头有一个带括号的字符串,我将如何使用正则表达式将它们与数字一起删除.例如,我有字符串A:
"(1) Hi How are you (2).
我希望它看起来像这样:字符串B:
Hi How Are you (2).
我试过用
string.replaceAll( "^\p {P}", "");
但这只做到了这一点:
1)你好,你好(2).
它只删除了表达式中的第一个括号.
为什么我运行console.log(action(8))两次,它给出了答案1108和2108?不应该是1108和1108?
var counter = 100; //line 1
function make(){
var local = 1000;
return function(x){
temp = local;
local *= 2;
return x + counter + temp;
}
}
var action = make();
console.log(action(8)); // 1108
console.log(action(8)); // 2108
Run Code Online (Sandbox Code Playgroud)