Default Arguments

Sort by

recency

|

197 Discussions

|

  • + 0 comments

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

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

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

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

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

    raw_input = input

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

  • + 0 comments

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

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

    Why does the problem say "the task is to debug the existing code" when there is no existing code at all?

    I am extremely cconfused by the problem saying "the task is to debug the existing code". It makes me think I don't need to define OddStream etc. myself. Why not just say define your own OddStream, EvenStream etc. and produce expected output???

  • + 1 comment

    It work perfect after adding stream.init() to reset current variable.

    def print_from_stream(n, stream=EvenStream()):

    stream.__init__()    # add this line to reset self.current
    for _ in range(n):
        print(stream.get_next())