jeudi 4 janvier 2018

Basic Python Algorithm Stress Test

Hi I am learning the basics of algorithms using python, this is my first stress testing script (I am still new to python).

When i run the test both the variables fast and result are printed as 0, the random number generator i included doesn't seem to be creating numbers for the functions mpp and mppf to use, either that or they are not assigning their results to the relevant variable, hence the variable remains = 0 and the loop keeps printing '0 0 OK'

I am receiving no errors except for the fact that my script isn't doing what i want it too!

  import random
    result = 0
    fast = 0

    while result == fast:
        if __name__ == '__main__':
            n = (random.randint(2, 11))
            a = list(random.randint(0, 99999) for r in range(n))
            assert (len(a) == n)

            def max_pairwise_product(n, a):
                global result
                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


            def max_pairwise_product_fast(n, a):
                global fast
                max_index1 = -1
                for i in range(n):
                    if max_index1 == -1 or a[i] > a[max_index1]:
                        max_index1 = i

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

            print(fast, result, "OK")
    else:
        print("Wrong Answer")




Aucun commentaire:

Enregistrer un commentaire