import math from functools import reduce def get_product(numbers): return reduce(lambda a,b:a*b,numbers) def get_negative_products(sorted_asc_negative_numbers): buffer = sorted_asc_negative_numbers.copy() negative_products = [] max_negative_numbers_to_take = len(buffer)-len(buffer)%2 while max_negative_numbers_to_take>0: negative_products.append(get_product(buffer[:2])) buffer = buffer[2:] max_negative_numbers_to_take-=2 return negative_products def get_max_number_of_negative_factor_to_take(neg_count,pos_count,take_count): max_negative_numbers_to_take = min(take_count-take_count%2,neg_count-neg_count%2) while take_count-max_negative_numbers_to_take > pos_count and max_negative_numbers_to_take>0: max_negative_numbers_to_take-=2 return max_negative_numbers_to_take def largest_product(numbers_to_multiply,n=3): # sort and filter list sorted_absolutes = sorted(numbers_to_multiply, key = lambda a: -math.fabs(a)) sorted_negative = list(filter(lambda a: a<0, sorted_absolutes)) sorted_positive = list(filter(lambda a: a>=0, sorted_absolutes)) max_negative_numbers_to_take = get_max_number_of_negative_factor_to_take(len(sorted_negative),len(sorted_positive),n) negative_products = get_negative_products(sorted_negative[0:max_negative_numbers_to_take]) product_index = 0 product_result = 1 while product_index<n-1: if len(negative_products)>0: if negative_products[0]>sorted_positive[0]: product_result *= negative_products.pop(0) product_index += 2 else: product_result *= sorted_positive.pop(0) product_index +=1 else: product_result *= sorted_positive.pop(0) product_index +=1 if product_index<n: product_result *= sorted_positive.pop(0) return product_result def maximum_product_of_three(numbers): return largest_product(numbers)
def maximum_product_of_three(lst):- import math
- from functools import reduce
max_pr = 0num_num = 0- def get_product(numbers):
- return reduce(lambda a,b:a*b,numbers)
for num in lst:for i in range(0,len(lst)):if i != lst[num_num] and num_num+2 < len(lst):try:if (num*lst[i+1]*lst[i+2])>max_pr:max_num = num*lst[i+1]*lst[i+2]except:passnum_num =+ 1- def get_negative_products(sorted_asc_negative_numbers):
- buffer = sorted_asc_negative_numbers.copy()
- negative_products = []
- max_negative_numbers_to_take = len(buffer)-len(buffer)%2
return max_num- while max_negative_numbers_to_take>0:
- negative_products.append(get_product(buffer[:2]))
- buffer = buffer[2:]
- max_negative_numbers_to_take-=2
- return negative_products
- def get_max_number_of_negative_factor_to_take(neg_count,pos_count,take_count):
- max_negative_numbers_to_take = min(take_count-take_count%2,neg_count-neg_count%2)
- while take_count-max_negative_numbers_to_take > pos_count and max_negative_numbers_to_take>0:
- max_negative_numbers_to_take-=2
- return max_negative_numbers_to_take
- def largest_product(numbers_to_multiply,n=3):
- # sort and filter list
- sorted_absolutes = sorted(numbers_to_multiply, key = lambda a: -math.fabs(a))
- sorted_negative = list(filter(lambda a: a<0, sorted_absolutes))
- sorted_positive = list(filter(lambda a: a>=0, sorted_absolutes))
- max_negative_numbers_to_take = get_max_number_of_negative_factor_to_take(len(sorted_negative),len(sorted_positive),n)
- negative_products = get_negative_products(sorted_negative[0:max_negative_numbers_to_take])
- product_index = 0
- product_result = 1
- while product_index<n-1:
- if len(negative_products)>0:
- if negative_products[0]>sorted_positive[0]:
- product_result *= negative_products.pop(0)
- product_index += 2
- else:
- product_result *= sorted_positive.pop(0)
- product_index +=1
- else:
- product_result *= sorted_positive.pop(0)
- product_index +=1
- if product_index<n:
- product_result *= sorted_positive.pop(0)
- return product_result
- def maximum_product_of_three(numbers):
- return largest_product(numbers)