This community-built FAQ covers the “Product of Everything Else” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.
Top Discussions on the Python challenge Product of Everything Else
There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in #get-help.
Agree with a comment or answer? Like () to up-vote the contribution!
This is my answer, not sure if there is a more efficient way:
def product_of_the_others(array):
new_list =
for i in range(len(array)):
temp_list = array[:i] + array[i+1:]
product = 1
for x in temp_list:
product = product * x
new_list.append(product)
return new_list
def product_of_the_others(array):
arr = []
for i, x in enumerate(array):
temp = array[:]
temp[i] = 1
prod = 1
for n in temp:
prod *= n
arr += [prod]
return arr
print(product_of_the_others([1, 2, 3, 4, 5]))
def product_of_the_others(array):
# Write your code here
position = 0
array_instance = []
new_list = []
for i in array:
new_number = 1
array_instance = array.copy()
array_instance.pop(position)
for i in array_instance:
new_number *= i
position+=1
new_list.append(new_number)
return new_list
print(product_of_the_others([1, 2, 3, 4, 5]))
from collections import deque
def product_of_the_others(array):
jar = deque(array)
tulemus = 1
counter = 0
uus = []
while counter < len(jar):
for i in range(1,len(jar)):
tulemus *= jar[i]
uus.append(tulemus)
jar.rotate(-1)
counter +=1
tulemus = 1
return uus
print(product_of_the_others([1, 2, 3, 4, 5]))
def product_of_the_others(array):
tulemus = 1
counter = 0
uus = []
while counter < len(array):
for i in range(1,len(array)):
tulemus *= array[i]
uus.append(tulemus)
es = array.pop(0)
array.append(es)
counter +=1
tulemus = 1
return uus
print(product_of_the_others([1, 2, 3, 4, 5]))
def product_of_the_others(array):
fullprod = 1;
zeroes = 0
for el in array:
if el != 0:
fullprod *= el
else:
zeroes += 1
return [0 if (zeroes and el != 0 or zeroes > 1 and el == 0) else fullprod/(el if el else 1) for el in array]
print(product_of_the_others([5,5,0,]))
def product_of_the_others(array):
tulemus = 1
uus = []
for i in range(len(array)):
for j in range (len(array)):
if i != j:
tulemus *=array[j]
uus.append(tulemus)
tulemus = 1
return uus
print(product_of_the_others([1, 2, 3, 4, 5]))
def product_of_the_others(array):
product = 1
res = []
for i in range (len(array)):
num_taken = array.pop(i)
for j in range (len(array)):
product *= array[j]
res.append(product)
array.insert(i, num_taken)
product = 1
return res
print(product_of_the_others([1, 2, 3, 4, 5]))
I first duplicates the original array with temp_array = array, but then all chages on the temp_array will effect the array itself as well.
def product_of_the_others(array):
products = []
for i in range(len(array)):
result = 1
temp_array = []
for x in array:
temp_array.append(x)
temp_array.remove(array[i])
for y in temp_array:
result *= y
products.append(result)
return products
print(product_of_the_others([1, 2, 3, 4, 5]))
def product_of_the_others(array):
# Write your code here
storage=1
newArray=[]
for x in range(len(array)):
for y in range(len(array)):
if x==y:
pass
else:
storage=storage*array[y]
newArray.append(storage)
storage=1
return(newArray)
print(product_of_the_others([1, 2, 3, 4, 5]))
def product_of_the_others(array):
from functools import reduce # functools comes standard; Guido doesn't like reduce(), though!
return_list = []
for i in array:
to_mult = array.copy() # copy the array, since I'm modifying it.
to_mult.remove(i) # remove the current index from the copy.
return_list.append(reduce(lambda x,y: x*y, to_mult)) # append the reduced list via multiplication
return return_list
print(product_of_the_others([1, 2, 3, 4, 5]))
Been toying around with functional programming, so I’m using lambda, map, filter, and reduce when possible.
I was able to use a custom product function and list slicing to solve this way.
The space complexity is O(N) and time complexity is O(N**2).
I don’t know if there is a more efficient way
import numpy
def product_of_the_others(array):
# Write your code here
lkjasd = []
yes = []
for i in range(len(array)):
yes = []
for j in range(len(array)):
if j != i:
yes.append(array[j])
lkjasd.append(numpy.prod(yes))
return lkjasd
print(product_of_the_others([1,2,3,4,5]))
will post explanation later (codecademy pls don’t kill me)
import numpy as np
def product_of_the_others(array):
# Write your code here
result = []
for idx in range(len(array)):
temp_array = array.copy()
temp_array.pop(idx)
result.append(np.product(temp_array))
return result
print(product_of_the_others([1, 2, 3, 4, 5]))
print(product_of_the_others([5, 5, 5]))
Classic version, which is slower, but works better if the numbers get big. O(n2)
It has a loop in a loop, iterating by index in each.
code for classic version
def product_of_the_others(array):
copy = array.copy()
length = len(array)
for i in range(length):
product = 1
for j in range(length):
if j != i:
product = product * array[j]
copy[i] = product
return copy
And a version that uses division (in a loop), which is faster than the other version, but requires logic to deal with edge cases. It’s almost O(n)
(Divides product of all of them by each integer in the list to get the answers.)
code for division version
# /* function for getting product of everything in the array */
def multiply(array):
so_far = 1
for x in array:
so_far *= x
return so_far
# /* function to check if everything in the list is an integer */
def all_are_integers(array):
if len(array) < 1:
return False
for x in array:
if not isinstance(x, int):
return False
return True
# /* function where store product, use division */
def product_of_the_others(array):
if (array.count(0) >= 2):
return [0 for x in array] # /* return all 0s */
product_of_all = multiply(array)
length = len(array)
copy = array.copy()
if all_are_integers(array) and isinstance(product_of_all, int):
div = lambda a, b : a // b
else:
div = lambda a, b : a / b
for i in range(length):
if copy[i] == 0:
product = 1
for j in range(length):
if j != i:
product = product * array[j]
copy[i] = product
else:
copy[i] = div( product_of_all, copy[i] )
return copy
My code is longer than the others. I’m not an efficient coder.