user3700852 user3700852 - 1 month ago 24
Python Question

Python for maximum pairwise 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()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"

Answer
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))
Comments