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?
__main__then you can only usePython 2instead ofPython 3to get correct result. (or maybe overwrite functionmap)