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:



  1. class EvenStream(object):
  2. def __init__(self):
  3. self.current = 0
  4. def get_next(self):
  5. to_return = self.current
  6. self.current += 2
  7. return to_return
  8. class OddStream(object):
  9. def __init__(self):
  10. self.current = 1
  11. def get_next(self):
  12. to_return = self.current
  13. self.current += 2
  14. return to_return
  15. def print_from_stream(n, stream=None):
  16. stream = OddStream() if stream else EvenStream()
  17. for _ in range(n):
  18. print(stream.get_next())
  19. queries = int(input())
  20. for _ in range(queries):
  21. stream_name, n = input().split()
  22. n = int(n)
  23. if stream_name == "even":
  24. print_from_stream(n)
  25. else:
  26. print_from_stream(n, OddStream())




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube