我很难弄清楚我的代码在哪里失败。 Hackerrank有自己的样板测试,我还不习惯。 Hackerrank中的算法工作者在我自己的Ide中调试输出,但在Stdout中返回“ NoneNone”。我知道什么也不返回不会产生任何结果,但是即使我执行“ return'/n'.join(a_list)也不起作用。这使我无法通过测试。我没看到什么?https://www.hackerrank.com/challenges/cut-the-sticks/problem
这不是重复的问题。否决票非常令人沮丧且无助于nvm。
#!/bin/python3
import math
import os
import random
import re
import sys
def cutTheSticks(arr):
currentSize = len(arr)
while currentSize > 0:
least = min(arr)
print(currentSize)
for stick in range(len(arr)):
arr[stick] -= least
if arr[stick] <= 0:
arr[stick] = 0
for i in range(arr.count(0)):
arr.remove(0)
currentSize = len(arr)
return
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
arr = list(map(int, input().rstrip().split()))
result = str(cutTheSticks(arr))
fptr.write(result)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()
答案 0 :(得分:0)
您必须从cutTheSticks
函数返回一个列表,其中包含每个操作之前存在的摇杆数量。基本上,样板将列表中存在的元素连接到字符串中(您无需将返回值显式转换为str)。
def cutTheSticks(arr):
currentSize = len(arr)
result = []
while currentSize > 0:
least = min(arr)
#print(currentSize)
result.append(currentSize)
for stick in range(len(arr)):
arr[stick] -= least
if arr[stick] <= 0:
arr[stick] = 0
for i in range(arr.count(0)):
arr.remove(0)
currentSize = len(arr)
return result
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input())
arr = list(map(int, input().rstrip().split()))
result = cutTheSticks(arr)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()