0

I am having a difficult time figuring out where my code is failing. Hackerrank has its own boilerplate testing that I am not accustomed to yet. The algorithm workers in the Hackerrank debug output and in my own Ide but returns "NoneNone" in Stdout. I know that returning nothing creates None but even when I do "return '/n'.join(a_list) it doesn't work. Making me unable to pass tests. What am I not seeing? https://www.hackerrank.com/challenges/cut-the-sticks/problem

This is NOT a duplicate problem. The down votes are very discouraging and unhelpful 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()

Colorful Codes
  • 577
  • 4
  • 16

1 Answers1

0

You have to return a list containing the number of sticks present before each operation from the cutTheSticks function. In the main, the boilerplate is joining the elements present in the list into a string (you don't need to explicitly cast the return value as 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()
Venkata Rathnam
  • 348
  • 1
  • 5
  • I should have posted the actual question link. https://www.hackerrank.com/challenges/cut-the-sticks/problem They don't want a list returned but the values printed. I tried to return '/n.join(a_list) but it wasn't working. The length of each iteration has to be outputted on a new line for stdout. – Colorful Codes Nov 15 '19 at 03:47
  • Yes, the output has to be printed. But the boilerplate code does that. For that only they are using file pointer 'fptr'. 'fptr.write' will print the output to stdout for you. Did you run the suggeted code at first? – Venkata Rathnam Nov 15 '19 at 04:30