Python Challenge - Max Product Finder

This community-built FAQ covers the “Max Product Finder” 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 Max Product Finder

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 (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 (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 (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

import itertools import numpy def max_product_finder_k(arr, k): return max([numpy.prod(x) for x in list(itertools.combinations(arr, k))]) print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9], 3))

O(nlogn + k^2)

def max_prod_for_sorted(arr, k): 
  res = 1
  for el in arr[:k]: 
    res *= el 
  return res 
def max_product_finder_k(arr, k):
  bestres = float("-inf")
  pos_for_odd = sorted([x for x in arr if x >= 0])
  pos_for_even = pos_for_odd[::-1]
  neg_for_even = sorted([x for x in arr if x < 0]) 
  neg_for_odd = neg_for_even[::-1]
  for i in range(k+1):
    if len(neg_for_odd) < i or len(pos_for_odd) < k-i: continue 
    if i%2: 
      neg = neg_for_odd
      pos = pos_for_odd
    else: 
      neg = neg_for_even 
      pos = pos_for_even 
    res = max_prod_for_sorted(neg, i) * max_prod_for_sorted(pos, k-i)
    bestres = max(bestres, res)
  return bestres

  
print(max_product_finder_k([-1,-2,-3, -4], 1))
from functools import reduce def max_product_finder_k(arr, k): return max( *[reduce(int.__mul__, sorted(arr, reverse=True)[:n], 1) * reduce(int.__mul__, sorted(arr)[:k-n], 1) for n in range(k + 1)], *[reduce(int.__mul__, sorted(arr, reverse=True)[:k-n], 1) * reduce(int.__mul__, sorted(arr)[:n], 1) for n in range(k + 1)] ) print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9], 3))
from itertools import combinations def max_product_finder_k(arr, k): res = [] C = combinations(arr, k) for i in C: n = 1 for j in i: n *= j res.append(n) return max(res) print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9], 3))

No need to convert itertools.combinations into a list.
This will work too:

return max(numpy.prod(x) for x in itertools.combinations(arr, k))

I was not sure about giving the max_prod variable an infinite value.

from itertools import combinations def max_product_finder_k(arr, k): combs = list(combinations(arr,k)) max_prod = -11111111111111110 for comb in combs: # print(comb) prod = 1 for x in comb: prod *= x if prod >= max_prod: max_prod = prod # print(max_prod) return max_prod print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9], 3))

You can set max_prod = 0 as well.

1 Like

Oh yes, of course. Thanks!

1 Like
def max_product_finder_k(arr, k): from itertools import combinations from functools import reduce return max([reduce(lambda x,y:x*y,comb) for comb in combinations(arr,k)]) print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9], 3))

Probably not the best way to do it, but I’ve had functional programming on my brain recently.

Not to be killjoy, but is this one of the challenges that states, ‘no libraries’?

1 Like

It doesn’t say so. Dealing with permutations or combinations is a pain otherwise.

1 Like

Tell me about it. Try doing it on a Timex Sinclair.

from itertools import combinations def max_product_finder_k(arr, k): # Write your code here combos = combinations(arr, k) def getProduct(nums): product = 1 for num in nums: product*=num return product products = [getProduct(nums) for nums in combos] return max(products) print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9], 3))


theres definitely easier solutions out there but i was trying not to use a library and also avoid going into permutations so I think it works quite well for that purpose.

i thought that as well and hate permutations so had to find a way to avoid both of those

1 Like
def max_product_finder_k(arr, k): def recur(arr,k,product): if k == 0 or len(arr) == 0: return product return max([ recur([x for x in arr if x !=rem],k-1,product*rem) for rem in arr]) return recur(arr, k, 1) print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9], 3))

using brute force recursion where
(base case) return accumulated product when

  1. run out of k
  2. no element to choose

(recursive case) find the max product of each element when it is removed from the array

eg max of key 1:[1,2,3] → max of key
{
3:[1,2]
2:[1,3]
1:[2,3]
}

import math import numpy def max_product_finder_k(arr, k): if k==0: return 0 else: x=len(arr)-1 while x>0: for i in range(0,x): if math.fabs(arr[i])<math.fabs(arr[i+1]): arr[i],arr[i+1]=arr[i+1],arr[i] x-=1 l=arr[0:k] if list(numpy.sign(l)).count(-1)%2==0: s=1 for i in l: s*=i else: l.sort(reverse=True) for i in range(0,len(l)): if l[i]<0: for b in arr[k:]: if b>=0: l[i]=b break if list(numpy.sign(l)).count(-1)%2==0: break s=1 for i in l: s*=i return s print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9],6))

Hi, could someone please explain why this is wrong. When I click test code, it says 4/5 tests passed. Thanks.

def max_product_finder_k(arr, k): from itertools import combinations from numpy import Inf from functools import reduce product = lambda x,y: x*y max_ret = -Inf for combo in list(combinations(arr,k)): ret = reduce(product,combo) if ret > max_ret: max_ret = ret return max_ret
from itertools import combinations def product(arr): result = 1 for num in arr: result*=num return result def max_product_finder_k(arr, k): comb = combinations(arr, k) products = [product(nums) for nums in comb] return max(products) print(max_product_finder_k([-8, 6, -7, 3, 2, 1, -9], 3))