Here is the problem
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
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].
Follow-up: what if you can't use division?
How would you do this with pen and paper?
(Aug-14-2020, 03:53 PM)Harshil Wrote: [ -> ]Follow-up: what if you can't use division?
Why is division relevant at all?
(Aug-14-2020, 03:55 PM)ndc85430 Wrote: [ -> ]How would you do this with pen and paper?
(Aug-14-2020, 03:53 PM)Harshil Wrote: [ -> ]Follow-up: what if you can't use division?
Why is division relevant at all?
No! Wait I got This Problem And I don't what here to do i can't observe pattern in this problem
Here is a solution for the follow up
def func(iterable):
arr = list(iterable)
prod = 1
for i, x in enumerate(arr):
arr[i] = prod
prod *= x
for j in range(i):
arr[j] *= x
return arr
if __name__ == '__main__':
import unittest as ut
class TestFunc(ut.TestCase):
def test_range(self):
data = [1, 2, 3, 4, 5]
self.assertEqual(func(data), [120, 60, 40, 30, 24])
def test_range2(self):
data = [3, 2, 1]
self.assertEqual(func(data), [2, 3, 6])
ut.main()
Output:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
This is very easy.
Quote:Input = [1, 2, 3, 4, 5]
Output[0] = 2 * 3 * 4 * 5
Output[1] = 1 * 3 * 4 * 5
Output[2] = 1 * 2 * 4 * 5
Output[3] = 1 * 2 * 3 * 5
Output[4] = 1 * 2 * 3 * 4
Output = [120, 60, 40, 30, 24]
There are a few ways you could solve the problem. One involves computing the produce of all values in the list and then getting the value for each position by dividing by the position value.
Quote:Input = [1, 2, 3, 4, 5]
product = 1 * 2 * 3 * 4 * 5 = 120
Output[0] = 120 / 1 = 120
Output[1] = 120 / 2 = 60
Output[2] = 120 / 3 = 40
Output[3] = 120 / 4 = 30
Output[4] = 120 / 5 = 24
Output = [120, 60, 40, 30, 24]
Since this approach fails if any input is zero, it is a no go for me.
The other solution is to iterate through all values and skip the value associated with the current index.
for a = 0 to number of inputs - 1:
outputs[a] = 1
for b = 0 to number of inputs - 1
if a != b:
outputs[a] *= inputs[b]
This solution involves more computation but it works for any list of numbers. Translation to Python is trivial
Other alternative for those who adhere the KISS principle: :-)
Paul
import math
L = [1,2,3,4,5]
result = []
for i in range(5):
L[i] = 1
result.append(math.prod(L))
L = [1,2,3,4,5]
print(result)
Output:
[120, 60, 40, 30, 24]
(Aug-15-2020, 05:48 AM)DPaul Wrote: [ -> ]alternative for those who adhere the KISS
I guess it depends what one considers simple :-)
Don't do the work for them :(.
Who's Solution I Should I DO?
(Aug-15-2020, 07:50 AM)Harshil Wrote: [ -> ]Who's Solution I Should I DO?
I would go for the one i understood the best.
Maybe you have to explain it to somebody.
Paul