HackerRank Python - Default Arguments
In this challenge, the task is to debug the existing code to successfully execute all provided test files.
Python supports a useful concept of default argument values. For each keyword argument of a function, we can assign a default value which is going to be used as the value of said argument if the function is called without it. For example, consider the following increment function:
- 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):
- stream = OddStream() if stream else EvenStream()
- for _ in range(n):
- print(stream.get_next())
-
-
- 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())