Python for maximum pairwise product

▼魔方 西西 提交于 2019-12-01 13:10:49

问题


n = int(input("Enter the number of elements in the array (2-200,000):"))
a = [int(x) for x in input("Enter all numbers of the sequence with only non-negative intergers not exceeding 100,000:").split()]
c = list()3

for i in range(0,n):
    for j in range (1,n):
        if a[i] != a[j]:
            m = a[i]*a[j]
            c.append(m)
        else:
            continue
print(max(c))

This code works. However, I want to define a function to automatically calculating the max product from the 5th line in the code shown below.

def MaxPairwiseProduct(n,a,c):
for i in range(0,n):
    for j in range (1,n):
        if a[i] != a[j]:
            m = a[i]*a[j]
            c.append(m)
        else:
            continue

        Product = max(c)

        return Product

n = int(input("Enter the number of elements in the array (2-200,000):"))
a = [int(x) for x in input("Enter all numbers of the sequence with only non-negative intergers not exceeding 100,000:").split()]
c = list()
MaxPairwiseProduct(n,a,c)

I re-write the function but it does not work. It reveals "IndentationError: expected an indented block"


回答1:


# Uses python3
def MaxPairwiseProduct(n,a,c):
    for i in range(0,n):
        for j in range (1,n):
            if a[i] != a[j]:
                m = a[i]*a[j]
                c.append(m)

            else:
                continue

    Product1 = max(c)
    return Product1

def MaxPairwiseProductFast(n,a):
    max_index1 = -1
    for i in range(0,n):
        if a[i] > a[max_index1]:
            max_index1 = i
        else:
            continue

    max_index2 = -1
    for j in range(0,n):
        if a[j] > a[max_index2] and a[j] != a[max_index1]:
            max_index2 = j   
        else:
            continue

    Product2 = a[max_index1]*a[max_index2]
    return Product2

n = int(input("Enter the number of elements in the array (2-200,000):"))
a = [int(x) for x in input("Enter all numbers of the sequence with only non-negative intergers not exceeding 100,000:").split()]
c = list()

print('The max value by regular algorithm:', MaxPairwiseProduct(n,a,c))
print('The max value by faster algorithm:', MaxPairwiseProductFast(n,a))  

This code contains the second algorithm to calculate the max value.




回答2:


def MaxPairwiseProduct(n,a,c):
    for i in range(0,n):
        for j in range (1,n):
            if a[i] != a[j]:
                m = a[i]*a[j]
                c.append(m)

            else:
                continue

    Product = max(c)
    return Product

n = int(input("Enter the number of elements in the array (2-200,000):"))
a = [int(x) for x in input("Enter all numbers of the sequence with only non-negative intergers not exceeding 100,000:").split()]
c = list()
print(MaxPairwiseProduct(n,a,c))



回答3:


My stupid solution to the maximum pairwise product.

   n = int(input()) #
   nums = input().strip().split()
   nums = [ int(i) for i in nums ]
   firstmax, secondmax = sorted(set(nums))[-2:]
   print(firstmax*secondmax)



回答4:


# python3
result = 0
def max_pairwise_product_fast(n, numbers):
    max_index1 = -1
    for i in range(n):
        if max_index1 == -1 or numbers[i] > numbers[max_index1]:
            max_index1 = i
    max_index2 = -1

   for i in range(n):
        if i != max_index1 and (max_index2 == -1 or numbers[i] > numbers[max_index2]):
            max_index2 = i

    return numbers[max_index1] * numbers[max_index2]
if __name__ == '__main__':
    n = int(input())
    a = [int(x) for x in input().split()]
    assert (len(a) == n)

    print(max_pairwise_product_fast(n, a))


来源:https://stackoverflow.com/questions/39950568/python-for-maximum-pairwise-product

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!