Posts: 61
Threads: 26
Joined: Aug 2020
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?
Posts: 1,838
Threads: 2
Joined: Apr 2017
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?
Posts: 61
Threads: 26
Joined: Aug 2020
(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
Posts: 4,790
Threads: 76
Joined: Jan 2018
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
Posts: 6,800
Threads: 20
Joined: Feb 2020
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
Posts: 741
Threads: 122
Joined: Dec 2017
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]
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Aug-15-2020, 06:27 AM
(This post was last modified: Aug-15-2020, 06:27 AM by buran.)
(Aug-15-2020, 05:48 AM)DPaul Wrote: alternative for those who adhere the KISS I guess it depends what one considers simple :-)
Posts: 1,838
Threads: 2
Joined: Apr 2017
Don't do the work for them :(.
Posts: 61
Threads: 26
Joined: Aug 2020
Who's Solution I Should I DO?
Posts: 741
Threads: 122
Joined: Dec 2017
(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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
|