stress test maximum pairwise product in python

#Uses python3
'trying to work with stress testing'

__author__ = 'InnerPeace'

import random
import argparse

#naive solution
def maxpairwiseproduct(n, a):
    assert len(a) == n, "the length of the list is not equal to n = %r" % n
    assert n >= 2, "'n' should be great or equal than 2"
    result = 0
    for i in range(0, n):
        for j in range(i+1, n):
            if a[i]*a[j] > result:
                result = a[i]*a[j]

    return result

#faster vision of solution
def maxpairwiseproductfast(n, a):
    #assert(len(a) ==  n)
    assert len(a) == n, "the length of the list is not equal to n = %r" % n
    assert n >= 2, "'n' should be great or equal than 2"
    #assert(n >= 2)
    max1 = -1
    max2 = -1
    for i in range(0, n):
        if max1 == -1:
            max1 = i
        elif a[i] >= a[max1]:
            max2 = max1
            max1 = i
        elif max2 == -1 or a[max2] < a[i]:
            max2 = i
    return a[max1] * a[max2]


if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument("-st", "-stress-test", help="run the stress testing", action="store_true")
    args = parser.parse_args()
    if args.st:
        while True:
            n = random.randint(0, 1000) + 2
            print (n)
            a = []
            for i in range(0, n):
                a.append(random.randint(0, 10000))
            print (a)

            res1 = maxpairwiseproductfast(n, a)
            res2 = maxpairwiseproduct(n, a)

            if res2 != res1:
                print ('wrong answer: %d, %d' % (res1, res2))
                break
            else:
                print ("ok")
    else:
        n = int(input())
        a = [int(x) for x in input().split()]
        print (maxpairwiseproductfast(n, a))
        #print (maxpairwiseproduct(n, a))

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source