HackerRank Python - Map and Lambda Function
The map() function applies a function to every member of an iterable and returns the result. It takes two parameters: first, the function that is to be applied and secondly, the iterables.
Let's say you are given a list of names, and you have to print a list that contains the length of each name.
- cube = lambda x: x ** 3
- def fibonacci(n):
- a, b, c = 0, 1, 1
- for _ in range(n):
- yield a
- a, b = b, a + b
- if __name__ == '__main__':
- n = int(input())
- print(list(map(cube, fibonacci(n))))