1

I'm currently doing a hackerrank problem.

The objective is to find the cubes of the first n Fibonacci numbers. The user inputs n. So for example, if the user inputs 5, the output should be [0, 1, 1, 8, 27], since the first five Fibonacci numbers are [0, 1, 1, 2, 3].

I've written the following code:

cube = lambda x: x**3 # complete the lambda function 

def fibonacci(n):
    # return a list of fibonacci numbers
    initiallist = []
    for i in range(n):
        if i < 2:
            initiallist += [i]          
        else:
            initiallist += [initiallist[-1] + initiallist[-2]]
    return initiallist

I'm given the following code (which I cannot edit):

if __name__ == '__main__':
    n = int(input())
    print(map(cube, fibonacci(n)))

The problem is that my code fails the test cases because it returns a map object instead of a list. How can I ensure it returns a list instead?

4
  • 2
    if you can't change code in __main__ then you can only use Python 2 instead of Python 3 to get correct result. (or maybe overwrite function map) Commented Jan 21, 2017 at 22:55
  • How does it work with Python 2? Commented Jan 21, 2017 at 22:59
  • 1
    In Python 2 the map function returns a list instead of a map object, so this should work as is. Commented Jan 21, 2017 at 23:01
  • Thanks so much! That clarifies Commented Jan 21, 2017 at 23:02

5 Answers 5

4

Just add list(),

if __name__ == '__main__':
    n = int(input())
    print(list(map(cube, fibonacci(n))))

I undeleted my comment because this is the is solution to map object, I don't think you can modify it before you actually called. as Ewoud said, map function acts differently in python3 and python2.

Sign up to request clarification or add additional context in comments.

Comments

2

You can just put list(...) around it, so

print(list(map(cube, fibonacci(n))))

(I havent tested the rest of your code)

Edit: If you are not to change the main function you either have to change the print or map function or use python 2.

1 Comment

Once again, the user can not edit that line of code.
1

If you can't edit the main function then use Python2.X instead of Python3.x. In Python3.x, map() function return iterator.

Here's [a link] https://docs.python.org/3/library/functions.html#map

Comments

0

This should give you more than you need. I added a few extra bits like looping and input validation just in case. Hope this helps

General Formula F(n) = F(n-1) + F(n-2)

Get User input

    def get_user_input():
        while True:
            try:
                nums_in_list = int(input("Enter the amout of numbers you want in the fibonacci list: "))

                if nums_in_list < 1:
                    print("Please enter a number greater than 0.")
                    continue

                return nums_in_list

            except ValueError:
                print ("Please enter an integer.")

Create Fabonacci List based on user input

    def genterate_list(user_input):
        fab_list = [0]
        if user_input == 1:
            return fab_list
        elif user_input == 2:
            fab_list.append(1)
            return fab_list
        else:
            count = 1
            while user_input > count:
                if count == 2:
                    fab_list.append(1)
                elif count > 2:
                    fab_list.append(fab_list[count - 2] + fab_list[count - 3])

                count += 1
        return fab_list

Extra Add in to loop the program

def loop_program():
    while True:
        opts = ['y', 'Y', 'N', 'n']

        loop = input("Would you like to run the program again (y/n): ")

        if loop not in opts:
            print("Please select (y/n): ")
            continue
        elif loop == 'N' or loop == 'n':
            return "N"
        else:
            return "Y"

Main section: Calls the functions defined above Displays the outputs.

    if __name__ == '__main__':
        while True:
            original = genterate_list(get_user_input())
            print("Original Fabonacci")

            for x in original:
                print(x)

            cube_funtion = lambda value : value ** 3
            cube_result = map(cube_funtion, original)
            print("Cubed Values")
            list(map(print, cube_result))
            if loop_program() == "N":
                break

Comments

0

I just ran this and it worked successfully on hackerank { cube = lambda x: x**3 # complete the lambda function

def fibonacci(n):
# return a list of fibonacci numbers
initiallist = []
for i in range(n):
    if i < 2:
        initiallist += [i]          
    else:
        initiallist += [initiallist[-1] + initiallist[-2]]
return initiallist

if __name__ == '__main__':
n = int(input())
print(list(map(cube, fibonacci(n))))

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.