Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
This is Very Hard!
#1
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?
Reply
#2
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?
Reply
#3
(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
Reply
#4
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
Reply
#5
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
Reply
#6
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'.
Reply
#7
(Aug-15-2020, 05:48 AM)DPaul Wrote: alternative for those who adhere the KISS
I guess it depends what one considers simple :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
Don't do the work for them :(.
Reply
#9
Who's Solution I Should I DO?
Reply
#10
(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. Smile

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'.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020