- 1
Given an array of integers, return a array such that each element at index i of the new array is the product of all the number
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
7 Answers
+ 5
Mahir Shah If you are asking solution then I will just say to do first by yourself if you want to learn coding.
+ 4
Hy Mahir Shah
Your code is correct(make first line "enter number of elements", rest is fine), for improvement you can take product of all elements while taking input only and then make desired list by each ith element as (product of all elements)/(ith element of original list)
If you are posting it as a Challenge/Assignment, then q/a section is not correct place to do so, you can post it as Assignment in Lesson factory OR make use of personal feed post.
+ 1
I know this is frowned uppon, but, I'm learning by doing it, hopefully so does the person who posted the question.
import functools
mylist1 = [1, 2, 3, 4, 5]
mylist2 = []
for z in range(len(mylist1)):
mylist2.append(functools.reduce(lambda x, y: x * y, mylist1) / mylist1[z])
print(mylist2)
0
No i posted a question
0
Thanks Gaurav