0

I'm taking an algorithms course online and I am trying to calculate the maximum pairwise product in a list of numbers. This question has already been answered before:

maximum pairwise product fast solution and Python for maximum pairwise product

I was able to pass the assignment by looking at those two posts. I was hoping that maybe someone could help me figure out how to correct my solution. I was able to apply a stress test and found out if the largest number in the array is in the starting index it will just multiply itself twice.

This is the test case I failed using the assignment automatic grader

Input:
2
100000 90000

Your output:
10000000000

Correct output:
9000000000

Here is my pairwise method and stress test

from random import randint

def max_pairwise_product(numbers):
    n = len(numbers)
    max_product = 0
    for first in range(n):
        for second in range(first + 1, n):
            max_product = max(max_product,
                numbers[first] * numbers[second])

    return max_product

def pairwise1(numbers):
    max_index1 =  0
    max_index2 = 0

    #find the highest number
    for i, val in enumerate(numbers):
        if int(numbers[i]) > int(numbers[max_index1]):
            max_index1 = i


    #find the second highest number
    for j, val in enumerate(numbers):
        if j != max_index1 and int(numbers[j]) > int(numbers[max_index2]):
            max_index2 = j


    #print(max_index1)    
    #print(max_index2)

    return int(numbers[max_index1]) * int(numbers[max_index2])   

def stressTest():
   while True:
        arr = []
        for x in range(5):
            random_num = randint(2,101)
            arr.append(random_num)
        print(arr)
        print('####')
        result1 = max_pairwise_product(arr)
        result2 = pairwise1(arr)
        print("Result 1 {}, Result2 {}".format(result1,result2))

        if result1 != result2:
            print("wrong answer: {} **** {}".format(result1, result2))
            break
        else:
            print("############################################# \n Ok", result1, result2)

if __name__ == '__main__':
    stressTest()
'''
    length = input()
    a = [int(x) for x in input().split()]
    answer = pairwise1(a)
    print(answer)
'''

Any feedback will be greatly appreciated. 
Thanks.


Zaynaib Giwa
  • 5,366
  • 7
  • 21
  • 26

1 Answers1

1

When max number is on position 0, you will get both max_index1 and max_index2 as 0. That's why you are getting like this .

Add the following lines before #find the second highest number in pairwise1 function .

if max_index1==0:
    max_index2=1
else:
    max_index2=0

So function will be like:

def pairwise1(numbers):
    max_index1 =  0
    max_index2 =  0

    #find the highest number
    for i, val in enumerate(numbers):
        if int(numbers[i]) > int(numbers[max_index1]):
            max_index1 = i

    if max_index1==0:
        max_index2=1
    else:
        max_index2=0

    #find the second highest number
    for j, val in enumerate(numbers):
        if j != max_index1 and int(numbers[j]) > int(numbers[max_index2]):
            max_index2 = j


    #print(max_index1)    
    #print(max_index2)

    return int(numbers[max_index1]) * int(numbers[max_index2])   
mahbubcseju
  • 2,200
  • 2
  • 16
  • 21
  • Thank for the simple solution. I was most definitely making things over complicated when I tried to come up with my own solution. I was wondering if you have any tips on how to get better with solving algorithmic problems and coding challenges. I've read your profile and it seems like you've done a lot. – Zaynaib Giwa Jun 19 '19 at 12:37