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!