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 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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].

6th Oct 2019, 1:36 PM
MahirShah
6 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.
6th Oct 2019, 2:31 PM
A͢J
A͢J - avatar
+ 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.
6th Oct 2019, 5:22 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
6th Oct 2019, 4:04 PM
MahirShah
+ 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)
6th Oct 2019, 4:15 PM
rodwynnejones
rodwynnejones - avatar
0
No i posted a question
6th Oct 2019, 3:56 PM
MahirShah
0
Thanks Gaurav
6th Oct 2019, 5:57 PM
MahirShah