Hackerrank Cut the Sticks challenge correct in debug but not outpu

2024-10-03 02:45:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难找出我的代码在哪里失败了。Hackerrank有自己的样板测试,我还不习惯。算法在Hackerrank调试输出和我自己的Ide中工作,但在Stdout中返回“none”。我知道不返回任何东西都不会产生任何结果,但即使我执行了“return'/n'.join(一个\u列表)操作,它也不起作用。让我无法通过考试。我没看见什么?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()


Tags: importforlenreturnosresultwriteleast
1条回答
网友
1楼 · 发布于 2024-10-03 02:45:08

您必须从cutTheSticks函数返回一个列表,其中包含每次操作前存在的棍子数。在main中,样板文件将列表中的元素连接到一个字符串中(不需要显式地将返回值转换为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()

相关问题 更多 >