我用这段 hackerrank 代码做错了什么?
What am I doing wrong with this code for hackerrank?
我一直在为 HackerRank 编写这个问题,我 运行 遇到了很多问题。这个问题叫做 "Plus Minus",我在 Python 中做 3. 方向在 https://www.hackerrank.com/challenges/plus-minus/problem 上。我尝试了很多东西,它说 "there is no response on stdout"。我想返回的是 none 类型。这是代码。:
def plusMinus(arr):
p = 0
neg = 0
z = arr.count(0)
no = 0
for num in range(n):
if arr[num] < 0:
neg+=1
if arr[num] > 0:
p+=1
else:
no += 1
continue
return p/n
以下是问题:
1) 变量n
,表示数组的长度,需要传递给函数plusMinus
2) 无需维护额外变量no
,因为您已经计算了零计数。因此,我们可以去掉多余的else
条件
3) 不需要使用continue
语句,因为语句后面没有代码。
4) 该函数需要打印值而不是返回。
为了便于理解,请查看以下代码并正确命名变量:
def plusMinus(arr, n):
positive_count = 0
negative_count = 0
zero_count = arr.count(0)
for num in range(n):
if arr[num] < 0:
negative_count += 1
if arr[num] > 0:
positive_count += 1
print(positive_count/n)
print(negative_count/n)
print(zero_count/n)
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().rstrip().split()))
plusMinus(arr, n)
末尾的 6 位小数需要:
Positive_Values = 0
Zeros = 0
Negative_Values = 0
n = int(input())
array = list(map(int,input().split()))
if len(array) != n:
print(f"Error, the list only has {len(array)} numbers out of {n}")
else:
for i in range(0,n):
if array[i] == 0:
Zeros +=1
elif array[i] > 0:
Positive_Values += 1
else:
Negative_Values += 1
Proportion_Positive_Values = Positive_Values / n
Proportion_Of_Zeros = Zeros / n
Proportion_Negative_Values = Negative_Values / n
print('{:.6f}'.format(Proportion_Positive_Values))
print('{:.6f}'.format(Proportion_Negative_Values))
print('{:.6f}'.format(Proportion_Of_Zeros))
我一直在为 HackerRank 编写这个问题,我 运行 遇到了很多问题。这个问题叫做 "Plus Minus",我在 Python 中做 3. 方向在 https://www.hackerrank.com/challenges/plus-minus/problem 上。我尝试了很多东西,它说 "there is no response on stdout"。我想返回的是 none 类型。这是代码。:
def plusMinus(arr):
p = 0
neg = 0
z = arr.count(0)
no = 0
for num in range(n):
if arr[num] < 0:
neg+=1
if arr[num] > 0:
p+=1
else:
no += 1
continue
return p/n
以下是问题:
1) 变量n
,表示数组的长度,需要传递给函数plusMinus
2) 无需维护额外变量no
,因为您已经计算了零计数。因此,我们可以去掉多余的else
条件
3) 不需要使用continue
语句,因为语句后面没有代码。
4) 该函数需要打印值而不是返回。
为了便于理解,请查看以下代码并正确命名变量:
def plusMinus(arr, n):
positive_count = 0
negative_count = 0
zero_count = arr.count(0)
for num in range(n):
if arr[num] < 0:
negative_count += 1
if arr[num] > 0:
positive_count += 1
print(positive_count/n)
print(negative_count/n)
print(zero_count/n)
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().rstrip().split()))
plusMinus(arr, n)
末尾的 6 位小数需要:
Positive_Values = 0
Zeros = 0
Negative_Values = 0
n = int(input())
array = list(map(int,input().split()))
if len(array) != n:
print(f"Error, the list only has {len(array)} numbers out of {n}")
else:
for i in range(0,n):
if array[i] == 0:
Zeros +=1
elif array[i] > 0:
Positive_Values += 1
else:
Negative_Values += 1
Proportion_Positive_Values = Positive_Values / n
Proportion_Of_Zeros = Zeros / n
Proportion_Negative_Values = Negative_Values / n
print('{:.6f}'.format(Proportion_Positive_Values))
print('{:.6f}'.format(Proportion_Negative_Values))
print('{:.6f}'.format(Proportion_Of_Zeros))