Для этой оценки Hackerrank мне нужно рассчитать коэффициенты для заданных входных данных. Я написал это, и всякий раз, когда я запускаю его в своей оболочке, он выдает правильный результат, но в компиляторе от Hackerrank он округляет результаты до полного целого числа.
Тестовый ввод:
def plusMinus(arr):
array = []
positive_int = 0
negative_int = 0
zero_int = 0
total_int = 0
#removing the first int from input & appending the rest of the input to the empty array
for i in arr[1:]: ***Error was in this line of code. Removed the parameter "[1:]" and it gave me the right output***
array.append(i)
total_int = len(array)
#calcuting with every integer in array if positive or negative and counting them
for i in array:
if i >= 1:
positive_int +=1
elif i < 0:
negative_int +=1
elif i == 0:
zero_int +=1
#calculate the ratio of the negatives and positives integers
pos_ratio = positive_int / total_int
negative_ratio = negative_int / total_int
zero_ratio = zero_int / total_int
#format to six decimal places
formatted_pos_ratio = "{:.6f}".format(pos_ratio)
formatted_negative_ratio = "{:.6f}".format(negative_ratio)
formatted_zero_ratio = "{:.6f}".format(zero_ratio)
#printing out the ratio's
print(str(formatted_pos_ratio))
print(str(formatted_negative_ratio))
print(str(formatted_zero_ratio))
Для этой оценки Hackerrank мне нужно рассчитать коэффициенты для заданных входных данных. Я написал это, и всякий раз, когда я запускаю его в своей оболочке, он выдает правильный результат, но в компиляторе от Hackerrank он округляет результаты до полного целого числа. Тестовый ввод: [code]6 -4 3 -9 0 4 1 [/code] Мой код: [code]def plusMinus(arr): array = [] positive_int = 0 negative_int = 0 zero_int = 0 total_int = 0
#removing the first int from input & appending the rest of the input to the empty array for i in arr[1:]: ***Error was in this line of code. Removed the parameter "[1:]" and it gave me the right output*** array.append(i) total_int = len(array)
#calcuting with every integer in array if positive or negative and counting them for i in array: if i >= 1: positive_int +=1 elif i < 0: negative_int +=1 elif i == 0: zero_int +=1
#calculate the ratio of the negatives and positives integers pos_ratio = positive_int / total_int negative_ratio = negative_int / total_int zero_ratio = zero_int / total_int
#format to six decimal places formatted_pos_ratio = "{:.6f}".format(pos_ratio) formatted_negative_ratio = "{:.6f}".format(negative_ratio) formatted_zero_ratio = "{:.6f}".format(zero_ratio)
#printing out the ratio's print(str(formatted_pos_ratio)) print(str(formatted_negative_ratio)) print(str(formatted_zero_ratio)) [/code]
Вывод в Hackerrank: [code]0.600000 0.200000 0.200000 [/code] и в моем компиляторе это: [code]0.500000 0.333333 0.166667 [/code] Что я делаю не так?