Find the maximum multiplication - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Find the maximum multiplication (/thread-31243.html) |
Find the maximum multiplication - ercv - Nov-30-2020 So you are given with the list of number, you should choose the three of them which multiplication is the greatest. The problem lies in the fact that the complexety of the code should be O(n). That's why the easiest solution (by sorting at first)is out. As for me the only way out is to find the first three maximum values(I understand how to find two numbers, but with the third I have some problems) and two minimum, and then compare their multiplication. Maybe there is easier solution? Here is some tests: Input:3 5 1 7 9 0 9 -3 10 Output:10 9 9 Input:-5 -30000 -12 Output:-5 -12 -30000 Here is the code for the same task, but for mutiplication of two numbers: a = list(map(int, input().split())) maxpos1, maxpos2 = a[0], a[1] maxneg1, maxneg2 = a[0], a[1] if len(a) == 2: print(min(maxpos1,maxpos2),max(maxpos1,maxpos2)) else: for i in range(2,len(a)): if a[i] >= maxpos1 and a[i] >= 0: maxpos2 = maxpos1 maxpos1 = a[i] elif maxpos2 < a[i] < maxpos1: maxpos2 = a[i] elif a[i] <= maxneg1 and a[i] < 0: maxneg2 = maxneg1 maxneg1 = a[i] elif maxneg1 < a[i] < maxneg2: maxneg2 = a[i] if maxneg1*maxneg2 > maxpos1*maxpos2: print(maxneg1, maxneg2) else: print(maxpos2, maxpos1) RE: Find the maximum multiplication - DeaD_EyE - Nov-30-2020 https://docs.python.org/3.9/library/heapq.html#heapq.nlargest import heapq data = (3, 5, 1, 7, 9, 0, 9, -3, 10) nlargest = heapq.nlargest(3, data) print(nlargest) if nlargest == sorted(data, reverse=True)[:3]: print("Result is correct") RE: Find the maximum multiplication - DPaul - Nov-30-2020 I wonder if there is a catch here. Because if a list contains 2 negatives, eg. - 1000 and -2000, their product is very large and positive. Paul RE: Find the maximum multiplication - DeaD_EyE - Nov-30-2020 Ah, I did not read it completely.
Is this right? The function math.prod calculates the Product and was introduced with Python 3.8. from heapq import nsmallest, nlargest from math import prod input_data = (3, 5, 1, 7, 9, 0, 9, -3, 10) largest = nlargest(3, input_data) smallest = nsmallest(2, input_data) product_largest = prod(largest) product_smallest = prod(smallest) if product_largest > product_smallest: print( f"Product of {largest} == {product_largest} " f"is bigger than the Product of {smallest} " f"== {product_smallest}" ) else: print(f"Product of {smallest} is bigger than the Product of {largest}")
print(prod(largest)) print(prod(smallest))The result from nlargest: [10, 9, 9] # big values first The result from nsmallest: [-3, 0] # small values first Your data contains also negative values and a 0 .If there is one 0 in your data, the result of product is 0 .If you have an even number of negative values, the product will be positive. An odd number of negative values will result in a negative result. |