Default Arguments

Sort by

recency

|

206 Discussions

|

  • + 0 comments

    no clarity in the challenge as per my understanding

  • + 0 comments

    Debug the given function print_from_stream

    I'm not sure where the function is given - there is no print_from_stream name in locals() (or any other name that could lead to it).

    it should use an instance of EvenStream class defined in the code stubs below

    I am a new user on this platform and I have no idea where these code stubs are.

    I have assumed that what the author actually meant was to implement print_from_stream, EvenStream and OddStream - and it seems to have worked: https://www.hackerrank.com/challenges/default-arguments/submissions/code/424109419 .

  • + 0 comments

    If you let stream argument with default value as EventStream() , Python declares the EvenStream instance shared across all this function calls, no matter how much times you call it. To fix it, you must set default value to None and declare the instance into the function.

    def print_from_stream(n, stream=None):
        if stream is None:
            stream = EvenStream()
            
        for _ in range(n):
            value = stream.get_next()
            print(value)
    
  • + 0 comments

    They shouldn't define the assignment as "debugging", it's misleading because "debugging" means a very specific process which requires test or application code with a use case. What they meant to say was "notice a bug", or "there is a bug in this code, what is it?", or "Is there bug in this code?". Also, there was no code on the screen at all. ...After a few next minutes looking for code and promised "provided test files", and pushing randomly buttons I got a message saying that the "test files" cost 5 hackos. Wouldn't be better at the begining of the assignment to inform the reader where the "test files" are and that they would cost money, and also from the beggining on to state that one should open a paying account?

  • + 0 comments

    class EvenStream: def init(self): self.current = 0

    def get_next(self):
        next_value = self.current
        self.current += 2
        return next_value
    

    class OddStream: def init(self): self.current = 1

    def get_next(self):
        next_value = self.current
        self.current += 2
        return next_value
    

    def print_from_stream(n, stream=None): if stream is None: stream = EvenStream() for _ in range(n): print(stream.get_next())

    if name == "main": queries = int(input()) for _ in range(queries): stream_name, n = input().split() n = int(n) if stream_name == "even": print_from_stream(n) elif stream_name == "odd": print_from_stream(n, OddStream())