Map and Lambda Function Hacker Rank Solution – Queslers

Problem : Map and Lambda Function Hacker Rank Solution

Let’s learn some new Python concepts! You have to generate a list of the first N fibonacci numbers, 0 being the first number. Then, apply the map function and a lambda expression to cube each fibonacci number and print the list.

ConceptThe 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.

>> print (list(map(len, ['Tina', 'Raj', 'Tom'])))  
[4, 3, 3]  

Lambda is a single expression anonymous function often used as an inline function. In simple words, it is a function that has only one line in its body. It proves very handy in functional and GUI programming.

>> sum = lambda a, b, c: a + b + c
>> sum(1, 2, 3)
6

Note :Lambda functions cannot use the return statement and can only have a single expression. Unlike def, which creates a function and assigns it a name, lambda creates a function and returns the function itself. Lambda can be used inside lists and dictionaries.


Input Format :

One line of input: an integer N.

Constraints :

  • 0 <= N <= 15

Output Format :

A list on a single line containing the cubes of the first N fibonacci numbers.


Sample Input :

5

Sample Output :

[0, 1, 1, 8, 27]

Explanation :

The first 5 fibonacci numbers are [0, 1, 1, 2, 3] , and their cubes are [0, 1, 1, 8, 27].

Map and Lambda Function Hacker Rank Solution in python 2

# Enter your code here. Read input from STDIN. Print output to STDOUT
def fib(n):
	if n == 0:
		return 0
	if n == 1:
		return 1
	return fib(n-2) + fib(n-1)
n = int(raw_input())
cube = lambda x: x*x*x
print list(map(cube,[fib(a) for a in xrange(n)]))

Map and Lambda Function Hacker Rank Solution in python 3

cube = lambda x: pow(x,3)# complete the lambda function 
def fibonacci(n):
    # return a list of fibonacci numbers
    lis = [0,1]
    for i in range(2,n):
        lis.append(lis[i-2] + lis[i-1])
    return(lis[0:n])

Map and Lambda Function Hacker Rank Solution in pypy

cube = lambda x: x**3

def fibonacci(n):
    # return a list of fibonacci numbers
    result = [0,1]
    for i in range(2,n):
        result.append(result[i-1]+result[i-2])
        
    return result[:n]

Map and Lambda Function Hacker Rank Solution in pypy 3

cube = lambda x: pow(x,3)# complete the lambda function 

def fibonacci(n):
    # return a list of fibonacci numbers
    lis = [0,1]
    
    for i in range(2,n):
        lis.append(lis[i-2] + lis[i-1])
        
    return(lis[0:n])
Map and Lambda Function Hacker Rank Solution Review:

In our experience, we suggest you solve this Map and Lambda Function Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Map and Lambda Function is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution

Map and Lambda Function Hacker Rank Solution Conclusion:

I hope this Map and Lambda Function Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Mini-Max Sum Hacker Rank Solution

String Validators Hacker Rank Solution

Text Alignment Hacker Rank solution

String validators Hacker Rank Solution

Staircase Hacker Rank Solution

Leave a Reply

Your email address will not be published.